<?php
require __DIR__.'/NativeGate.php';NFGate::$dry=true;$lane=getenv('NF_LANE');NFGate::$lane=$lane;$plan=json_decode(file_get_contents(__DIR__.'/execution-plan.json'),true);$base=json_decode(file_get_contents(__DIR__.'/private/baseline-original-'.$lane.'.json'),true)['source'];$rows=array_values(array_filter($base['nutrition'],static function($r){return $r['name']==='CDXNF11F5-candidate-product';}));$row=$rows[0];$positive=0;$negative=0;
$reject=static function(callable $run)use(&$negative){try{$run();}catch(RuntimeException $e){$negative++;return;}throw new RuntimeException('Negative accepted');};
foreach($plan['cases']as $spec){$params=['uuid'=>$row['uuid']];foreach(PlainScalarNativeContract::FIELDS as $f)if($f!=='is_own')$params[$f]=$f==='use_weight'?$spec['value']:$row[$f];$case=['path'=>'/m/nutrition/update','params'=>$params,'spec'=>$spec];NFGate::$case=$case;NFGate::$dml=0;
if($spec['target_DML_attempt_max']===0){$reject(static function()use($lane,$spec){PlainScalarNativeContract::expectedSql($lane,$spec);});continue;}
$sql=PlainScalarNativeContract::expectedSql($lane,$spec);$binds=[];foreach(PlainScalarNativeContract::FIELDS as $f){if($lane==='C'&&$f==='use_weight'&&$spec['C_weight_shape']==='generated-REAL')continue;$v=$f==='is_own'?1:$params[$f];$binds[':data__'.$f]=['value'=>$v,'type'=>in_array($f,['use_weight','use_number','employ','is_nutrition_station','is_own'],true)?1:2,'php_type'=>gettype($v),'ok'=>true];}$binds[':where_AND_uuid']=['value'=>$params['uuid'],'type'=>2,'php_type'=>'string','ok'=>true];NFGate::binds($sql,$binds);$positive++;
foreach(['sql','uuid','type','value','extra','phpType','second','info','spec','params']as $mut){$s=$sql;$b=$binds;$c=$case;$ep=null;NFGate::$dml=0;switch($mut){case 'sql':$s.=' ';break;case 'uuid':$b[':where_AND_uuid']['value']='wrong';break;case 'type':$b[':data__use_number']['type']=2;break;case 'value':$b[':data__name']['value']='wrong';break;case 'extra':$b[':data__power']=['value'=>1.0,'type'=>1,'php_type'=>'double','ok'=>true];break;case 'phpType':$b[':data__use_number']['php_type']='double';break;case 'second':NFGate::$dml=1;break;case 'info':$c['path']='/m/nutrition/info';break;case 'spec':$c['spec']['value']='12.346';break;case 'params':$ep=[];break;}NFGate::$case=$c;$reject(static function()use($s,$b,$ep){NFGate::binds($s,$b,$ep);});}NFGate::$case=$case;}
NFGate::$warningPending=false;$reject(static function(){NFGate::binds('SHOW WARNINGS',[]);});if($lane==='B'){NFGate::$warningPending=true;NFGate::$warningReads=0;NFGate::binds('SHOW WARNINGS',[]);$positive++;NFGate::$warningReads=1;$reject(static function(){NFGate::binds('SHOW WARNINGS',[]);});}
$r=['lane'=>$lane,'positive'=>$positive,'negative'=>$negative,'SQL'=>0,'HTTP'=>0,'DML'=>0,'PDO_initialization'=>0];$f=__DIR__.'/private/gate-results.json';file_put_contents($f,json_encode($r));chmod($f,0600);echo json_encode($r),PHP_EOL;
