<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';
$check(!is_file($runtime.'/fixture-journal.json'),'No preparation replay after success or uncertain state');$f=json_decode(file_get_contents($runtime.'/preflight-original.json'),true);$check($f['DML']===0&&count($f['plans'])===7,'Approved preflight7');$before=$own();$check($before===$f['original'],'Fresh full protected scope exact before preparation');
$approvedSha='47c0f06cae327aac2027b1d256c2dac7fe0fec7e372985cce77006b2a17a2b41';$check(hash_file('sha256',__DIR__.'/fixture-values-template.json')===$approvedSha,'Root-approved exact fixture template');
$quote=static function($k)use($dm){return $dm?'"'.strtoupper($k).'"':'`'.$k.'`';};$journal=['lane'=>$lane,'template_sha256'=>$approvedSha,'before'=>$before,'statements'=>[],'owned'=>[],'committed'=>false];$persist('fixture-journal.json',$journal);
$db->beginTransaction();try{foreach($f['plans'] as $i=>$p){$r=$p['values'];$keys=array_keys($r);$sql='INSERT INTO yoshop_'.$p['table'].' ('.implode(',',array_map($quote,$keys)).') VALUES ('.implode(',',array_fill(0,count($keys),'?')).')';$journal['statements'][$i]=['table'=>$p['table'],'module'=>$p['module'],'queryString'=>$sql,'execute_params'=>array_values($r),'prepared'=>false,'executed'=>false,'rowCount'=>null];$persist('fixture-journal.json',$journal);$s=$db->prepare($sql);$journal['statements'][$i]['prepared']=true;$persist('fixture-journal.json',$journal);$s->execute(array_values($r));$journal['statements'][$i]['executed']=true;$journal['statements'][$i]['rowCount']=$s->rowCount();$journal['statements'][$i]['queryString']=$s->queryString;$persist('fixture-journal.json',$journal);$check($s->rowCount()===1,'Only one affected INSERT');$field=$p['table']==='user'?'nick_name':($p['table']==='user_feedback'?'contents':($dm?'"DESCRIBE"':'`describe`'));$value=$r[$p['table']==='user'?'nick_name':($p['table']==='user_feedback'?'contents':'describe')];$got=$query($db,'SELECT * FROM yoshop_'.$p['table'].' WHERE store_id=? AND '.$field.'=?',[$r['store_id'],$value]);$check(count($got)===1,'Own generated original row unique');$row=$got[0];$check((int)$row[$p['pk']]>0,'Real generated PK');$expectedKeys=array_merge($keys,[$p['pk']]);sort($expectedKeys);$actualKeys=array_keys($row);sort($actualKeys);$check($expectedKeys===$actualKeys,'Full physical columns');foreach($r as $k=>$v){if(is_int($v))$check((int)$row[$k]===$v,'Integer original '.$p['table'].'.'.$k);elseif(in_array($k,['balance','withdrawable_balance','activity_principal_balance','subsidy_balance','gift_balance','pay_money','expend_money','money','balance_after'],true))$check(is_numeric($row[$k])&&(float)$row[$k]===0.0&&$v==='0.00','Explicit zero-decimal original '.$k);else $check($row[$k]===$v,'Exact bytes original '.$p['table'].'.'.$k);}$journal['owned'][]=['table'=>$p['table'],'module'=>$p['module'],'pk'=>$p['pk'],'id'=>(int)$row[$p['pk']],'original'=>$row];$persist('fixture-journal.json',$journal);}
 $tx=[];foreach($pks as $t=>$pk)$tx[$t]=$query($db,'SELECT * FROM yoshop_'.$t.' WHERE store_id IN (2147001111,2147001112,2147001113) ORDER BY '.$pk);foreach($before as $t=>$old){$ids=[];foreach($journal['owned'] as $o)if($o['table']===$t)$ids[]=$o['id'];$without=array_values(array_filter($tx[$t],static function($r)use($ids,$pks,$t){return !in_array((int)$r[$pks[$t]],$ids,true);}));$check($without===$old,'All existing full rows unchanged '.$t);$check(count($tx[$t])===count($old)+count($ids),'Only approved rows added '.$t);}$db->commit();$journal['committed']=true;$persist('fixture-journal.json',$journal);$after=$own();$check($after===$tx,'Independent new-PDO all originals exact after commit');$journal['after']=$after;$persist('fixture-journal.json',$journal);$out=['lane'=>$lane,'preparation_INSERT'=>7,'affected_rows'=>7,'UPDATE'=>0,'DELETE'=>0,'DDL'=>0,'old32_unchanged'=>true,'independent_new_PDO_exact'=>true,'owned'=>array_map(static function($o){unset($o['original']);return $o;},$journal['owned'])];file_put_contents(__DIR__.'/fixture-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT));echo json_encode($out),"\n";
}catch(Throwable $e){if($db->inTransaction())$db->rollBack();$journal['error']=['class'=>get_class($e),'message'=>$e->getMessage()];$persist('fixture-journal.json',$journal);throw $e;}
