<?php
declare(strict_types=1);
try {
 require __DIR__.'/common.php';
 $scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';$cases=finishCases($runtime);
 $check(isset($cases[$scenario]),'Exact finish case');$case=$cases[$scenario];$baseScenario=$case['base_scenario']??$scenario;
 $tokens=json_decode(file_get_contents($runtime.'/tokens.json'),true);
 $check($_SERVER['REQUEST_METHOD']==='POST'&&parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)===$case['path']&&($_SERVER['QUERY_STRING']??'')===''&&$_SERVER['HTTP_HOST']===$case['host']&&($_SERVER['CONTENT_TYPE']??'')==='application/json'&&file_get_contents('php://input')===$case['body_raw']&&($_SERVER['HTTP_ACCESS_TOKEN']??'')===$tokens['one'],'Exact original POST/JSON/host/Auth/body');
 $check(!file_exists($runtime.'/wire-'.$scenario.'.json'),'No HTTP replay');
 $patched=$lane==='C'||strpos($scenario,'FINAL_CONTROL')!==false;
 $source=json_decode(file_get_contents($patched?__DIR__.'/source-final-sha.json':dirname(__DIR__).'/source-frozen-sha.json'),true)['files'];
 foreach($source as $name=>$sha)$check(hash_file('sha256',$root.'/'.$name)===$sha,'Whole selected source before HTTP '.$name);
 $_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';
 $before=$small();$guardBefore=$protected();$pre=json_decode(file_get_contents($runtime.'/preflight.json'),true);
 $check($guardBefore===$pre['protected'],'Protected whole batch initial unchanged');
 $app=invApp($root,$runtime,$baseScenario);InvGate::$scenario=$baseScenario;InvGate::$recordScenario=$scenario;InvGate::$case=$case;InvGate::lease($case,$before);
 $persist('lease-'.$scenario.'.json',['case'=>$case,'before'=>$before,'operations'=>InvGate::$ops,'source_stage'=>$patched?'final':'original']);
 $http=$app->http;$response=$http->run();$after=$small();$guardAfter=$protected();
 $check($guardBefore===$guardAfter,'All original rows/actors/dictionary unchanged');
 $included=[];foreach(get_included_files() as $file)if(strpos($file,$root.'/')===0){$name=substr($file,strlen($root)+1);$included[$name]=hash_file('sha256',$file);$check($source[$name]===$included[$name],'Actual included selected source '.$name);}
 $persist('wire-'.$scenario.'.json',['scenario'=>$scenario,'case'=>$case,'source_stage'=>$patched?'final':'original','http_status'=>$response->getCode(),'raw'=>$response->getContent(),'before'=>$before,'after'=>$after,'protected_before'=>$guardBefore,'protected_after'=>$guardAfter,'included_source_sha256'=>$included,'successful_master_INSERT'=>InvGate::$masterInserts,'successful_answer_INSERT'=>InvGate::$answerInserts,'master_generated_native_ID'=>InvGate::$masterId,'successful_UPDATE'=>InvGate::$updates,'successful_DELETE'=>InvGate::$deletes,'exact_operation_count'=>InvGate::$index]);
 $response->send();$http->end($response);
} catch(Throwable $e) {
 if(isset($persist))$persist('harness-failure-'.preg_replace('/[^A-Z0-9_]/','',strtoupper($scenario??'INIT')).'.json',['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine()]);
 http_response_code(500);header('Content-Type: application/json');echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]);
}
