<?php
require_once 'config/database.php';
secureSessionStart();

// Log the logout
if (isset($_SESSION['admin_id']) && isset($conn)) {
    $ip = $_SERVER['REMOTE_ADDR'];
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $stmt = mysqli_prepare($conn, "INSERT INTO admin_logs (admin_id, action, ip_address, user_agent) VALUES (?, 'logout', ?, ?)");
    mysqli_stmt_bind_param($stmt, "iss", $_SESSION['admin_id'], $ip, $user_agent);
    mysqli_stmt_execute($stmt);
}

// Destroy session
$_SESSION = array();
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}
session_destroy();

header("Location: login.php");
exit();
?>