<?php

namespace App\Filters;

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

class TipsterAuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        if (!session()->get('tipsterLoggedIn')) {
            return redirect()->to('/tipster/login');
        }

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

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

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

