<?php
require __DIR__.'/_bootstrap.php';
use Zoo\Database;
use Zoo\Admin\AdminApiAuth;
use Zoo\Withdrawal\RaastAgentService;
try{
    AdminApiAuth::requireKey();
    $db=Database::connect();$svc=new RaastAgentService($db);
    if($_SERVER['REQUEST_METHOD']==='POST'){
        $in=input_json();
        $action=(string)($in['action']??'create');
        if($action==='create'){
            $agent=$svc->createAgent(
                (string)($in['display_name']??''),
                (string)($in['supported_channels']??'JazzCash, Easypaisa, Raast'),
                (int)($in['max_active_requests']??10),
                (float)($in['daily_limit']??0)
            );
            json_out(['ok'=>true,'agent'=>$agent],201);
        }
        if($action==='status'){
            $svc->setStatus((int)($in['agent_id']??0),(string)($in['status']??''));
            json_out(['ok'=>true]);
        }
        if($action==='reset_secret'){
            $secret=$svc->resetSecret((int)($in['agent_id']??0));
            json_out(['ok'=>true,'agent'=>$secret]);
        }
        throw new RuntimeException('Invalid action');
    }
    json_out(['ok'=>true,'agents'=>$svc->listAgents()]);
}catch(Throwable $e){json_out(['ok'=>false,'error'=>$e->getMessage()],422);}
