<?php

namespace App\Http\Middleware;

use App\Services\FeatureFlagService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class FeatureFlagMiddleware
{
    public function __construct(
        protected FeatureFlagService $featureFlagService
    ) {
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next, string $featureKey): Response
    {
        $userId = auth()->id();
        
        if (!$this->featureFlagService->isEnabled($featureKey, $userId)) {
            abort(404, 'Feature not available');
        }

        return $next($request);
    }
}
