<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$check(!is_file($runtime.'/fixture-journal.json'),'No preparation replay');$old=json_decode(file_get_contents('/tmp/cdx-ai-terminal-goods-write4-20261011/private/evidence/final-original.json'),true);$recharge=json_decode(file_get_contents('/tmp/cdx-ai-order-recharge-read-20261011/private/evidence/fixture-journal.json'),true);$before=$own();foreach($old['original'] as $t=>$rows)$check($before[$t]===$rows,'Old sealed twenty tables exact '.$t);foreach(['recharge_order','recharge_order_plan'] as $t)$check($before[$t]===$recharge['after'][$t],'Old own recharge original exact '.$t);$check($before['order_export']===[],'No own export collision');$template=json_decode(file_get_contents(__DIR__.'/fixture-values-template.json'),true);$meta=json_decode(file_get_contents(__DIR__.'/structure-'.$lane.'.json'),true);foreach($meta['tables'] as $t=>$m)$check($m['foreign_keys']===0&&$m['triggers']===0,'FK/trigger zero '.$t);$quote=static function($k)use($dm){return $dm?'"'.strtoupper($k).'"':'`'.$k.'`';};$plans=[];
foreach(['order'=>'order_id','order_goods'=>'order_goods_id','order_export'=>'id'] as $t=>$pk){$row=$template[$t];$keys=array_keys($row);$full=$keys;$full[]=$pk;sort($full);$all=array_column($meta['tables'][$t]['columns'],'column_name');sort($all);$check($full===$all,'Every nonPK column explicit '.$t);foreach($meta['tables'][$t]['columns'] as $c){if($c['column_name']===$pk){if($dm){$ddl=$read("SELECT DBMS_METADATA.GET_DDL('TABLE',?,'AIZHCT_APP') ddl FROM DUAL",[strtoupper('yoshop_'.$t)])[0]['ddl'];$check(preg_match('/"'.strtoupper($pk).'"[^,\n]*(?:IDENTITY\s*\(|AUTO_INCREMENT\b)/i',$ddl)===1,'Actual generated target PK');}else $check($c['extra']==='auto_increment','Actual generated source PK');}elseif($c['data_type']==='decimal')$check($row[$c['column_name']]==='0.00','No financial nonzero fixture');}$plans[$t]='INSERT INTO yoshop_'.$t.' ('.implode(',',array_map($quote,$keys)).') VALUES ('.implode(',',array_fill(0,count($keys),'?')).')';$db->prepare($plans[$t]);}
foreach($tenants as $module=>$tenant){$check((int)$read('SELECT COUNT(*) n FROM yoshop_order WHERE order_no=?',[$marker.'_'.$module])[0]['n']===0,'New exact order marker collision');$check((int)$read('SELECT COUNT(*) n FROM yoshop_order_export WHERE file_path=?',['/uploads/synthetic-no-file/'.$marker.'_'.$module.'.xlsx'])[0]['n']===0,'New export path collision');$id=$recharge['owned'][$module];foreach(['user'=>'user_id','goods'=>'goods_id','upload_file'=>'file_id'] as $t=>$pk){$rows=array_values(array_filter($before[$t],static function($r)use($tenant,$pk,$id,$t){return (int)$r['store_id']===$tenant&&(int)$r[$pk]===$id[$t];}));$check(count($rows)===1,'Exact same-tenant old owner '.$t);}$u=array_values(array_filter($before['user'],static function($r)use($id){return (int)$r['user_id']===$id['user'];}))[0];$check((int)$u['is_delete']===1,'No active user manipulation');}
$checkRow=static function($table,$row,$actual=null)use($meta,$check){foreach($row as $key=>$value){$fields=array_values(array_filter($meta['tables'][$table]['columns'],static function($c)use($key){return $c['column_name']===$key;}));$check(count($fields)===1,'One actual metadata column');$c=$fields[0];$type=strtolower($c['data_type']);$check($value!==null,'Explicit required field');$max=$c['character_maximum_length']??$c['data_length']??null;if(is_string($value)&&$max!==null&&(strpos($type,'char')!==false||strpos($type,'text')!==false))$check(mb_strlen($value,'UTF-8')<=(int)$max,'Resolved actual max length '.$table.'.'.$key);if($actual!==null){if(strpos($type,'int')!==false)$check((int)$actual[$key]===(int)$value,'Integer roundtrip '.$key);elseif(in_array($type,['decimal','number','double','float','double precision'],true))$check(bccomp((string)$actual[$key],(string)$value,8)===0,'Numeric exact roundtrip '.$key);else $check($actual[$key]===$value,'Original field bytes roundtrip '.$key);}}};
foreach(['order','order_goods','order_export'] as $t)$checkRow($t,$template[$t]);
if(defined('DASHBOARD_PREFLIGHT_ONLY')){echo json_encode(['lane'=>$lane,'pure_preflight'=>true,'old_23_tables_exact'=>true,'plans'=>6,'allcols_required_lengths_generated_pk_fk_trigger_gates'=>true,'business_dml'=>0]),"\n";exit;}
$journal=['lane'=>$lane,'before'=>$before,'before_protect'=>$protect(),'owned'=>$recharge['owned'],'dml'=>[],'committed'=>false,'template_sha256'=>hash_file('sha256',__DIR__.'/fixture-values-template.json')];$persist('fixture-journal.json',$journal);$db->beginTransaction();
try{
 foreach($tenants as $module=>$tenant){$ids=$recharge['owned'][$module];$refs=['@module_tenant'=>$tenant,'@existing_same_tenant_own_user_id'=>$ids['user'],'@existing_same_tenant_own_goods_id'=>$ids['goods'],'@existing_same_tenant_own_local_file_id'=>$ids['upload_file']];
  foreach(['order'=>'order_id','order_goods'=>'order_goods_id','order_export'=>'id'] as $t=>$pk){$row=$template[$t];foreach($row as &$v){if(is_string($v)&&isset($refs[$v]))$v=$refs[$v];elseif(is_string($v)&&strpos($v,'@module')!==false)$v=str_replace('@module',$module,$v);if(is_string($v)&&strpos($v,'@')===0)throw new RuntimeException('Unresolved fixture reference');}unset($v);$checkRow($t,$row);$stmt=$db->prepare($plans[$t]);$stmt->execute(array_values($row));$check($stmt->rowCount()===1,'Exactly one approved fixture insert');
   if($t==='order')$r=$query($db,'SELECT * FROM yoshop_order WHERE order_no=? AND store_id=?',[$marker.'_'.$module,$tenant]);elseif($t==='order_goods')$r=$query($db,'SELECT * FROM yoshop_order_goods WHERE order_id=? AND store_id=?',[$refs['@new_DB_generated_order_id'],$tenant]);else $r=$query($db,'SELECT * FROM yoshop_order_export WHERE file_path=? AND store_id=?',[$row['file_path'],$tenant]);
   $check(count($r)===1,'Independent exact generated identity');$checkRow($t,$row,$r[0]);$new=(int)$r[0][$pk];$check($new>0,'Actual generated positive PK');$journal['owned'][$module]['stat_'.$t]=$new;$journal['dml'][]=['table'=>$t,'values'=>$row,'generated_PK'=>$new];if($t==='order')$refs['@new_DB_generated_order_id']=$new;$persist('fixture-journal.json',$journal);
  }
 }
 $inTransaction=[];foreach($pks as $t=>$pk)$inTransaction[$t]=$query($db,'SELECT * FROM yoshop_'.$t.' WHERE store_id IN (2147001111,2147001112) ORDER BY '.$pk);foreach($before as $t=>$rows){if(in_array($t,['order','order_goods','order_export'],true)){$check(count($inTransaction[$t])===count($rows)+2,'Only two new rows '.$t);foreach($rows as $row)$check(in_array($row,$inTransaction[$t],true),'Old row retained before commit '.$t);}else $check($inTransaction[$t]===$rows,'Other table exact before commit '.$t);}$check($journal['before_protect']===$protect(),'Natural/default before commit');$journal['in_transaction_old23_stable']=true;$db->commit();$journal['committed']=true;$journal['after']=$own();$journal['after_protect']=$protect();$check($inTransaction===$journal['after'],'Independent postcommit full23 rows exact');$check($journal['before_protect']===$journal['after_protect'],'Natural/default unchanged');$persist('fixture-journal.json',$journal);echo json_encode(['lane'=>$lane,'preparation_INSERT'=>6,'UPDATE'=>0,'DELETE'=>0,'DDL'=>0,'API_DML'=>0,'owned_new'=>array_map(static function($ids){return array_intersect_key($ids,array_flip(['stat_order','stat_order_goods','stat_order_export']));},$journal['owned'])]),"\n";
}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$journal['failure']=get_class($e).': '.$e->getMessage();$persist('fixture-journal.json',$journal);throw $e;}
