<?php

namespace App\Http\Middleware;

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

class EnsureModuleEnabled
{
    public function __construct(private ModuleManager $modules) {}

    public function handle(Request $request, Closure $next, string $module): Response
    {
        abort_unless(isset(ModuleManager::MODULES[$module]), 404);
        abort_unless(
            $this->modules->enabled($module),
            404,
            ModuleManager::MODULES[$module]['label'].' module is disabled in System Settings.'
        );

        return $next($request);
    }
}
