<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class RoleFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $user = service('session')->get('auth_user');
        if (! $user) return redirect()->to('/login');
        $allowed = $arguments ?: [];
        if ($allowed && ! in_array($user['role'] ?? '', $allowed, true)) {
            return redirect()->to('/')->with('error', 'You do not have permission to access that area.');
        }
    }

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