<?php
namespace App\Middleware;

class AdminAuth
{
    public function handle(): void
    {
        $timeout = 30 * 60; // 30 minutos de inactividade

        if (empty($_SESSION['admin'])) {
            redirect(base_url('admin/login'));
        }

        // Verificar timeout de inactividade
        if (isset($_SESSION['admin_last_active'])) {
            if (\time() - $_SESSION['admin_last_active'] > $timeout) {
                $_SESSION = [];
                \session_destroy();
                redirect(base_url('admin/login?expired=1'));
            }
        }

        // Actualizar timestamp de última actividade
        $_SESSION['admin_last_active'] = \time();
    }
}
