<?php
declare(strict_types=1);
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/contract.php';
try{$db=smPdo();$lane=getenv('SM7_LANE');$root=getenv('SM7_ROOT');$seed=json_decode(file_get_contents(__DIR__.'/FIXTURE_SEED.json'),true);$meta=hcMetadata($db);$baseline=json_decode(file_get_contents(__DIR__.'/baseline-meta-private.json'),true)['tables'];if(hcMetaHash($meta)!==hcMetaHash($baseline)){throw new RuntimeException('Approved table metadata changed');}
foreach($meta as $v){if($v['foreign_keys']||$v['triggers']){throw new RuntimeException('FK/trigger boundary');}}
$source=['application/p/controller/DateMenu.php'=>'e8c33254370994003a183c911dc343b319e8dc8624ca98a8acd757c04deb9710','application/p/logic/DateMenu.php'=>'4749b69e8e4de3cfd0c05eb4be1a9cf182a7a22182d7c020a9cd48c60f7e44e3'];foreach($source as $f=>$h){if(hash_file('sha256',$root.'/'.$f)!==$h){throw new RuntimeException('Frozen source drift');}}
foreach(file(__DIR__.'/SOURCE_SHA256.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $line){[$h,$f]=preg_split('/\s+/',$line,2);if(hash_file('sha256',$root.'/'.$f)!==$h){throw new RuntimeException('Dependency source drift '.$f);}$source[$f]=$h;}
$actor=smLoad('actor-private.json');$own=smRows($db,'SELECT * FROM ydy_user WHERE id=? AND uuid=? AND account=? AND create_by=?',[$actor['id'],$actor['uuid'],$actor['marker'],$actor['marker']]);if(count($own)!==1||smDigest($own)!==smDigest([$actor['source']])||(int)$own[0]['type']!==0||(int)$own[0]['status']!==0){throw new RuntimeException('Disabled actor ownership drift');}
$rid=$lane==='B'?77:12;$rmarker=$lane==='B'?'CDXCENTB3C677BA4':'CDXCENTC789071DA';$restaurants=smRows($db,'SELECT * FROM ydy_restaurants WHERE id=? AND name=?',[$rid,$rmarker]);if(count($restaurants)!==1||!preg_match('/^[A-F0-9-]{36}$/Di',$restaurants[0]['uuid'])){throw new RuntimeException('Existing owned restaurant identity');}
foreach(HC_TABLES as $t){if($t==='staff_department'){$collide=smRows($db,'SELECT id FROM ydy_staff_department WHERE department_uuid=? OR staff_uuid=?',[$seed['department'],$seed['staff']]);}else{$k=['date_menu_hardware_img'=>'image','initial_menu'=>'menu'][$t]??$t;$collide=smRows($db,'SELECT id FROM ydy_'.$t.' WHERE uuid=?',[$seed[$k]]);}if($collide){throw new RuntimeException('Fresh fixture collision');}}
$rows=hcRowsForInsert($seed,$rid,date('Y-m-d H:i:s'));foreach($rows as $t=>$row){foreach($meta[$t]['columns'] as $c){$n=strtolower($c['column_name']);$nullable=$lane==='B'?$c['is_nullable']==='YES':$c['nullable']==='Y';$def=$lane==='B'?$c['column_default']:$c['data_default'];if(!$nullable&&$def===null&&$n!=='id'&&!array_key_exists($n,$row)){throw new RuntimeException('Unapproved required field '.$t.'.'.$n);}}}
$before=hcSnapshot($db,$seed);smSave('preflight-private.json',['lane'=>$lane,'seed'=>$seed,'rows'=>$rows,'meta_hash'=>hcMetaHash($meta),'source_sha256'=>$source,'before'=>$before,'actor_digest'=>smDigest($own),'restaurant_digest'=>smDigest($restaurants),'restaurant_id'=>$rid,'restaurant_uuid'=>$restaurants[0]['uuid'],'restaurant_marker'=>$rmarker]);
echo json_encode(['status'=>'pass','lane'=>$lane,'tables'=>7,'rows_to_insert'=>7,'metadata_exact'=>true,'actor_unchanged'=>true,'owned_restaurant_verified'=>true,'collision_rows'=>0,'extra_required_fields'=>0,'database_writes'=>0]),PHP_EOL;
}catch(Throwable $e){smSave('preflight-error-private.json',['class'=>get_class($e),'message'=>$e->getMessage()]);echo json_encode(['status'=>'fail','class'=>get_class($e),'message_sha256'=>hash('sha256',$e->getMessage())]),PHP_EOL;exit(1);}
