<?php
session_start();

// Record logout activity
if (isset($_SESSION['admin_id'])) {
    require_once '../config/database.php';
    $db = Database::getInstance()->connect();
    
    $stmt = $db->prepare("INSERT INTO activity_logs (admin_id, action, description, ip_address, user_agent, details, created_at)
                         VALUES (?, 'logout', 'Admin logout', ?, ?, ?, NOW())");
    $stmt->execute([
        $_SESSION['admin_id'],
        $_SERVER['REMOTE_ADDR'] ?? 'unknown',
        $_SERVER['HTTP_USER_AGENT'] ?? 'unknown',
        json_encode(['action' => 'logout'])
    ]);
}

// Destroy session
session_destroy();

// Clear session cookie
setcookie(session_name(), '', time() - 3600, '/');

// Redirect to login
if (isset($_GET['timeout'])) {
    header("Location: login.php?timeout=1");
} else {
    header("Location: login.php");
}
exit();
?>