<?php
declare(strict_types=1);
require __DIR__.'/gate.php';
set_error_handler(static function($severity,$message,$file,$line){throw new ErrorException($message,0,$severity,$file,$line);});
$p=lr_load($argv[1]);$seq=lr_sequence($p);$checks=0;$rejects=[];
$ok=static function(bool $x)use(&$checks):void{lr_assert($x,'test failed');$checks++;};
$no=static function(string $label,callable $fn)use(&$rejects):void{try{$fn();}catch(Throwable $e){$rejects[]=$label;return;}throw new RuntimeException('negative accepted '.$label);};
$ok(count($seq)===50);$ok($seq[0]['role']==='identity_schema'&&$seq[1]['role']==='identity_version'&&$seq[2]['kind']==='prepare_only');
foreach($seq as $i=>$s){lr_step_gate($p,$seq,$i,$s,$i);$checks++;}
$case=$seq[3];$mut=$case;$mut['parameters'][0]['runtime_value']='12.35';$no('changed bound value',static function()use($p,$seq,$mut){lr_step_gate($p,$seq,3,$mut,3);});
$mut=$case;$mut['parameters'][0]['pdo_type_code']=1;$no('changed bind type',static function()use($p,$seq,$mut){lr_step_gate($p,$seq,3,$mut,3);});
$mut=$case;$mut['sql']='SELECT * FROM NUTRITION';$no('natural table SQL',static function()use($p,$seq,$mut){lr_step_gate($p,$seq,3,$mut,3);});
$no('retry same step',static function()use($p,$seq,$case){lr_step_gate($p,$seq,3,$case,4);});
$no('beyond budget',static function()use($p,$seq,$case){lr_step_gate($p,$seq,50,$case,50);});
$no('skip identity',static function()use($p,$seq,$case){lr_step_gate($p,$seq,0,$case,0);});
$row=array_fill_keys(['x','whole','scaled','lower_i','rounded_i','quantized','cap','clipped','stored_real','stored_double'],0.0);
$row+=['input_is_null'=>0,'half_side'=>0,'was_clipped'=>0];
lr_result_gate($case,[$row],['supported'=>true,'columns'=>array_fill(0,13,[])],100);$checks++;
$no('extra result row',static function()use($case,$row){lr_result_gate($case,[$row,$row],['supported'=>true,'columns'=>array_fill(0,13,[])],100);});
$mut=$row;$mut['stored_double']='0';$no('native numeric string',static function()use($case,$mut){lr_result_gate($case,[$mut],['supported'=>true,'columns'=>array_fill(0,13,[])],100);});
$mut=$row;unset($mut['x']);$no('missing column',static function()use($case,$mut){lr_result_gate($case,[$mut],['supported'=>true,'columns'=>array_fill(0,13,[])],100);});
$no('bytes overflow',static function()use($case,$row){lr_result_gate($case,[$row],['supported'=>true,'columns'=>array_fill(0,13,[])],65537);});
$no('unknown metadata',static function()use($case,$row){lr_result_gate($case,[$row],['supported'=>false],100);});
$nullStep=null;foreach($seq as $s)if($s['kind']==='select'&&$s['role']==='literal_rint'&&$s['parameters'][0]['original_value']===null)$nullStep=$s;
lr_assert($nullStep!==null,'NULL original present');
$nr=array_fill_keys(['x','whole','scaled','lower_i','rounded_i','quantized','clipped','stored_real','stored_double','half_side','was_clipped'],null);$nr['cap']=999999.99;$nr['input_is_null']=1;
lr_result_gate($nullStep,[$nr],['supported'=>true,'columns'=>array_fill(0,13,[])],100);$checks++;
$mut=$nr;$mut['stored_double']=0.0;$no('NULL changed to zero',static function()use($nullStep,$mut){lr_result_gate($nullStep,[$mut],['supported'=>true,'columns'=>array_fill(0,13,[])],100);});
$mut=$row;$mut['stored_double']=INF;$no('nonfinite result',static function()use($case,$mut){lr_result_gate($case,[$mut],['supported'=>true,'columns'=>array_fill(0,13,[])],100);});
$no('schema wrong',static function(){lr_result_gate(['role'=>'identity_schema'],[['schema_name'=>'AIZHCT_APP']],['supported'=>true,'columns'=>[[]]],100);});
echo json_encode(['status'=>'PURE_GREEN','checks'=>$checks,'negative_rejections'=>$rejects],JSON_PRETTY_PRINT),"\n";
