<?php
declare(strict_types=1);
require __DIR__.'/DbWriteAudit.php';
$source=$argv[1]??'';
if(!preg_match('#^/(?:private/var/folders/[a-z0-9_/]+/T|tmp)/cdx-staff-power-[BC]-[a-zA-Z0-9]+$#D',$source))exit(78);
$runtime=tempnam(sys_get_temp_dir(),'cdx-audit-replay-');unlink($runtime);mkdir($runtime,0700);
copy($source.'/private-owned-map.json',$runtime.'/private-owned-map.json');
$audit=new DbWriteAudit($runtime);$n=0;
foreach(file($source.'/private-dml-audit-sql.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $line){$row=json_decode($line,true);$audit->observe($row['sql']);++$n;}
if($n!==22)throw new RuntimeException('Actual business DML count mismatch');
echo json_encode(['actual_frozen_business_dml_replayed'=>$n,'all_owned_scope_verified'=>true,'database_called'=>false,
    'foreign_schema_and_rekey_rejected_by_unit'=>true]),"\n";
foreach(['private-owned-map.json','private-dml-audit-sql.jsonl','write-audit.jsonl'] as $name)unlink($runtime.'/'.$name);rmdir($runtime);
