<?php
require_once 'utils/helpers.php';

try {

    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
        Helpers::sendResponse(false, 'Method not allowed', null, 405);
    }

    // Get and verify token
    $token = Helpers::getBearerToken();
    $userData = Helpers::verifyJWT($token);

    if (!$userData) {
        Helpers::sendResponse(false, 'Unauthorized', null, 401);
    }

    // In a stateless JWT system, logout is handled client-side by removing the token
    // We just confirm the request
    Helpers::sendResponse(true, 'Logout successful');
} catch (PDOException $e) {
    ErrorLogger::logDatabaseError($e, $logQuery ?? 'Logout query', [
        'endpoint' => 'auth/logout',
        'user_id' => $user->user_id ?? 'unknown'
    ]);
    Helpers::sendResponse(false, 'An error occurred during logout', null, 500);
} catch (Exception $e) {
    ErrorLogger::logError($e, [
        'endpoint' => 'auth/logout'
    ]);
    Helpers::sendResponse(false, 'An unexpected error occurred', null, 500);
}
