<?php

namespace App\Http\Middleware;

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

class EnsureModuleAccess
{
    public function handle(Request $request, Closure $next): Response
    {
        $module = $request->route()?->getPrefix();
        $tenant = $request->user()?->tenant;

        if ($tenant && !$tenant->hasModuleAccess($module)) {
            return response()->json(['message' => 'This module is not enabled for the current package.'], 403);
        }

        return $next($request);
    }
}
