<?php
declare(strict_types=1);
$lane=getenv('CATEGORY_LANE');$runtime=getenv('CATEGORY_RUNTIME');$dm=getenv('POC_DIALECT')==='dm';$db=new PDO($dm?'dm:host=127.0.0.1;port=5238;schema=AIZHCT_APP':'mysql:host='.getenv('POC_AI_HOST').';port='.getenv('POC_AI_PORT').';dbname='.getenv('POC_AI_SCHEMA').';charset=utf8',getenv('POC_AI_USER'),getenv('POC_AI_PASSWORD'),[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_CASE=>PDO::CASE_LOWER]);$q=static function(string $sql,array $args=[])use($db):array{$s=$db->prepare($sql);$s->execute($args);$rows=[];while($r=$s->fetch(PDO::FETCH_ASSOC)){$r=array_change_key_case($r,CASE_LOWER);foreach($r as &$v)if(is_resource($v))$v=stream_get_contents($v);unset($v);$rows[]=$r;}return $rows;};$main=json_decode((string)file_get_contents($runtime.'/report.json'),true);$extra=json_decode((string)file_get_contents($runtime.'/extra-report.json'),true);$ids=array_column($main['owned'],'template_id');$rows=$q('SELECT * FROM yoshop_payment_template ORDER BY template_id');$natural=[];$retained=[];foreach($rows as $r){if(in_array((int)$r['template_id'],$ids,true)){$cfg=json_decode((string)$r['config'],true);$retained[]=['template_id'=>(int)$r['template_id'],'store_id'=>(int)$r['store_id'],'marker'=>$r['name'],'method'=>$r['method'],'is_delete'=>(int)$r['is_delete'],'sort'=>(int)$r['sort'],'raw_config_sha256'=>hash('sha256',$r['config']),'raw_config_bytes'=>strlen($r['config']),'selected_config'=>$cfg];}else $natural[]=$r;}$payments=$q('SELECT * FROM yoshop_payment ORDER BY payment_id');$stores=$q('SELECT store_id,store_name FROM yoshop_store WHERE store_id IN (?,?) ORDER BY store_id',[2147000701,2147000702]);$references=$q('SELECT payment_id FROM yoshop_payment WHERE template_id IN ('.implode(',',array_fill(0,count($ids),'?')).')',$ids);$ownValid=count($retained)===4;foreach($retained as $r)$ownValid=$ownValid&&in_array($r['store_id'],[2147000701,2147000702],true)&&strpos($r['marker'],'CDXTPL10_')===0&&$r['is_delete']===($r['method']==='wechat'?1:0);
$report=['lane'=>$lane,'independent_new_connection'=>true,'business_writes'=>0,'exact_own_pk_exclusion'=>true,'natural_templates_unchanged'=>hash('sha256',serialize($natural))===$main['source_before']['payment_template']['sha256'],'payment_bindings_unchanged'=>hash('sha256',serialize($payments))===$main['source_before']['payment']['sha256'],'four_owned_templates_valid'=>$ownValid,'two_owned_stores_valid'=>count($stores)===2&&$stores[0]['store_name']==='CDXPAYTPL10_store'&&$stores[1]['store_name']==='CDXPAYTPL10_terminal','owned_template_binding_references'=>count($references),'retained_inventory'=>array_map(static function($r){unset($r['selected_config']);return $r;},$retained),'tripwire_calls'=>is_file($runtime.'/tripwire.jsonl')?count(file($runtime.'/tripwire.jsonl')):0,'queue_user_stub_calls'=>is_file(getenv('CATEGORY_ROOT').'/runtime/poc-stub-hits.log')?count(file(getenv('CATEGORY_ROOT').'/runtime/poc-stub-hits.log')):0,'tokens_deleted_in_all_runs'=>$main['cleanup']['all_tokens_absent']&&$extra['all_tokens_absent']];$report['all_passed']=$report['natural_templates_unchanged']&&$report['payment_bindings_unchanged']&&$ownValid&&$report['two_owned_stores_valid']&&$report['owned_template_binding_references']===0&&$report['tripwire_calls']===0&&$report['queue_user_stub_calls']===0&&$report['tokens_deleted_in_all_runs'];file_put_contents($runtime.'/audit.json',json_encode($report,JSON_PRETTY_PRINT));chmod($runtime.'/audit.json',0600);echo json_encode($report),"\n";exit($report['all_passed']?0:2);
