<?php
declare(strict_types=1);
require dirname(__DIR__).'/lib/HexGrid.php';
require dirname(__DIR__).'/lib/SynhexSim.php';
require dirname(__DIR__).'/lib/SynhexMatch.php';
$checks=0;
function check(bool $ok,string $label): void {global $checks;$checks++;if(!$ok)throw new RuntimeException($label);}

$cfg=SynhexMatch::config();
check($cfg['grid_cols']===17 && $cfg['grid_rows']===14,'Documented 17×14 odd-r board');
check($cfg['walk_range']===5 && $cfg['act_duration_ms']===3000,'Walk 5 hexes / 3s act');
check($cfg['base_accuracy']===15,'Base accuracy 15%');
check($cfg['weapon_shotgun_pellets']>=2,'Shotgun fires multiple pellets');
check($cfg['vision_cone_half_deg']===60,'120° cone (half-angle 60)');
check(count($cfg['spawn']['w'])===4 && count($cfg['spawn']['b'])===4,'Four units per side');
check($cfg['spawn']['w'][0]['role']==='vip' && $cfg['spawn']['w'][0]['weapon']==='pistol','VIP pistol');
$weapons=array_column($cfg['spawn']['w'],'weapon');
check($weapons===['pistol','smg','smg','shotgun'],'VIP + 2 SMG + shotgun');

check(HexGrid::distance(0,0,1,0)===1,'East neighbor is 1');
check(HexGrid::distance(0,0,-1,0)===1,'West neighbor is 1');
[$q,$r]=HexGrid::axial(3,4);[$c,$row]=HexGrid::offset($q,$r);
check($c===3 && $row===4,'Offset/axial round-trip');
check(HexGrid::inCone(0,0,0,2,0,60),'Forward hex is in cone');
check(!HexGrid::inCone(0,0,0,-2,0,60),'Rear hex is outside cone');
$walls=[HexGrid::key(1,0)=>true];
check(!HexGrid::los(0,0,2,0,$walls),'Wall blocks LoS');
check(HexGrid::los(0,0,0,1,[]),'Open LoS');
$walk=HexGrid::allHexes(8,6);
unset($walk[HexGrid::key(...HexGrid::axial(3,2))]);
[$q1,$r1]=HexGrid::axial(0,2);[$q2,$r2]=HexGrid::axial(6,2);
$path=HexGrid::path($q1,$r1,$q2,$r2,$walk);
check($path!==null && count($path)>2,'A* walks around a wall');

$s=SynhexMatch::initial(24,$cfg);
check(count($s['units'])===8,'Eight operatives');
check($s['grid']['cols']===17 && $s['phase']==='planning','Planning on configured grid');
check(count(array_filter($s['units'],fn($u)=>$u['role']==='vip'))===2,'Two VIPs');
check(SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Both plan at once');
foreach ($s['walls'] as $key) check(is_string($key) && str_contains($key,','),'Walls stored as q,r keys');

$s=SynhexMatch::commit($s,'w',['units'=>[]],$cfg);
check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'First lock waits for opponent');
$s=SynhexMatch::commit($s,'b',['units'=>[]],$cfg);
check(($s['phase']??'')==='playback','Both locks run the act');
check((int)$s['timeline']['durationMs']===3000,'Playback lasts one act');
check(isset($s['timeline']['snapshots'][0]['units']),'Timeline has snapshots');
$again=SynhexMatch::initial(24,$cfg);
$again=SynhexMatch::commit($again,'w',['units'=>[]],$cfg);
$again=SynhexMatch::commit($again,'b',['units'=>[]],$cfg);
check($again['timeline']['shots']===$s['timeline']['shots'],'Same seed is deterministic');

$s=SynhexMatch::ackPlayback($s,'w');
check(!SynhexMatch::isYourTurn($s,'w') && SynhexMatch::isYourTurn($s,'b'),'Playback ack waits for opponent');
$s=SynhexMatch::ackPlayback($s,'b');
check($s['phase']==='planning' || $s['phase']==='ended','After both Play, next plan or ended');

$tiny=$cfg;
$tiny['grid_cols']=7;$tiny['grid_rows']=5;$tiny['wall_density']=0;$tiny['wall_gen_attempts']=1;
$tiny['spawn']=['w'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>1,'row'=>2,'facing'=>0]],'b'=>[['id'=>'vip','role'=>'vip','weapon'=>'pistol','col'=>5,'row'=>2,'facing'=>3]]];
$tiny['base_accuracy']=100;$tiny['accuracy_cap']=100;$tiny['shot_range']=10;$tiny['vision_range']=10;
$duel=SynhexMatch::initial(7,$tiny);
$duel=SynhexMatch::commit($duel,'w',['units'=>[]],$tiny);
$duel=SynhexMatch::commit($duel,'b',['units'=>[]],$tiny);
check($duel['timeline']['shots']!==[],'Facing VIPs fire when accuracy is certain');
check($duel['result']!==null,'VIP death produces a result after the act');

$shotCfg=$cfg;$shotCfg['weapon_shotgun_pellets']=5;
$st=SynhexMatch::initial(3,$shotCfg);
$shot=null;
foreach ($st['units'] as $u) if ($u['weapon']==='shotgun') $shot=$u;
check($shot!==null,'Shotgun unit exists');
$alive=array_values(array_filter($st['units'],fn($u)=>$u['team']!==$shot['team']));
$target=$alive[0];
$aims=new ReflectionClass(SynhexSim::class);
$m=$aims->getMethod('pelletAims');
$m->setAccessible(true);
$points=$m->invoke(null,$shot,$target,5,(int)$shotCfg['weapon_shotgun_spread']);
check(count($points)===5,'Shotgun volley has five rays');

$bad=SynhexMatch::initial(1,$cfg);
try {SynhexMatch::sanitizePlan($bad,'w',['units'=>['w-vip'=>['path'=>[['q'=>99,'r'=>99]]]]],$cfg);check(false,'Rejects illegal path');}
catch (DomainException) {check(true,'Rejects illegal path');}

$view=SynhexMatch::clientView($st,'w',$cfg);
check(isset($view['grid'],$view['units'],$view['rules']['walkRange']),'Client view exposes board + tunables');
$own=array_filter($view['units'],fn($u)=>$u['team']==='w' && empty($u['hologram']));
check(count($own)===4,'Viewer always sees own units');

echo "Synhex: $checks assertions passed.\n";
