<?php

declare(strict_types=1);

namespace App\Http\Middleware;

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

final class EnsureStaffAccess
{
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        if (! $user || ! $user->hasStaffAccess()) {
            if ($request->expectsJson() || $request->header('X-Inertia')) {
                abort(Response::HTTP_FORBIDDEN, 'Forbidden.');
            }

            return redirect()->route('index')->with('error', 'You do not have access to this area.');
        }

        return $next($request);
    }
}
