<?php

require_once __DIR__ . '/db/auth.php';

auth_start_session();

$_SESSION = [];

if (ini_get('session.use_cookies')) {
    $session_cookie_params = session_get_cookie_params();
    setcookie(
        session_name(),
        '',
        time() - 42000,
        $session_cookie_params['path'],
        $session_cookie_params['domain'],
        $session_cookie_params['secure'],
        $session_cookie_params['httponly']
    );
}

session_destroy();

$is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';

if ($is_ajax) {
    header('Content-Type: application/json; charset=UTF-8');
    echo json_encode([
        'success' => true,
        'message' => 'Déconnexion effectuée.',
    ], JSON_UNESCAPED_UNICODE);
    exit;
}

header('Location: index.php');
exit;
