<?php
declare(strict_types=1);
require __DIR__.'/DbWriteAudit.php';$runtime=tempnam(sys_get_temp_dir(),'cdx-weight-audit-');unlink($runtime);mkdir($runtime,0700);
file_put_contents($runtime.'/private-owned-map.json',json_encode(['staff_id'=>801,'user_id'=>901,'tenant'=>2147000921,'weight_ids'=>[701],'current_ids'=>[601]]));$a=new DbWriteAudit($runtime);
$cases=[
    ['weight-insert-own',"INSERT INTO yoshop_staff_weight (staff_id,store_id) VALUES (801,2147000921)",true],
    ['history-set-own',"INSERT INTO yoshop_meal_recommend_history SET staff_id=801,store_id=2147000921",true],
    ['staff-update-own',"UPDATE yoshop_staff SET weight='62.375' WHERE yoshop_staff.store_id='2147000921' AND id=801",true],
    ['weight-update-own',"UPDATE yoshop_staff_weight SET is_delete=1 WHERE yoshop_staff_weight.store_id='2147000921' AND id=701",true],
    ['current-update-own',"UPDATE yoshop_meal_recommend SET recommend_power=1 WHERE yoshop_meal_recommend.store_id='2147000921' AND id=601",true],
    ['insert-other-staff',"INSERT INTO yoshop_staff_weight (staff_id,store_id) VALUES (802,2147000921)",false],
    ['insert-other-store',"INSERT INTO yoshop_staff_weight (staff_id,store_id) VALUES (801,10001)",false],
    ['insert-fraction-owner',"INSERT INTO yoshop_staff_weight (staff_id,store_id) VALUES (801.1,2147000921)",false],
    ['staff-other-id',"UPDATE yoshop_staff SET weight=1 WHERE yoshop_staff.store_id='2147000921' AND id=802",false],
    ['weight-other-id',"UPDATE yoshop_staff_weight SET is_delete=1 WHERE yoshop_staff_weight.store_id='2147000921' AND id=702",false],
    ['current-other-id',"UPDATE yoshop_meal_recommend SET recommend_power=1 WHERE yoshop_meal_recommend.store_id='2147000921' AND id=602",false],
    ['no-scope',"UPDATE yoshop_staff SET weight=1 WHERE id=801",false],
    ['scope-widen-or',"UPDATE yoshop_staff SET weight=1 WHERE store_id=2147000921 AND id=801 OR id=802",false],
    ['rekey',"UPDATE yoshop_staff SET id=802 WHERE store_id=2147000921 AND id=801",false],
    ['change-owner',"UPDATE yoshop_staff_weight SET staff_id=802 WHERE store_id=2147000921 AND id=701",false],
    ['user-write-forbidden',"UPDATE yoshop_user SET balance=1 WHERE store_id=2147000921 AND id=901",false],
    ['coupon-write-forbidden',"INSERT INTO yoshop_coupon (staff_id,store_id) VALUES (801,2147000921)",false],
    ['foreign-schema',"INSERT INTO other.yoshop_staff_weight (staff_id,store_id) VALUES (801,2147000921)",false],
    ['delete-forbidden',"DELETE FROM yoshop_staff_weight WHERE id=701",false],
];$out=[];foreach($cases as [$name,$sql,$want]){$ok=true;try{$a->observe($sql);}catch(RuntimeException $e){$ok=false;}if($ok!==$want)throw new RuntimeException('Missed weight audit '.$name);$out[]=['case'=>$name,'accepted'=>$ok,'expected'=>$want];}
echo json_encode(['assertions'=>count($out),'all_passed'=>true,'database_called'=>false,'cases'=>$out],JSON_PRETTY_PRINT),"\n";
foreach(['private-owned-map.json','private-dml-audit-sql.jsonl','write-audit.jsonl'] as $name)unlink($runtime.'/'.$name);rmdir($runtime);
