<?php
class PermissionMiddleware
{
    private string $permission;

    public function __construct(string $permission = '')
    {
        $this->permission = $permission;
    }

    public function handle(Request $request): void
    {
        if ($this->permission && !Gate::can($this->permission)) {
            if ($request->isAjax()) {
                Response::json(['success' => false, 'message' => 'Forbidden'], 403);
            }
            http_response_code(403);
            $viewFile = ROOT_PATH . '/views/errors/403.php';
            if (file_exists($viewFile)) {
                include $viewFile;
            }
            exit;
        }
    }
}