<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../classes/User.php';

$user = new User();

try {
    $user->logout();
} catch (Exception $e) {
    // Ignore errors during logout
}

// Ensure session cookie is cleared even if session_destroy already ran
if (ini_get('session.use_cookies')) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', [
        'expires' => time() - 42000,
        'path' => $params['path'] ?? '/',
        'domain' => $params['domain'] ?? '',
        'secure' => $params['secure'] ?? false,
        'httponly' => $params['httponly'] ?? true,
        'samesite' => $params['samesite'] ?? 'Lax'
    ]);
}

// Also clear remember token cookie
setcookie('remember_token', '', [
    'expires' => time() - 42000,
    'path' => '/',
    'secure' => false,
    'httponly' => true,
    'samesite' => 'Lax'
]);

redirect('login.php');
