<?php
require __DIR__.'/_bootstrap.php';
use Zoo\Database;
use Zoo\Auth\GuestService;
use Zoo\Wallet\WalletQuery;

try {
    $in=input_json();
    $token=(string)($in['session_token'] ?? ($_SERVER['HTTP_X_GUEST_SESSION'] ?? ''));
    $db=Database::connect();
    $guest=(new GuestService($db))->authenticate(
        $token,
        $_SERVER['HTTP_USER_AGENT'] ?? null,
        $_SERVER['REMOTE_ADDR'] ?? null
    );
    $wallet=(new WalletQuery($db))->get((int)$guest['user_id']);
    json_out(['ok'=>true,'guest'=>$guest,'wallet'=>$wallet]);
} catch(Throwable $e) {
    json_out(['ok'=>false,'error'=>$e->getMessage()],401);
}
