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

use Zoo\Database;
use Zoo\Game\ProvablyFair;

try {
    $id=(int)($_GET['round_id']??0);
    $db=Database::connect();
    $q=$db->prepare("SELECT * FROM rounds WHERE id=? AND state='SETTLED'");
    $q->execute([$id]);
    $r=$q->fetch();
    if(!$r) json_out(['ok'=>false,'error'=>'Settled round not found'],404);

    $ok=ProvablyFair::verify(
        $r['server_seed'],$r['public_round_id'],(int)$r['rng_number'],$r['server_seed_hash']
    );
    json_out([
      'ok'=>true,
      'verified'=>$ok,
      'public_round_id'=>$r['public_round_id'],
      'commit_hash'=>$r['server_seed_hash'],
      'revealed_server_seed'=>$r['server_seed'],
      'rng_number'=>(int)$r['rng_number'],
      'winner'=>$r['winner'],
    ]);
} catch(Throwable $e) {
    json_out(['ok'=>false,'error'=>$e->getMessage()],500);
}
