<?php
require __DIR__.'/gate.php';
$uuid='A1B2C3D4-1111-2222-3333-123456789ABC';
$ctx=['case'=>6,'lane'=>'C','table'=>'nutrition_time','op'=>'UPDATE','fields'=>['status'=>'0'],'uuid'=>$uuid,'creator_sha'=>hash('sha256','actor'),'nonce'=>'known','expected_nonce'=>'known'];
$sql='UPDATE "YDY_NUTRITION_TIME" SET "STATUS"=:data__status WHERE "UUID" = :where_AND_uuid';
$bind=[':data__status'=>['value'=>'0','type'=>2],':where_AND_uuid'=>['value'=>$uuid,'type'=>2]];
$count=0;$assert=function($ok,$name)use(&$count){++$count;if(!$ok)throw new RuntimeException($name);};
$oracle=function($table,$id,$insert){return $insert?0:1;};
if(!function_exists('cdxWriteGate')){echo "RED: missing strict native gate\n";exit(2);}
$assert(cdxWriteGate($sql,$bind,null,$ctx,$oracle)==='UPDATE','approved status update');
$mutations=[
 ['natural UUID', $sql, array_replace($bind,[':where_AND_uuid'=>['value'=>'NATURAL','type'=>2]]),$ctx],
 ['wrong table',str_replace('NUTRITION_TIME','USER',$sql),$bind,$ctx],
 ['extra column',str_replace('SET "STATUS"=:data__status','SET "STATUS"=:data__status,"NAME"=:data__name',$sql),$bind+[':data__name'=>['value'=>'x','type'=>2]],$ctx],
 ['no uuid WHERE',str_replace('WHERE "UUID" = :where_AND_uuid','WHERE 1=1',$sql),$bind,$ctx],
 ['unknown nonce',$sql,$bind,array_replace($ctx,['nonce'=>'wrong'])],
 ['unknown action',$sql,$bind,array_replace($ctx,['op'=>'DELETE'])],
 ['multi UUID',str_replace('= :where_AND_uuid','IN (:where_AND_uuid,:other)',$sql),$bind+[':other'=>['value'=>$uuid,'type'=>2]],$ctx],
 ['physical delete','DELETE FROM YDY_NUTRITION_TIME WHERE UUID=:where_AND_uuid',$bind,$ctx],
 ['DDL','ALTER TABLE YDY_NUTRITION_TIME ADD X INT',[],$ctx],
 ['two statements',$sql.';'.$sql,$bind,$ctx],
 ['wrong status',$sql,array_replace($bind,[':data__status'=>['value'=>'1','type'=>2]]),$ctx],
 ['unbound SQL literal',str_replace(':data__status','0',$sql),$bind,$ctx],
 ['execute array',$sql,$bind, $ctx,[]],
 ['unknown bind',$sql,$bind+[':other'=>['value'=>'x','type'=>2]],$ctx],
 ['unknown case',$sql,$bind,array_replace($ctx,['case'=>99])],
 ['missing approved atomic predicate',$sql,$bind,array_replace($ctx,['require_atomic'=>true])],
 ];
foreach($mutations as $m){$rejected=false;try{cdxWriteGate($m[1],$m[2],$m[4]??null,$m[3],$oracle);}catch(Throwable $e){$rejected=true;}$assert($rejected,$m[0]);}
$assert(cdxWriteGate($sql.' AND "STATUS" <> :where_AND_status',$bind+[':where_AND_status'=>['value'=>'0','type'=>2]],null,$ctx,$oracle)==='UPDATE','canonical C atomic no-op guard');
$rejected=false;try{cdxWriteGate($sql,$bind,null,$ctx,function(){return 2;});}catch(Throwable $e){$rejected=true;}$assert($rejected,'exact one owned row');
$insertCtx=['case'=>1,'lane'=>'B','table'=>'nutrition_time','op'=>'INSERT','fields'=>['name'=>'CDXNW11D6A2-time-A','order_no'=>'620001','function'=>'CDXNW11D6A2-function-A'],'creator_sha'=>hash('sha256','actor'),'nonce'=>'known','expected_nonce'=>'known'];
$insert='INSERT INTO `ydy_nutrition_time` (`name`,`order_no`,`function`,`create_by`,`uuid`,`create_time`) VALUES (:data__name,:data__order_no,:data__function,:data__create_by,:data__uuid,:data__create_time)';
$vals=$insertCtx['fields']+['create_by'=>'actor','uuid'=>$uuid,'create_time'=>date('Y-m-d H:i:s')];$ib=[];foreach($vals as $k=>$v)$ib[':data__'.$k]=['value'=>$v,'type'=>2];
$assert(cdxWriteGate($insert,$ib,null,$insertCtx,$oracle)==='INSERT','business generated insert');
foreach(['id','number','status','del_flag'] as $column){$rejected=false;try{cdxWriteGate(str_replace('`create_by`','`'.$column.'`',$insert),$ib,null,$insertCtx,$oracle);}catch(Throwable $e){$rejected=true;}$assert($rejected,'reject insert '.$column);}
echo json_encode(['passed'=>true,'assertions'=>$count,'HTTP'=>0,'DML'=>0,'DDL'=>0]),"\n";
