<?php
declare(strict_types=1);
require dirname(__DIR__).'/lib/MoveFeedback.php';
$checks=0;
function verify(bool $ok,string $label): void {global $checks;$checks++;if(!$ok)throw new RuntimeException($label);}
function setup(array $pieces,string $turn='w'): array {
    $s=Chess::initial();$s['board']=array_fill(0,20,'.');foreach($pieces as $i=>$p)$s['board'][$i]=$p;
    $s['turn']=$turn;$s['history']=[Chess::key($s)];return $s;
}
$cases=[
    'quiet opening'=>[Chess::initial(),15,11,null,null],
    'free queen'=>[setup([19=>'K',3=>'k',12=>'R',8=>'q']),12,8,null,'good'],
    'poisoned pawn'=>[setup([19=>'K',1=>'k',12=>'Q',8=>'p',0=>'r']),12,8,null,'poor'],
    'equal rook exchange'=>[setup([19=>'K',3=>'k',12=>'R',8=>'r',0=>'r']),12,8,null,null],
    'checkmate'=>[setup([10=>'K',0=>'k',6=>'Q']),6,5,null,'good'],
    'allows mate in one'=>[setup([16=>'K',10=>'k',14=>'q',3=>'R']),3,7,null,'poor'],
    'promotion'=>[setup([16=>'K',3=>'k',5=>'P']),5,1,'q','good'],
    'underpromotion draw'=>[setup([16=>'K',3=>'k',5=>'P']),5,1,'n',null],
];
foreach($cases as $name=>[$s,$from,$to,$promotion,$quality]){
    verify(!Chess::check($s,Chess::opposite($s['turn'])),"Legal fixture: $name");
    $next=Chess::move($s,$from,$to,$promotion);$result=(new MoveFeedback())->rate($s,$next);
    echo "$name: ".json_encode($result)."\n";
    verify($result['quality']===$quality,"$name quality");
    verify($result['elapsed_ms']<500,"$name stays comfortably inside feedback window");
    verify(Chess::afterLegal($s,$next['last'])===$next,"Shared transition: $name");
    // Same tactical verdict with colors swapped and board rotated.
    $mirror=$s;$mirror['board']=array_map(fn($p)=>$p==='.'?'.':(ctype_upper($p)?strtolower($p):strtoupper($p)),array_reverse($s['board']));$mirror['turn']='b';$mirror['history']=[Chess::key($mirror)];
    $next=Chess::move($mirror,19-$from,19-$to,$promotion);
    verify((new MoveFeedback())->rate($mirror,$next)['quality']===$quality,"Black perspective: $name");
}
$s=Chess::initial();$next=Chess::move($s,15,11);
foreach([new MoveFeedback(0),new MoveFeedback(200,1)] as $engine){$r=$engine->rate($s,$next);verify($r['quality']===null&&$r['reason']==='inconclusive','Budget exhaustion is silent, not a false verdict');}
$s=setup([19=>'K',3=>'k',12=>'R',8=>'q']);$s['halfmove']=100;
verify((new MoveFeedback())->rate($s,Chess::move($s,12,8))['quality']===null,'Existing draw claim suppresses move-only grading');
$s=setup([10=>'K',0=>'k',6=>'Q']);$s['halfmove']=149;
verify((new MoveFeedback())->rate($s,Chess::move($s,6,5))['quality']==='good','Mate takes precedence over automatic move-count draw');
$s=setup([10=>'K',0=>'k',6=>'Q']);$next=Chess::move($s,6,7);$next['history']=array_fill(0,5,Chess::key($next));
verify((new MoveFeedback())->rate($s,$next)['reason']==='draw','Fivefold repetition respected');
// Small reproducible sample exercises realistic positions without any exhaustive search.
mt_srand(17);$s=Chess::initial();$worst=0;$total=0;$silent=0;
for($i=0;$i<30;$i++){
    if(Chess::result($s))$s=Chess::initial();$moves=Chess::legal($s);$m=$moves[mt_rand(0,count($moves)-1)];$next=Chess::afterLegal($s,$m);
    $r=(new MoveFeedback())->rate($s,$next);$worst=max($worst,$r['elapsed_ms']);$total+=$r['elapsed_ms'];$silent+=$r['reason']==='inconclusive'?1:0;$s=$next;
    verify(in_array($r['quality'],[null,'good','poor'],true),'Only supported pulse values');
}
echo "$checks feedback checks passed. Sample: mean ".round($total/30,2)." ms, max $worst ms, $silent budget/horizon abstentions / 30 moves.\n";
