<?php
require __DIR__.'/common.php';
if(is_file(__DIR__.'/private/fixture-complete-journal.json'))throw new RuntimeException('No completion replay');$old=$source();$baseline=json_decode(file_get_contents(__DIR__.'/private/batch-before20.json'),true);unset($baseline['sentinel']);if($old!==$baseline)throw new RuntimeException('Before completion source drift');$logsBefore=$logs();if(count($logsBefore)!==76)throw new RuntimeException('Expected original76');
$meta=$lane==='B'?$q('SELECT COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? ORDER BY ORDINAL_POSITION',['ydy_dishes']):$q('SELECT COLUMN_NAME,DATA_TYPE,DATA_LENGTH,DATA_PRECISION,DATA_SCALE,NULLABLE,DATA_DEFAULT FROM USER_TAB_COLUMNS WHERE TABLE_NAME=? ORDER BY COLUMN_ID',['YDY_DISHES']);
$triggers=$lane==='B'?$q('SELECT TRIGGER_NAME,ACTION_TIMING,EVENT_MANIPULATION,ACTION_STATEMENT FROM information_schema.TRIGGERS WHERE EVENT_OBJECT_SCHEMA=DATABASE() AND EVENT_OBJECT_TABLE=? ORDER BY TRIGGER_NAME',['ydy_dishes']):$q('SELECT * FROM USER_TRIGGERS WHERE TABLE_NAME=? ORDER BY TRIGGER_NAME',['YDY_DISHES']);if($triggers!==[])throw new RuntimeException('Dishes trigger');
$id='C8EAA110-0002-4000-8000-202610110002';if(count($old['dishes'])!==1||(float)$old['dishes'][0]['ssb']!==0.0||(float)$old['dishes'][0]['raw_flag_weight']!==0.0||$old['dishes'][0]['create_by']!=='CDXNR11A')throw new RuntimeException('Exact own old0');
$j=['lane'=>$lane,'authority'=>'Root explicit 2026-10-11 own fixture CAS ssb1 then raw_flag_weight100;2 UPDATE only','committed'=>false,'before'=>$old,'logs_before'=>$logsBefore,'metadata'=>$meta,'triggers'=>$triggers,'events'=>[]];$save('fixture-complete-journal.json',$j);$db->beginTransaction();
try{foreach(['ssb'=>1,'raw_flag_weight'=>'100.00']as $field=>$value){$sql='UPDATE ydy_dishes SET '.$field.'=? WHERE uuid=? AND create_by=? AND '.$field.'=?';$binds=[$value,$id,'CDXNR11A',0];$i=count($j['events']);$j['events'][]=['queryString'=>$sql,'binds'=>$binds,'sent'=>true,'success'=>false];$save('fixture-complete-journal.json',$j);$s=$db->prepare($sql);foreach($binds as $n=>$v)$s->bindValue($n+1,$v,is_int($v)?PDO::PARAM_INT:PDO::PARAM_STR);$s->execute();$j['events'][$i]['success']=true;$j['events'][$i]['rowCount']=$s->rowCount();$j['events'][$i]['errorInfo']=$s->errorInfo();$save('fixture-complete-journal.json',$j);if($s->rowCount()!==1)throw new RuntimeException('CAS exact1');$now=$source();$expected=$old;$expected['dishes'][0][$field]=$now['dishes'][0][$field];if((float)$now['dishes'][0][$field]!== (float)$value||$now!==$expected||$logs()!==$logsBefore)throw new RuntimeException('Completion protection '.$field);$old=$now;$j['events'][$i]['full_readback']=$now['dishes'][0];$save('fixture-complete-journal.json',$j);}$db->commit();$j['committed']=true;$j['after']=$old;$save('fixture-complete-journal.json',$j);$save('batch-before-complete.json',$source(true));$save('fixture-complete-oracle.json',$GLOBALS['NR_ORACLE']);
$request=json_decode(file_get_contents(__DIR__.'/private/request.json'),true);$actions=['Onenutrient','TeamDietaryPattern','StageDietaryPattern','TeamProtein','StageTeamProtein'];foreach($request['cases']as $case){if(in_array(basename($case['path']),$actions,true)){$case['label']=basename($case['path']).'-complete';$case['group']=3;$case['baseline']='complete';$request['cases'][]=$case;}}$save('request.json',$request);$save('completion-cases.json',array_values(array_filter($request['cases'],static function($c){return $c['group']===3;})));echo json_encode(['lane'=>$lane,'UPDATE'=>2,'committed'=>true,'new_cases'=>5,'new_log_budget'=>21]),"\n";
}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$j['error']=['class'=>get_class($e),'message'=>$e->getMessage()];$save('fixture-complete-journal.json',$j);throw $e;}
