<?php
require __DIR__.'/_bootstrap.php';
use Zoo\Database;
use Zoo\Auth\PlayerAuthService;
use Zoo\Withdrawal\WithdrawalService;


try {
    $in=input_json(); $db=Database::connect();
    $user=(new PlayerAuthService($db))->authenticate($in);
    $result=(new WithdrawalService($db))->request(
        (int)$user['user_id'],
        (int)($in['withdrawal_account_id']??0),
        (float)($in['amount']??0),
        (string)($in['payout_route']??'direct'),
        isset($in['agent_code'])?(string)$in['agent_code']:null,
        isset($in['confirmation_code'])?(string)$in['confirmation_code']:null
    );
    json_out(['ok'=>true,'withdrawal'=>$result],201);
} catch(Throwable $e) {
    json_out(['ok'=>false,'error'=>$e->getMessage()],422);
}
