<?php
declare(strict_types=1);
define('CDX_MS_CONTEXT',true);require __DIR__.'/preflight.php';
$out=['lane'=>$lane,'phase'=>'natural-read-before-schema-repair','database_writes'=>0,'routes'=>[]];$reference=json_decode((string)file_get_contents(__DIR__.'/private-reference.json'),true);$source=$q('SELECT * FROM '.$p.'marketing_strategy WHERE ID=(SELECT MIN(ID) FROM '.$p.'marketing_strategy WHERE IS_DELETE=0 AND STORE_ID=10001)');$check(count($source)===1,'Natural in-scope marketing source exists');$selected=$source[0];$admin=$q('SELECT ID,UUID,TYPE FROM '.$p.'user WHERE ID=(SELECT MIN(ID) FROM '.$p.'user WHERE TYPE=0 AND IS_DELETE=0)')[0];require_once $root.'/application/base/Security.php';$token=\app\base\Security::encrypt(implode("\t",[(string)$admin['id'],(string)$admin['uuid'],'CDXMS10READ','0','','','CDXMS10READ']));
$shape=static function($v)use(&$shape){if(is_object($v)){$r=[];foreach(get_object_vars($v) as $k=>$x)$r[$k]=$shape($x);ksort($r);return ['object'=>$r];}return is_array($v)?['array'=>array_map($shape,$v)]:(is_int($v)||is_float($v)?'number':gettype($v));};
$rq=static function(string $action,array $params=[])use($lane,$token):array{$raw=@file_get_contents('http://127.0.0.1:'.($lane==='B'?18301:18302).'/api/marketingStrategy/'.$action,false,stream_context_create(['http'=>['method'=>'POST','header'=>'token: '.$token."\r\nContent-Type: application/json\r\n",'content'=>json_encode($params),'ignore_errors'=>true,'timeout'=>30]]));preg_match('#^HTTP/\S+\s+(\d+)#',$http_response_header[0]??'',$m);return [(int)($m[1]??0),json_decode((string)$raw,true),json_decode((string)$raw)];};
foreach(['lists'=>['strategyName'=>$selected['strategy_name'],'page'=>1,'pageSize'=>5],'select'=>[],'detail'=>['id'=>(int)$selected['id']]] as $action=>$params){[$http,$body,$wire]=$rq($action,$params);$item=['method'=>'POST','path'=>'/api/marketingStrategy/'.$action,'http_status'=>$http,'code'=>$body['code']??null,'wire_shape'=>$shape($wire)];if(($body['code']??null)===0){$d=$body['data'];$item['rows']=$action==='lists'?count($d['data']):($action==='select'?count($d):1);$item['source_column_count']=count($selected);if($action==='detail'){$mismatch=[];foreach($selected as $key=>$value){$actual=$d[$key]??null;$same=$actual===$value;if(!$same&&is_numeric($actual)&&is_numeric($value))$same=bccomp((string)$actual,(string)$value,8)===0;if(!$same)$mismatch[]=$key;}$item['all_original_fields_present']=count(array_diff(array_keys($selected),array_keys($d)))===0;$item['source_mismatch_fields']=$mismatch;$item['strategy_value_type']=gettype($d['strategy_value']);$item['create_time_length']=strlen($d['create_time']);$item['update_time_length']=strlen($d['update_time']);$item['consume_scene_type_present']=array_key_exists('consume_scene_type',$d);}$item['positive_read']=true;}$out['routes'][$action]=$item;}
$out['natural_seven_tables_unchanged']=true;foreach($tables as $t){$rows=$q('SELECT * FROM '.$p.$t.' ORDER BY ID');$out['natural_seven_tables_unchanged']=$out['natural_seven_tables_unchanged']&&hash('sha256',serialize($rows))===$report['tables'][$t]['sha256'];}
file_put_contents(__DIR__.'/readonly-result-private.json',json_encode($out,JSON_PRETTY_PRINT));chmod(__DIR__.'/readonly-result-private.json',0600);echo json_encode($out),"\n";
