<?php
require __DIR__.'/_bootstrap.php';
use Zoo\Database;
use Zoo\Auth\UserAuthService;

function bearer_token(): string {
    $h = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
    if (stripos($h,'Bearer ')===0) return trim(substr($h,7));
    return (string)($_SERVER['HTTP_X_USER_SESSION'] ?? '');
}

try {
    $token=bearer_token();
    (new UserAuthService(Database::connect()))->logout($token);
    json_out(['ok'=>true]);
} catch(Throwable $e) {
    json_out(['ok'=>false,'error'=>$e->getMessage()],400);
}
