<?php
declare(strict_types=1);
define('RFJ_CONTEXT_ONLY',true);require __DIR__.'/preflight.php';
$owned=json_decode((string)file_get_contents(__DIR__.'/private-write-journal.json'),true);$result=json_decode((string)file_get_contents(__DIR__.'/result-private.json'),true);if(!is_array($owned)||!preg_match('/^CDXRFJ10[BC][A-F0-9]{8}$/D',$owned['marker']??''))exit(78);
$counts=['orders'=>count($q($store,'SELECT ID FROM '.$s.'meal_order WHERE ID=? OR ORDER_NO=?',[$owned['order_id'],$owned['marker']])),'refunds'=>count($q($store,'SELECT ID FROM '.$s.'meal_order_refund WHERE MEAL_ORDER_ID=? OR APPLY_DESC=?',[$owned['order_id'],$owned['marker']])),'stalls'=>count($q($store,'SELECT ID FROM '.$s.'restaurants WHERE ID=? OR UUID=?',[$owned['stall_id'],$owned['stall_uuid']])),'audits'=>count($q($store,'SELECT ID FROM '.$s.'security_audit_log WHERE OPERATOR=? AND TARGET_TYPE=? AND TARGET_ID=?',['CDXRFJ10AUDIT','order',(string)$owned['order_id']]))];foreach($owned['refund_ids'] as $id)$counts['audits']+=count($q($store,'SELECT ID FROM '.$s.'security_audit_log WHERE OPERATOR=? AND TARGET_TYPE=? AND TARGET_ID=?',['CDXRFJ10AUDIT','refund',(string)$id]));
$sources=['store_user'=>$q($store,'SELECT * FROM '.$s.'user WHERE ID=?',[(int)$chosen['store_user']['id']]),'store_staff'=>$q($store,'SELECT * FROM '.$s.'staff WHERE UUID=?',[$chosen['staff']['uuid']]),'store_existing_orders'=>$q($store,'SELECT * FROM '.$s.'meal_order WHERE USER_ID=? AND ORDER_NO<>?',[(int)$chosen['store_user']['id'],$owned['marker']])];foreach(['user','user_balance_log','user_subsidy','user_subsidy_log','user_subsidy_used','user_gift_log','account_settlement'] as $table)$sources['ai_'.$table]=$q($ai,'SELECT * FROM '.$a.$table.' WHERE USER_ID=?',[(int)$chosen['ai_user']['user_id']]);
$unchanged=[];foreach($sources as $key=>$rows){foreach($rows as &$row)ksort($row);unset($row);usort($rows,static function($x,$y){return strcmp(serialize($x),serialize($y));});$sha=hash('sha256',json_encode($rows,JSON_UNESCAPED_UNICODE|JSON_INVALID_UTF8_SUBSTITUTE|JSON_PRESERVE_ZERO_FRACTION));$unchanged[$key]=['rows'=>count($rows),'sha'=>$sha,'same_before'=>$sha===$result['wallet_source_before'][$key]['sha']];}
$expected=['p/controller/MealOrder.php'=>'0e818de300828a2c3546d6ccb83fe22ef0dd87bf5cc9c885ef646e6342f2ad5b','p/logic/MealOrder.php'=>'fc03bfe67b7d4e2eb8edafbbd9423d7917fba4c456fd705ce87b94fcaf677d13','common/service/SecurityAuditService.php'=>'a57fc430f52f541d1619ae87fe29651dca3ab408013fe6f38a80f5aa6db62cf1','common/service/Order.php'=>'e38805a0f3e6a540e041aa0cdbac29a398ca35d5f63f61d8e128854e8bd599a1'];$hashes=[];foreach($expected as $file=>$sha)$hashes[$file]=['sha256'=>hash_file('sha256',__DIR__.'/source/application/'.$file),'matches_frozen'=>hash_file('sha256',__DIR__.'/source/application/'.$file)===$sha];
$pass=array_sum($counts)===0;foreach($unchanged as $row)$pass=$pass&&$row['same_before'];foreach($hashes as $row)$pass=$pass&&$row['matches_frozen'];$report=['lane'=>$lane,'independent_new_connections'=>true,'remaining_owned_rows'=>$counts,'wallet_sources'=>$unchanged,'source_sha256'=>$hashes,'database_writes'=>0,'all_passed'=>$pass];file_put_contents(__DIR__.'/post-cleanup-audit.json',json_encode($report,JSON_PRETTY_PRINT));chmod(__DIR__.'/post-cleanup-audit.json',0600);echo json_encode($report),"\n";exit($pass?0:2);
