<?php
declare(strict_types=1);
// No bootstrap/PDO/network: verify the original immutable HTTP/source artifacts offline.
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/projection.php';
$path=$argv[1]??'';if(!is_dir($path)){exit(78);}
$old=json_decode(file_get_contents($path.'/report.json'),true,512,JSON_THROW_ON_ERROR);
$response=json_decode(file_get_contents($path.'/responses-private.json'),false,512,JSON_THROW_ON_ERROR);
$source=json_decode(file_get_contents($path.'/source-private.json'),true,512,JSON_THROW_ON_ERROR);$proof=[];
foreach($old['cases'] as &$case){
    $name=$case['case'];$s=$source[$name];$p=strpos($name,'_MENU_')!==false?rmMenuFromRows($s['source_rows'],$s['relation_rows']):rmRoleFromRows($s['source_rows'],$s['relation_rows']);
    $expected=json_decode(json_encode($p['expected'],JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION|JSON_THROW_ON_ERROR));$actual=$response->$name->body->data->list??null;$err=rmStrict($expected,$actual);
    $case['difference']=$err;$case['source_pass']=$case['http_status']===200&&$case['status']===200&&$err===null;$proof[$name]=$p;
}unset($case);
$old['projection_tool_correction']=['offline_saved_original_wire_and_source_only'=>true,'new_http'=>0,'new_database_queries'=>0,'reason'=>'empty model Collection remains object; actual original eager-load relations remain visible','original_source_failures'=>count(array_filter(json_decode(file_get_contents($path.'/report.json'),true)['cases'],static function($c){return !$c['source_pass'];}))];
$writes=0;foreach($old['events'] as $e){if($e['kind']==='query_observed'&&in_array($e['operation'],['insert','update','delete','merge','replace'],true)){$writes++;}}$old['dml']=$writes;
foreach(['verified-report.json'=>$old,'verified-source-private.json'=>$proof] as $f=>$v){file_put_contents($path.'/'.$f,json_encode($v,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION|JSON_THROW_ON_ERROR));chmod($path.'/'.$f,0600);}
$failed=array_values(array_filter($old['cases'],static function($c){return !$c['source_pass'];}));echo json_encode(['lane'=>$old['lane'],'cases'=>count($old['cases']),'source_failures'=>count($failed),'failed'=>$failed,'new_http'=>0,'new_database_queries'=>0,'observed_dml'=>$writes]),PHP_EOL;exit($failed||$writes?1:0);
