<?php
declare(strict_types=1);
$lane=getenv('COMMENT_LANE');$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='.getenv('POC_AI_CHARSET'),getenv('POC_AI_USER'),getenv('POC_AI_PASSWORD'),[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::ATTR_CASE=>PDO::CASE_LOWER]);
if($dm?$db->query("SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL")->fetchColumn()!=='AIZHCT_APP':($db->query('SELECT DATABASE()')->fetchColumn()!==getenv('POC_AI_SCHEMA')||strpos($db->query('SELECT VERSION()')->fetchColumn(),'5.6.')!==0))throw new RuntimeException('DB identity');
$q=static function($sql,$p=[])use($db){$s=$db->prepare($sql);$s->execute($p);$r=$s->fetchAll(PDO::FETCH_ASSOC);foreach($r as &$row){foreach($row as &$v)if(is_resource($v))$v=stream_get_contents($v);ksort($row);}return $r;};
$pk=$lane==='B'?['goods'=>10023,'file'=>24452,'user'=>46913]:['goods'=>12,'file'=>9715,'user'=>1927];
$raw=['goods'=>$q('SELECT * FROM yoshop_goods WHERE goods_id=? AND store_id=2147001112',[$pk['goods']]),'upload_file'=>$q('SELECT * FROM yoshop_upload_file WHERE file_id=? AND store_id=2147001112',[$pk['file']]),'user'=>$q('SELECT * FROM yoshop_user WHERE user_id=? AND store_id=2147001112',[$pk['user']])];
if(count($raw['goods'])!==1||$raw['goods'][0]['goods_no']!=='CDXCM11F782_terminal'||(int)$raw['goods'][0]['is_delete']!==1||(int)$raw['goods'][0]['status']!==20||(int)$raw['goods'][0]['stock_total']!==0||count($raw['upload_file'])!==1||count($raw['user'])!==1)throw new RuntimeException('Own source drift');
$j=json_decode(file_get_contents('/tmp/cdx-ai-order-related-read8-20261011/private/evidence/fixture-journal.json'),true);
foreach(['goods'=>'goods_id','upload_file'=>'file_id','user'=>'user_id'] as $table=>$key){$prior=array_values(array_filter($j['after'][$table],static function($row)use($raw,$table,$key){return $row[$key]===$raw[$table][0][$key];}));if($prior!==$raw[$table])throw new RuntimeException('Sealed source bytes changed '.$table);}
$settingKey=$dm?'"KEY"':'`key`';$settingValue=$dm?'"VALUES"':'`values`';
$settings=$q('SELECT store_id,'.$settingValue.' cfg FROM yoshop_store_setting WHERE '.$settingKey.'=? AND store_id IN (0,2147001111,2147001112) ORDER BY store_id',['printer']);
$defaults=[];foreach($settings as $s){$cfg=json_decode($s['cfg'],true);if(!is_array($cfg))throw new RuntimeException('Printer config malformed');$defaults[]=['store_id'=>(int)$s['store_id'],'is_open'=>$cfg['is_open']??null,'printer_id'=>$cfg['printer_id']??null,'order_status'=>$cfg['order_status']??null,'row_sha256'=>hash('sha256',json_encode($s))];if((int)($cfg['printer_id']??0)!==0)throw new RuntimeException('Default actual printer link');}
$collisions=['sku'=>(int)$q('SELECT COUNT(*) n FROM yoshop_goods_sku WHERE goods_id=?',[$pk['goods']])[0]['n'],'image'=>(int)$q('SELECT COUNT(*) n FROM yoshop_goods_image WHERE goods_id=?',[$pk['goods']])[0]['n'],'printer'=>(int)$q('SELECT COUNT(*) n FROM yoshop_printer')[0]['n']];
if(array_sum($collisions)!==0)throw new RuntimeException('Fixture collision');
file_put_contents(__DIR__.'/private/inspect-raw.json',json_encode(['raw'=>$raw,'defaults'=>$settings,'prior_journal_sha256'=>hash_file('sha256','/tmp/cdx-ai-order-related-read8-20261011/private/evidence/fixture-journal.json')],JSON_PRETTY_PRINT|JSON_PRESERVE_ZERO_FRACTION));chmod(__DIR__.'/private/inspect-raw.json',0600);
$out=['lane'=>$lane,'identity_verified'=>true,'business_dml'=>0,'pk'=>$pk,'goods_sha256'=>hash('sha256',json_encode($raw['goods'][0],JSON_PRESERVE_ZERO_FRACTION)),'goods'=>$raw['goods'][0],'file_marker'=>array_intersect_key($raw['upload_file'][0],array_flip(['file_id','file_name','file_path','storage','is_delete','store_id'])),'user_sha256'=>hash('sha256',json_encode($raw['user'][0],JSON_PRESERVE_ZERO_FRACTION)),'collisions'=>$collisions,'printer_defaults'=>$defaults];
file_put_contents(__DIR__.'/inspect-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_PRESERVE_ZERO_FRACTION));echo json_encode(['lane'=>$lane,'identity_verified'=>true,'business_dml'=>0,'goods_sha256'=>$out['goods_sha256'],'collisions'=>$collisions,'printer_defaults'=>$defaults]),"\n";
