<?php

namespace App\Filters;

use App\Core\Auth\Services\AuthService;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class PermissionFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $permission = $arguments[0] ?? null;

        if ($permission && ! (new AuthService())->can($permission)) {
            return redirect()->to('/dashboard')->with('error', 'Accès refusé pour votre rôle.');
        }

        return null;
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        return null;
    }
}
