<?php

namespace App\Http\Middleware;

use App\Support\CheckoutRoutes;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
    protected function redirectTo(Request $request): ?string
    {
        if ($request->expectsJson()) {
            return null;
        }

        if ($request->routeIs('buy.packages') && $request->route('packageId')) {
            return CheckoutRoutes::wizardUrl(
                $request->route('packageId'),
                $request->route('locale')
            );
        }

        return route('login');
    }
}
