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

try {
    $in=input_json();
    $db=Database::connect();
    $user=(new UserAuthService($db))->login(
        (string)($in['phone']??''),
        (string)($in['password']??''),
        $_SERVER['HTTP_USER_AGENT']??null,
        $_SERVER['REMOTE_ADDR']??null
    );
    $wallet=(new WalletQuery($db))->get((int)$user['user_id']);
    json_out(['ok'=>true,'user'=>$user,'wallet'=>$wallet]);
} catch(Throwable $e) {
    json_out(['ok'=>false,'error'=>$e->getMessage()],401);
}
