<?php
declare(strict_types=1);require __DIR__.'/native-store-continue.php';require __DIR__.'/decimal-oracle.php';require __DIR__.'/datetime-oracle.php';
ck($lane==='C'&&!file_exists($runtime.'/prepared.json')&&!file_exists($runtime.'/prepare-store-continue-attempt.json'),'Only C remaining Store5 once');saveOnce('prepare-store-continue-attempt.json',['lane'=>$lane,'at'=>date('c'),'budget'=>'C cumulative9 INSERT attempts=oldAIrollback1+AIcommit2+staffrollback1+Store5; no AI reinsert']);$source=sourceProof();$planRaw=file_get_contents(__DIR__.'/typed-prepare-plan.json');ck(hash('sha256',$planRaw)==='50a7b2dba60b68b81aa6685676fdfb821da9441207d47108541e7a722321262a','Typed unchanged7 plan');$plan=json_decode($planRaw,true);$prior=json_decode(file_get_contents($runtime.'/prepare-retry-failed.json'),true);ck($prior['ids']===['AI_U1'=>1931,'AI_U2'=>1932,'STORE_S1'=>2214],'Exact committed/rollback generated IDs');$ids=['AI_U1'=>1931,'AI_U2'=>1932];$rows=[];$events=array_map(static function($l){return json_decode($l,true);},file($runtime.'/independent-native-retry.jsonl',FILE_IGNORE_NEW_LINES));
foreach(['AI_U1','AI_U2'] as $name){$original=[];foreach($events as $e)if($e['queryString']==='SELECT * FROM yoshop_user WHERE user_id=?'&&(string)$e['bindValue'][0]['value']===(string)$ids[$name])$original=$e['rows'];ck(count($original)===1,'Original committed AI fullrow captured');$actual=readNative('AI','SELECT * FROM yoshop_user WHERE user_id=?',[$ids[$name]]);ck($actual===$original,'Original committed AI fullrow exact lease '.$name);$texts=readNative('AI','SELECT CAST(balance AS VARCHAR(40)) balance_text,CAST(gift_balance AS VARCHAR(40)) gift_balance_text,CAST(pay_money AS VARCHAR(40)) pay_money_text FROM yoshop_user WHERE user_id=?',[$ids[$name]]);foreach(['balance'=>$name==='AI_U1'?'1.00':'0.00','gift_balance'=>'0.00','pay_money'=>'0.00'] as $key=>$v)ck(nativeDec102Type($actual[0][$key])&&dec102text($texts[0][$key.'_text'])===$v,'Committed AI DEC102 lease');$rows[$name]=$actual[0];}
$old=oldProtection();$base=json_decode(file_get_contents($runtime.'/protection-C.json'),true)['protected'];foreach($old as $key=>$value)ck($key==='ZHCT.exact_config_hashes'?$value===$base[$key]['sha256']:$value===$base[$key]['rows'],'Old protection before Store5 '.$key);
$validation=json_decode(file_get_contents($runtime.'/prepare-validation-C.json'),true);foreach($validation['native_events'] as $e){if($e['connection']==='AI')continue;$z=readNative('ZHCT',$e['queryString'],array_column($e['bindValue'],'value'));ck((int)$z[0]['n']===0,'Store own marker/count fresh0');}$zero=readNative('ZHCT','SELECT COUNT(*) n FROM ydy_staff WHERE id=?',[2214]);ck((int)$zero[0]['n']===0,'Rolled back Store2214 absent');
$b=json_decode(file_get_contents(__DIR__.'/preflight-B-reference.json'),true);$c=json_decode(file_get_contents($runtime.'/preflight-C.json'),true);$schema=[];foreach(['staff','user'] as $table)foreach(['B'=>$b,'C'=>$c] as $side=>$p)foreach($p['schema']['ZHCT'][$table]['columns'] as $col)if(strtolower($col['column_name'])==='create_time')$schema[$table][$side]=$col;foreach($schema as $table=>$ss)ck(exactCreateTime($table,'create_time','2026-10-11 00:00:00.000000','2026-10-11 00:00:00',$ss['B'],$ss['C']),'Explicit frozen source/target date schema');saveOnce('prepare-store-continue-preconditions.json',['AI_fullrows'=>$rows,'old_protection'=>$old,'rollback2214_count'=>$zero,'create_time_metadata'=>$schema]);$writes=[];$p=nativePdo('ZHCT');$active=false;
try{ck($p->beginTransaction(),'Store5 native begin');$active=true;event('prepare-native-store-continue.jsonl',['connection'=>'ZHCT','operation'=>'beginTransaction','success'=>true]);foreach($plan['rows'] as $entry){if($entry['connection']!=='mysql_zhct')continue;$values=[];$bind=[];foreach($entry['values'] as $key=>[$type,$v]){if($type==='ref'){ck(isset($ids[$v]),'Only real prior generated ref');$v=$ids[$v];}$values[$key]=$v;$bind[]=['column'=>$key,'value'=>$v,'pdo_type'=>$type==='str'?PDO::PARAM_STR:PDO::PARAM_INT,'php_type'=>gettype($v)];}ck(!isset($values[$entry['pk']]),'No identity supplied');$sql='INSERT INTO '.$entry['table'].' ('.implode(',',array_keys($values)).') VALUES ('.implode(',',array_fill(0,count($values),'?')).')';$s=$p->prepare($sql);foreach($bind as $i=>$v)ck($s->bindValue($i+1,$v['value'],$v['pdo_type']),'Exact actual Store5 bound type');$ok=$s->execute();$rec=['connection'=>'ZHCT','name'=>$entry['name'],'queryString'=>$s->queryString,'bindValue'=>$bind,'execute'=>$ok,'rowCount'=>$s->rowCount()];event('prepare-native-store-continue.jsonl',$rec);ck($ok&&$s->rowCount()===1,'Store5 affected1');$id=$p->lastInsertId();event('prepare-native-store-continue.jsonl',['connection'=>'ZHCT','name'=>$entry['name'],'operation'=>'lastInsertId','value'=>$id,'php_type'=>gettype($id)]);ck(ctype_digit((string)$id)&&(int)$id>0,'Actual Store identity');$ids[$entry['name']]=(int)$id;$actual=readNative('ZHCT','SELECT * FROM '.$entry['table'].' WHERE '.$entry['pk'].'=?',[$id]);ck(count($actual)===1,'Store5 complete fullrow');$table=substr($entry['table'],4);foreach($values as $key=>$v){if($key==='create_time'&&isset($schema[$table]))ck(exactCreateTime($table,$key,$actual[0][$key],$v,$schema[$table]['B'],$schema[$table]['C']),'Only explicit CREATE_TIME zero fraction oracle');else ck((string)$actual[0][$key]===(string)$v,'Exact Store5 field '.$entry['name'].'.'.$key);}$rows[$entry['name']]=$actual[0];$writes[]=$rec;}
ck(count($writes)===5,'Exactly Store5');$after=oldProtection();ck($after===$old,'Old protection before Store commit');foreach(['AI_U1','AI_U2'] as $name)ck(readNative('AI','SELECT * FROM yoshop_user WHERE user_id=?',[$ids[$name]])===[$rows[$name]],'Committed AI unchanged through Store5');ck($p->commit(),'Native Store5 commit');$active=false;event('prepare-native-store-continue.jsonl',['connection'=>'ZHCT','operation'=>'commit','success'=>true]);saveOnce('prepared.json',['lane'=>$lane,'ids'=>$ids,'rows'=>$rows,'writes'=>array_merge($prior['completed_writes'],$writes),'source'=>$source,'old_protection_before'=>$old,'old_protection_after'=>$after,'INSERT'=>7,'prepare_attempts_total'=>9,'UPDATE'=>0,'DELETE'=>0,'DDL'=>0,'HTTP'=>0,'finished_at'=>date('c')]);echo json_encode(['lane'=>$lane,'prepared7'=>true,'new_Store_INSERT'=>5,'cumulative_INSERT_attempts'=>9,'ids'=>$ids]),"\n";}catch(Throwable $e){if($active){$p->rollBack();event('prepare-native-store-continue.jsonl',['connection'=>'ZHCT','operation'=>'rollBack','success'=>true]);}saveOnce('prepare-store-continue-failed.json',['class'=>get_class($e),'message'=>$e->getMessage(),'ids'=>$ids,'writes'=>$writes]);throw $e;}
