<?php

namespace App\Filters;

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

class AdminFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $user = session()->get('auth_user');

        if (! $user) {
            return redirect()->to(site_url('login'))->with('error', 'Tens de iniciar sessão para continuar.');
        }

        if (($user['role'] ?? null) !== 'admin') {
            return redirect()->to(site_url('dashboard'))->with('error', 'Não tens permissões de administrador.');
        }

        return null;
    }

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