<?php

namespace App\Filters;

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

class GuestFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        $user = service('session')->get('auth_user');
        if ($user) {
            $path = match ($user['role'] ?? 'patient') {
                'admin', 'operations' => '/admin',
                'practitioner' => '/practitioner',
                'partner' => '/partner',
                default => '/patient',
            };
            return redirect()->to($path);
        }
    }

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