<?php

// app/Http/Middleware/AuthAnyGuard.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class AuthAnyGuard
{
    public function handle($request, Closure $next, ...$guards)
    {
        // List of route names or paths to skip
        $excludedRoutes = [
            'login',
            'admin.login',
            'bank.login',
            // add other public route names here
        ];

        if ($request->route() && in_array($request->route()->getName(), $excludedRoutes)) {
            return $next($request);
        }

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                Auth::shouldUse($guard);
                return $next($request);
            }
        }

        // Store the intended URL before redirecting to login
        return redirect()->guest(LaravelLocalization::localizeUrl('/login'));
    }
}
