<?php
require __DIR__.'/_bootstrap.php';

use Zoo\Database;
use Zoo\Withdrawal\RaastAgentService;

try{
    $in=input_json();
    $db=Database::connect();
    $svc=new RaastAgentService($db);

    $token=(string)($_SERVER['HTTP_X_RAAST_AGENT_TOKEN']??'');
    $agent=$svc->authenticate($token);

    $result=$svc->generateConfirmationCode(
        (int)$agent['id'],
        (int)($in['ttl_minutes']??30)
    );

    json_out(['ok'=>true,'code'=>$result],201);
}catch(Throwable $e){
    json_out(['ok'=>false,'error'=>$e->getMessage()],422);
}
