<?php

namespace App\Http\Middleware;

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

class EnsureTenantHasMenu
{
    public function handle(Request $request, Closure $next, string $menuCode): Response
    {
        $tenant = $request->user()?->tenant()->first();
        $hasMenu = $tenant?->menues()
            ->where('menues.code', $menuCode)
            ->exists() ?? false;

        abort_unless($hasMenu, 404);

        return $next($request);
    }
}
