<?php
declare(strict_types=1);
require __DIR__.'/bootstrap.php';require __DIR__.'/oracle.php';require __DIR__.'/StrictWire.php';require __DIR__.'/SqlCoverageRecorder.php';$phases=$lane==='B'?['original','candidate']:['original','candidate','candidate_v2'];$typed=static function($v)use(&$typed){if(is_object($v)){$a=get_object_vars($v);ksort($a);$members=[];foreach($a as $k=>$x)$members[$k]=$typed($x);return ['type'=>'object','members'=>$members];}if(is_array($v))return ['type'=>'array','items'=>array_map($typed,$v)];return ['type'=>gettype($v),'value_sha256'=>hash('sha256',serialize($v))];};$reports=[];$cases=[];$fresh=analysisOriginalSnapshot($read,$tenant,$staff,$operator);$freshSha=hash('sha256',serialize($fresh));$diff=static function($a,$b,$path='')use(&$diff){if(gettype($a)!==gettype($b))return [['path'=>$path,'actual_type'=>gettype($a),'expected_type'=>gettype($b)]];if(is_object($a)||is_array($a)){$out=[];foreach((array)$b as $k=>$v)$out=array_merge($out,$diff(((array)$a)[$k]??null,$v,$path.'/'.$k));return $out;}return $a===$b?[]:[['path'=>$path,'value_diff'=>true,'actual_type'=>gettype($a),'expected_type'=>gettype($b)]];};foreach($phases as $phase){$r=json_decode(file_get_contents($runtime.'/private-meal-infos-'.$phase.'.json'));$reports[$phase]=$r;$check($r->original_unchanged&&$r->after_sha256===$freshSha&&$r->tokens_reclaimed,'Same independent original snapshot across infos phases');$check($r->http_status===200&&$r->body->status===200,'Actual infos success envelope');if($phase!=='original'||$lane==='B')$check($r->native_exit===0&&strictWireEqual($r->body->data,$r->expected_data),'Actual complete three-table original oracle');$cases[]=['phase'=>$phase,'http_status'=>$r->http_status,'business_status'=>$r->body->status,'native_exit'=>$r->native_exit,'complete_original_equal'=>$r->complete_original_equal,'actual_contract'=>$typed($r->body->data),'expected_contract'=>$typed($r->expected_data),'difference'=>$diff($r->body->data,$r->expected_data),'original_unchanged'=>true,'rows'=>count($r->body->data->list),'raw_response_sha256'=>$r->raw_sha256,'native_stats'=>$r->stats,'boundary'=>$r->boundary,'controller_identity'=>$r->controller_identity];}
$invariant=null;if($lane==='B'){$a=clone $reports['original']->body;$b=clone $reports['candidate']->body;unset($a->trace_id,$b->trace_id);$check(strictWireEqual($a,$b),'MySQL complete infos original body invariant except top trace');$invariant=['complete_body_unchanged_except_top_trace'=>true,'sha256'=>hash('sha256',serialize($typed($a)))];}
$dependency=[];$depStats=[];if($lane==='C'){$r=json_decode(file_get_contents($runtime.'/private-mi-dependency.json'));$check($r->native_exit===0&&$r->original_unchanged&&count($r->cases)===7,'Final C shared dependency actual seven requests');foreach($r->cases as $c){$check($c->complete_original_equal&&strictWireEqual($c->body->data,$c->expected_data)&&$c->http_status===200,'Dependency complete original oracle');$dependency[]=['scenario'=>$c->scenario,'complete_original_equal'=>true,'http_status'=>$c->http_status,'actual_contract'=>$typed($c->body->data),'expected_contract'=>$typed($c->expected_data),'raw_response_sha256'=>$c->raw_sha256];}$depStats=$r->stats;}
$dml=0;$audit=[];
foreach(file($runtime.'/private-sql.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $line){
    $v=json_decode($line,true);if(strpos($v['scenario'],'PILOT_TA_MI_')!==0)continue;$sql=$v['sql'];
    if(preg_match('/^\s*(INSERT|UPDATE|DELETE|REPLACE|MERGE|ALTER|DROP|CREATE)\b/i',$sql))$dml++;
    $tables=[];
    if(preg_match('/^\s*SELECT\b/i',$sql)&&preg_match_all('/\b(?:FROM|JOIN)\s+["`]?yoshop_([a-z_]+)["`]?/i',$sql,$m))$tables=array_map('strtolower',$m[1]);
    $scoped=array_diff($tables,['dishes_cate','config_enum']);
    $tenantOk=!$scoped||preg_match('/store_id["`]?\s*=\s*[\x27]?'.$tenant.'\b/i',$sql)===1;
    $check($tenantOk,'Actual scoped SELECT tenant condition');
    if(strpos($v['scenario'],'PILOT_TA_MI_DEP_')!==0)$check(array_diff($tables,['meal','meal_infos','dishes'])===[],'Infos actual business read only exact three tables');
    $audit[]=['scenario'=>$v['scenario'],'tables'=>$tables,'tenant_scope_verified'=>$tenantOk,'operation'=>SqlCoverageRecorder::fingerprint($sql)['operation'],'sql_sha256'=>hash('sha256',$sql)];
}
$check($dml===0,'DML0');$cache->clear();
$out=['lane'=>$lane,'path'=>'GET /terminal/meal.Meal/infos','cases'=>$cases,
    'dependency_C_seven'=>$dependency,'dependency_native_stats'=>$depStats,'final_boundary'=>$boundary,
    'business_DML'=>0,'sql_audit'=>$audit,'fresh_original_snapshot_sha256'=>$freshSha,
    'original_rows_unchanged'=>true,'mysql_complete_body_invariance'=>$invariant,
    'B_final_v2_native_replayed'=>false,
    'B_v2_proof'=>'Original/v1 complete body unchanged plus real MySQL connector pure passthrough unit tests; no repeated B HTTP',
    'tokens_cache_reclaimed'=>true,'metadata'=>$metadata,
    'data_policy'=>'Natural values differ across lanes: full own original PDO oracle, only type/value hash public'];
file_put_contents(__DIR__.'/infos-evidence-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRESERVE_ZERO_FRACTION));
$collection=json_decode(json_encode($reports['candidate']->collection),true);$i=&$collection['item'][0]['item'][0];$i['request']['url']=preg_replace('/staff_id=\d+/','staff_id={{staff_id}}',str_replace($base,'{{base_url}}',$i['request']['url']));foreach($i['request']['header'] as &$h)if($h['key']==='Access-Token')$h['value']='{{terminal_token}}';unset($h);$i['event'][0]['script']['exec'][2]="pm.test('Business status only; full three-table oracle requires private runner',function(){pm.expect(b.status).to.eql(200);});";unset($i);file_put_contents(__DIR__.'/meal-infos-smoke-'.$lane.'.apifox-cli.json',json_encode($collection,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));echo json_encode(['lane'=>$lane,'infos_HTTP'=>count($cases),'dependency_HTTP'=>count($dependency),'all_current_original_equal'=>true,'business_DML'=>0,'mysql_invariant'=>$invariant!==null]),"\n";
