<?php

namespace App\Filters;

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

class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        if (!session()->has('id_user')) {
            return redirect()->to('/login')
                ->with('error', 'Anda harus login terlebih dahulu.');
        }

        // Cek role jika ada parameter auth:1, auth:2, auth:3
        if (!empty($arguments)) {
            $role = session()->get('id_role');

            if ($role != $arguments[0]) {
                return redirect()->to('/dashboard')
                    ->with('error', 'Anda tidak memiliki hak akses.');
            }
        }
    }

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