<?php

namespace App\Http\Middleware;

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

class EnsurePluginEnabled
{
    public function __construct(private readonly PluginManager $plugins)
    {
    }

    public function handle(Request $request, Closure $next): Response
    {
        $plugin = $this->plugins->pluginForRoute($request->route()?->getName());

        if ($plugin && ! $this->plugins->isEnabled($plugin)) {
            abort(404, 'This feature is currently disabled.');
        }

        return $next($request);
    }
}
