<?php
// File: logout.php
require_once 'config/auth.php';

// Log activity before logout
if (isset($_SESSION['user_id'])) {
    // Optional: Log logout activity
    $log_file = 'logs/activity.log';
    $log_dir = dirname($log_file);
    
    if (!is_dir($log_dir)) {
        mkdir($log_dir, 0755, true);
    }
    
    $log_message = date('Y-m-d H:i:s') . " - User: {$_SESSION['username']} logged out - Auto: " . (isset($_GET['auto']) ? 'Yes' : 'No') . PHP_EOL;
    file_put_contents($log_file, $log_message, FILE_APPEND);
}

// Get redirect parameter
$redirect = isset($_GET['redirect']) ? $_GET['redirect'] : 'index';

// Destroy session
logout(); // Function from auth.php will destroy session and redirect

// If logout() doesn't redirect, do it manually
if ($redirect === 'index') {
    header('Location: index.php');
} else {
    header('Location: ' . $redirect . '.php');
}
exit;
?>