<?php
declare(strict_types=1);
require __DIR__.'/ConfigWriteAudit.php';$r=tempnam(sys_get_temp_dir(),'cdx-cf-audit-');unlink($r);mkdir($r,0700);file_put_contents($r.'/private-owned-map.json',json_encode(['staff_id'=>801,'user_id'=>901]));$a=new ConfigWriteAudit($r,2147001321);
$cases=[
['master-own',"INSERT INTO yoshop_athlete_master (store_id,staff_id) VALUES (2147001321,'801')",true],
['pal-own-deferred-parent',"INSERT INTO yoshop_athlete_pal (store_id,staff_id,master_id) VALUES (2147001321,'801',701)",true],
['health-own',"INSERT INTO yoshop_health SET store_id=2147001321,staff_id=801,health=1",true],
['health-difference-own',"DELETE FROM yoshop_health WHERE yoshop_health.store_id='2147001321' AND staff_id=801 AND health IN (1,3)",true],
['health-difference-quoted',"DELETE FROM yoshop_health WHERE store_id=2147001321 AND staff_id='801' AND health IN ('2','5')",true],
['other-staff-insert',"INSERT INTO yoshop_athlete_master (store_id,staff_id) VALUES (2147001321,'802')",false],
['other-store-insert',"INSERT INTO yoshop_health (store_id,staff_id,health) VALUES (10001,801,1)",false],
['multi-staff-csv',"INSERT INTO yoshop_athlete_master (store_id,staff_id) VALUES (2147001321,'801,802')",false],
['pal-zero-parent',"INSERT INTO yoshop_athlete_pal (store_id,staff_id,master_id) VALUES (2147001321,'801',0)",false],
['invalid-health-insert',"INSERT INTO yoshop_health (store_id,staff_id,health) VALUES (2147001321,801,99)",false],
['other-staff-delete',"DELETE FROM yoshop_health WHERE store_id=2147001321 AND staff_id=802 AND health IN (1,3)",false],
['other-store-delete',"DELETE FROM yoshop_health WHERE store_id=10001 AND staff_id=801 AND health IN (1,3)",false],
['invalid-health-delete',"DELETE FROM yoshop_health WHERE store_id=2147001321 AND staff_id=801 AND health IN (1,99)",false],
['no-store-delete',"DELETE FROM yoshop_health WHERE staff_id=801 AND health IN (1,3)",false],
['or-widen-delete',"DELETE FROM yoshop_health WHERE store_id=2147001321 AND staff_id=801 AND health IN (1,3) OR staff_id=802",false],
['duplicate-scope',"DELETE FROM yoshop_health WHERE store_id=2147001321 AND store_id=2147001321 AND health IN (1,3)",false],
['updates-forbidden',"UPDATE yoshop_health SET health=5 WHERE store_id=2147001321 AND staff_id=801",false],
['parent-delete-forbidden',"DELETE FROM yoshop_athlete_master WHERE store_id=2147001321 AND staff_id=801 AND health IN (1)",false],
['external-table',"INSERT INTO yoshop_order (store_id,staff_id) VALUES (2147001321,801)",false],
];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Config/Health audit '.$name);}echo json_encode(['cases'=>count($cases),'all_passed'=>true,'database_called'=>false,'post_execution_only'=>true,'pal_master_fk_requires_independent_verification_after_commit'=>true]),"\n";foreach(['private-owned-map.json','private-write-sql.jsonl','write-audit.jsonl'] as $f)unlink($r.'/'.$f);rmdir($r);
