<?php
declare(strict_types=1);require __DIR__.'/connect.php';$check=static function($ok,$why){if(!$ok)throw new RuntimeException($why);};$template=json_decode(file_get_contents(__DIR__.'/fixture-values-template.json'),true);$out=['lane'=>$lane,'HTTP'=>0,'DML'=>0,'DDL'=>0,'template_sha256'=>hash_file('sha256',__DIR__.'/fixture-values-template.json'),'tables'=>[],'isolated_equipment_tenant'=>[]];foreach(['store','store_user','staff','staff_face'] as $t){$n=(int)$q('SELECT COUNT(*) n FROM yoshop_'.$t.' WHERE store_id=2147001113')[0]['n'];$check($n===0,'Equipment-only virtual tenant no identity/Store/Staff/Face '.$t);$out['isolated_equipment_tenant'][$t]=$n;}$check((int)$q('SELECT COUNT(*) n FROM yoshop_equipment WHERE type=60')[0]['n']===0,'No existing type60 equipment at all');$actor=$q('SELECT store_user_id,store_id,user_name,is_super,is_delete FROM yoshop_store_user WHERE store_user_id=? AND store_id=2147001112',[$lane==='B'?10075:10011]);$check(count($actor)===1&&$actor[0]['user_name']==='cdxop11f64c_terminal'&&(int)$actor[0]['is_delete']===0,'Existing marked non-login operator actor');$out['existing_operator_verified']=['id'=>(int)$actor[0]['store_user_id'],'store_id'=>2147001112];
foreach(['equipment','dishes','dishes_img'] as $t){$upper='YOSHOP_'.strtoupper($t);$cols=$dm?$q('SELECT COLUMN_NAME,DATA_TYPE,NULLABLE,DATA_DEFAULT,DATA_LENGTH,CHAR_LENGTH,CHAR_USED,DATA_PRECISION,DATA_SCALE FROM USER_TAB_COLUMNS WHERE TABLE_NAME=? ORDER BY COLUMN_ID',[$upper]):$q('SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,EXTRA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? ORDER BY ORDINAL_POSITION',['yoshop_'.$t]);$values=$template[$t];foreach($values as &$v){if($v==='@same_library_terminal_file_24452_or_9715')$v=$lane==='B'?24452:9715;elseif($v==='@same_library_terminal_nonlogin_operator_10075_or_10011')$v=$lane==='B'?10075:10011;}unset($v);$fields=[];$missing=[];foreach($cols as $c){$key=strtolower($c['column_name']);if($key==='id')continue;$check(array_key_exists($key,$values)&&$values[$key]!==null,'Explicit all nonPK nonnull '.$t.'.'.$key);$v=$values[$key];$type=strtolower($c['data_type']);$f=['value'=>$v,'actual_metadata'=>$c,'passed'=>true];if(strpos($type,'char')!==false){$chars=mb_strlen($v,'UTF-8');$bytes=strlen($v);$max=(int)($dm?$c['char_length']:$c['character_maximum_length']);$check($chars<=$max&&(!$dm||$bytes<=(int)$c['data_length']),'Exact character and byte capacity '.$t.'.'.$key);$f['actual_value_chars']=$chars;$f['actual_value_bytes']=$bytes;}elseif($type==='datetime'){$check(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/D',$v)===1,'Explicit datetime required '.$key);}elseif(strpos($type,'decimal')!==false){$p=(int)($dm?$c['data_precision']:$c['numeric_precision']);$s=(int)($dm?$c['data_scale']:$c['numeric_scale']);$check($p===10&&$s===2&&$v==='0.00','Explicit DEC10,2 all-zero synthetic nutritional fixture '.$key);}elseif($v==='@new_same_library_dishes_generated_PK'){$check($t==='dishes_img'&&$key==='dishes_id','Only known deferred generated parent PK');$f['deferred_PK_must_be_positive_DB_generated_same_library']=true;}else{$check(is_int($v)&&$v>=0&&$v<=2147001113,'Explicit source integer valid domain '.$key);}$fields[$key]=$f;}foreach($values as $key=>$v)if(!array_key_exists($key,$fields))$missing[]=$key;$check($missing===[]||($lane==='C'&&$t==='equipment'&&$missing===['status']),'Only approved pending STATUS DDL missing allowed at pure preflight');$out['tables'][$t]=['all_actual_nonPK_columns_explicit'=>true,'fields'=>$fields,'pending_approved_DDL_columns'=>$missing];}
file_put_contents(__DIR__.'/fixture-capacity-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));echo json_encode(['lane'=>$lane,'HTTP'=>0,'DML'=>0,'DDL'=>0,'fields_checked'=>array_map(static function($m){return count($m['fields']);},$out['tables']),'pending_approved_STATUS_DDL'=>!empty($out['tables']['equipment']['pending_approved_DDL_columns']),'isolated_equipment_tenant'=>$out['isolated_equipment_tenant']]),"\n";
