<?php

namespace App\Http\Middleware;

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

class EnsureUserIsSuperAdmin
{
    /**
     * Restrict a route to platform-wide Super Admins (company_id = null).
     * The old project relied entirely on hiding menu links in Blade — any
     * authenticated user could still POST directly to /savecompanies. This
     * enforces it at the route level instead.
     */
    public function handle(Request $request, Closure $next): Response
    {
        abort_unless($request->user()?->isSuperAdmin(), 403, 'Only a Super Admin can manage companies.');

        return $next($request);
    }
}
