<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
use App\Helpers\PermissionHelper;

class PermissionMiddleware
{
    public function handle(Request $request, Closure $next, string $resource, string $action = 'view'): Response
    {
        $user = Auth::user();

        if (!$user || !PermissionHelper::can($user, $resource, $action)) {
            abort(403, 'Accès non autorisé à cette ressource.');
        }

        return $next($request);
    }
}
