<?php

namespace App\Http\Middleware;

use Filament\Http\Middleware\Authenticate as FilamentAuthenticateMiddleware;
use Illuminate\Http\Request;

class FilamentAuthenticate extends FilamentAuthenticateMiddleware
{
    /**
     * Handle an incoming request.
     */
    public function handle($request, \Closure $next, ...$guards): mixed
    {
        $this->authenticate($request, $guards);

        $user = $request->user();

        if (! $user || ! $user->is_admin) {
            abort(403, 'You do not have permission to access the admin panel.');
        }

        if ($user->banned) {
            abort(403, 'Your account has been banned.');
        }

        return $next($request);
    }

    protected function redirectTo($request): ?string
    {
        return route('login');
    }
}
