<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (! $request->user()?->isAdmin()) {
            if ($request->user()?->isSupportTeam()) {
                return redirect()
                    ->route('support-team.dashboard')
                    ->with('error', 'Only administrators can access the admin panel.');
            }

            return redirect()
                ->route('dashboard')
                ->with('error', 'Only administrators can access the admin panel.');
        }

        return $next($request);
    }
}
