<?php
declare(strict_types=1);
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/contract.php';
set_exception_handler(static function(Throwable $e):void{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);});
$sha=n6Sha();$db=smPdo();$actor=n6Actor($db);$meta=n6Metadata($db);smSave('metadata-private.json',$meta);if(getenv('SM7_LANE')==='B'){smSave('mysql-columns-private.json',$meta);}
if(!is_file(getenv('SM7_RUNTIME').'/mysql-columns-private.json')){throw new RuntimeException('Frozen MySQL type contract missing');}
$all=smRows($db,smTop('SELECT id,type,restaurant_id FROM ydy_equipment WHERE del_flag=0 AND type<>11 ORDER BY id',5));
$weigh=smRows($db,smTop('SELECT id,type,restaurant_id,code,left_dishes_uuid,right_dishes_uuid FROM ydy_equipment WHERE del_flag=0 AND type IN (2,3,4,7,10) ORDER BY id',5));$weighWithDish=null;
foreach($weigh as $row){$uuids=array_values(array_unique(array_filter([$row['left_dishes_uuid'],$row['right_dishes_uuid']])));if($uuids){$place=implode(',',array_fill(0,count($uuids),'?'));$dish=smRows($db,'SELECT uuid FROM ydy_dishes WHERE uuid IN ('.$place.')',$uuids);if($dish){$weighWithDish=$row;break;}}}
$versions=smRows($db,smTop('SELECT type,code FROM ydy_equipment_version ORDER BY id',5));
$staffCandidates=smRows($db,smTop('SELECT s.uuid,s.name,sd.department_uuid FROM ydy_staff s JOIN ydy_staff_department sd ON sd.staff_uuid=s.uuid JOIN ydy_department d ON d.uuid=sd.department_uuid WHERE s.is_show=1 AND d.is_show=1 AND d.status=1 AND d.leaf_flag=1 ORDER BY s.id',5));$dining=null;
foreach($staffCandidates as $candidate){if(strpbrk($candidate['name'],'%_')!==false){continue;}$count=smRows($db,'SELECT COUNT(*) n FROM ydy_staff s JOIN ydy_staff_department sd ON sd.staff_uuid=s.uuid WHERE sd.department_uuid=? AND s.is_show=1 AND s.name LIKE ?',[$candidate['department_uuid'],'%'.$candidate['name'].'%']);if((int)$count[0]['n']>20){continue;}$dates=smRows($db,smTop('SELECT menu_date FROM ydy_date_menu WHERE staff_uuid=? ORDER BY id DESC',1),[$candidate['uuid']]);$date=$dates?substr((string)$dates[0]['menu_date'],0,10):'2001-01-10';$menuCount=smRows($db,'SELECT COUNT(*) n FROM ydy_date_menu WHERE staff_uuid=? AND menu_date=?',[$candidate['uuid'],$date]);if((int)$menuCount[0]['n']>100){continue;}$dining=$candidate+['date'=>$date,'visible_source_count'=>(int)$count[0]['n'],'menu_rows'=>(int)$menuCount[0]['n']];break;}
$h7=smLoad('h7-fixture-private.json');$pre=['lane'=>getenv('SM7_LANE'),'source_sha256'=>$sha,'actor_digest'=>smDigest($actor),'equipment_read'=>$all[0]??null,'weigh'=>$weigh[0]??null,'weigh_dishes'=>$weighWithDish,'version'=>$versions[0]??null,'dining'=>$dining,'owned_recipe'=>['date'=>$h7['seed']['date'],'meal'=>1,'type'=>1,'restaurant_id'=>$h7['own_rows']['recipes']['restaurant_id'],'recipe_id'=>$h7['ids']['recipes'],'dishes_uuid'=>$h7['seed']['dishes']],'dml'=>0,'ddl'=>0];smSave('preflight-private.json',$pre);
echo json_encode(['lane'=>$pre['lane'],'source_files'=>count($sha),'equipment_read'=>(bool)$all,'weigh_list'=>(bool)$weigh,'weigh_dishes'=>(bool)$weighWithDish,'version'=>(bool)$versions,'staff_dining'=>(bool)$dining,'dml'=>0,'ddl'=>0]),PHP_EOL;
