<?php
require_once 'includes/init.php';

// Logout user
if (isAuthenticated()) {
    $db = getDB();
    
    // Invalidate session
    if (isset($_SESSION['session_token'])) {
        $stmt = $db->prepare("UPDATE sessions SET is_valid = 0 WHERE session_token = ?");
        $stmt->execute([$_SESSION['session_token']]);
    }
    
    // Log activity
    $stmt = $db->prepare("INSERT INTO activity_logs (user_id, action, details, ip_address, user_agent) VALUES (?, 'logout', 'User logged out', ?, ?)");
    $stmt->execute([$_SESSION['user_id'], $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] ?? '']);
}

// Clear session
session_destroy();

// Redirect to login
redirect(APP_URL . '/login.php');
?>
