<?php
declare(strict_types=1);
require __DIR__.'/gate.php';
$p=nh_load(__DIR__.'/plan.json');foreach(['B','C']as $lane)nh_shape($p,$lane);
$bcols=[];$ccols=[];foreach(['ID','MARKER','MD','RAW32']as $i=>$n)$bcols[]=['column_name'=>strtolower($n),'column_type'=>['int(11)','varchar(16)','float(8,2)','float'][$i],'is_nullable'=>$i<2?'NO':'YES','column_default'=>null,'extra'=>''];
foreach(['ID','MARKER','MD_REAL','RAW64']as $i=>$n)$ccols[]=['column_name'=>$n,'data_type'=>['INT','VARCHAR','REAL','FLOAT'][$i],'data_length'=>[4.0,16.0,4.0,8.0][$i],'data_precision'=>$i<2?null:($i===2?24.0:53.0),'data_scale'=>0.0,'nullable'=>$i<2?'N':'Y','data_default'=>$i<2?null:'NULL'];
$b=[$bcols,[['index_name'=>'PRIMARY','non_unique'=>0,'column_name'=>'id']],[['constraint_type'=>'PRIMARY KEY']],[['column_name'=>'id','referenced_table_name'=>null]],[],[['create table'=>'CREATE TABLE "cdx_nfh_261011_e2f7" ("MARKER" VARCHAR(16))']]];
$c=[$ccols,[['index_name'=>'INDEX90001','index_type'=>'CLUSTER','uniqueness'=>'NONUNIQUE'],['index_name'=>'INDEX90002','index_type'=>'NORMAL','uniqueness'=>'UNIQUE']],[['index_name'=>'INDEX90002','column_name'=>'ID','column_position'=>1.0]],[['constraint_name'=>'CONS90003','constraint_type'=>'P','status'=>'ENABLED']],[['constraint_name'=>'CONS90003','column_name'=>'ID','position'=>1.0]],[],[['ddl'=>'CREATE TABLE "ZHCT_APP"."CDX_NFH_261011_E2F7" ("MARKER" VARCHAR(16))']]];
nh_meta_gate($b,'B');nh_meta_gate($c,'C');$checks=4;$rejected=[];
$negative=static function(callable $fn,string $name)use(&$checks,&$rejected){$checks++;try{$fn();}catch(Throwable $e){$rejected[]=$name;return;}throw new RuntimeException('Mutation survived '.$name);};
foreach([
 'round original neighboring string'=>static function()use($p){$x=$p;$x['lanes']['B']['point_cases'][8]['DML']['bindings'][2]['original_value']='0.005';nh_shape($x,'B');},
 'PDO type mismatch'=>static function()use($p){$x=$p;$x['lanes']['C']['point_cases'][0]['DML']['bindings'][2]['PDO_type_code']=1;nh_shape($x,'C');},
 'business table insert'=>static function()use($p){$x=$p;$x['lanes']['B']['point_cases'][0]['DML']['sql']='INSERT INTO ydy_nutrition VALUES(?,?,?,?)';nh_shape($x,'B');},
 'unexpected third row'=>static function()use($p){$x=$p;$x['lanes']['B']['point_cases'][0]['DML']['bindings'][0]['original_value']=3;nh_shape($x,'B');},
 'case omitted'=>static function()use($p){$x=$p;array_pop($x['lanes']['B']['point_cases']);nh_shape($x,'B');},
 'aggregate pair changed'=>static function()use($p){$x=$p;$x['lanes']['C']['aggregate_cases'][0]['DML'][1]['bindings'][2]['original_value']='12.34';nh_shape($x,'C');},
 'metadata REAL replaced by FLOAT53'=>static function()use($c){$x=$c;$x[0][2]['data_type']='FLOAT';$x[0][2]['data_precision']=53.0;nh_meta_gate($x,'C');},
 'quoted string default NULL'=>static function()use($c){$x=$c;$x[0][2]['data_default']="'NULL'";nh_meta_gate($x,'C');},
 'other ID NULL default string'=>static function()use($c){$x=$c;$x[0][0]['data_default']='NULL';nh_meta_gate($x,'C');},
 'C type nullable changed'=>static function()use($c){$x=$c;$x[0][2]['nullable']='N';nh_meta_gate($x,'C');},
 'extra system index'=>static function()use($c){$x=$c;$x[1][]=$x[1][0];nh_meta_gate($x,'C');},
 'cluster removed'=>static function()use($c){$x=$c;array_shift($x[1]);nh_meta_gate($x,'C');},
 'wrong cluster role'=>static function()use($c){$x=$c;$x[1][0]['uniqueness']='UNIQUE';nh_meta_gate($x,'C');},
 'wrong normal index column'=>static function()use($c){$x=$c;$x[2][0]['column_name']='MD_REAL';nh_meta_gate($x,'C');},
 'unexpected trigger'=>static function()use($c){$x=$c;$x[5][]=['trigger_name'=>'ALIEN'];nh_meta_gate($x,'C');},
 'unexpected FK'=>static function()use($b){$x=$b;$x[3][]=['column_name'=>'md','referenced_table_name'=>'ydy_nutrition'];nh_meta_gate($x,'B');},
 'old FLOAT table reused'=>static function()use($b){$x=$b;$x[5][0]['create table']=str_replace('cdx_nfh_261011_e2f7','cdx_nf5_261011_d9a4',$x[5][0]['create table']);nh_meta_gate($x,'B');},
 'unknown rowcount cast0'=>static function(){nh_num('x',0,'Exact zero');},
]as $name=>$fn)$negative($fn,$name);
echo json_encode(['status'=>'PURE_GATE_PASS','checks'=>$checks,'negative_gates_rejected'=>$rejected,'new_DB_connections'=>0,'new_SQL'=>0],JSON_PRETTY_PRINT),"\n";
