<?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)
    {
        $role = session()->get('role');
        
        if ($arguments && !in_array($role, $arguments)) {
            // Redirect to appropriate dashboard if role mismatch
            if ($role == 'wali') return redirect()->to('/wali');
            if ($role == 'kesantrian') return redirect()->to('/kesantrian');
            if ($role == 'satpam') return redirect()->to('/satpam');
            if ($role == 'admin') return redirect()->to('/admin');
            
            return redirect()->to('/login');
        }
    }

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