<?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()->get('isLoggedIn')) {
            return redirect()->to('/login');
        }

        // Check session timeout (30 minutes)
        $lastActivity = session()->get('lastActivity');
        if ($lastActivity && (time() - $lastActivity > 1800)) {
            session()->destroy();
            return redirect()->to('/login')->with('error', 'Session expired. Please login again.');
        }

        // Update last activity time
        session()->set('lastActivity', time());
    }

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