<?php

namespace App\Http\Middleware;

use App\Services\ModuleEntitlementService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureModuleEnabled
{
    public function handle(Request $request, Closure $next, string $moduleKey): Response
    {
        $schoolId = $request->user()?->school_id;

        if (! $schoolId || ! app(ModuleEntitlementService::class)->schoolHasModule($schoolId, $moduleKey)) {
            abort(403, 'این قابلیت برای مدرسه فعال نیست.');
        }

        return $next($request);
    }
}
