<?php
define('CDX_MS_CONTEXT',true);require __DIR__.'/preflight.php';$base=json_decode((string)file_get_contents(__DIR__.'/baseline-audit.json'),true);$ownIds=$lane==='B'?[109,110]:[6,7];$original=$base[$lane];$checks=[];$evidence=[];
foreach($tables as $table){$key=$table==='marketing_strategy'?'ID':'STRATEGY_ID';$field='*';if($lane==='C'&&$table==='marketing_strategy'){$names=array_map(static function($v){return strtoupper($v['column_name']);},$base['C_original_main']['original_columns']);$field=implode(',',array_map(static function($s){return '"'.$s.'"';},$names));}$rows=$q('SELECT '.$field.' FROM '.$p.$table.' WHERE '.$key.' NOT IN (?,?) ORDER BY ID',$ownIds);$sha=hash('sha256',serialize($rows));$expected=$lane==='C'&&$table==='marketing_strategy'?$base['C_original_main']['original_projection_sha256']:$original['tables'][$table]['sha256'];$checks[$table.'_natural_unchanged']=$sha===$expected;$evidence[$table]=['rows'=>count($rows),'sha256'=>$sha,'original_sha256'=>$expected];}
$inventory=[];foreach($ownIds as $id){$row=$q('SELECT ID,STRATEGY_NAME,CREATE_BY,STATUS,IS_DELETE,EFFECTIVE_START,EFFECTIVE_END FROM '.$p.'marketing_strategy WHERE ID=?',[$id]);$ok=count($row)===1&&strpos($row[0]['strategy_name'],'CDXMS10'.$lane)===0&&$row[0]['create_by']==='CDXMS10ADMIN'&&(int)$row[0]['status']===0&&(int)$row[0]['is_delete']===1&&substr($row[0]['effective_start'],0,10)==='2010-01-01'&&substr($row[0]['effective_end'],0,10)==='2010-01-02';$counts=[];foreach(array_slice($tables,1) as $t){$counts[$t]=count($q('SELECT ID FROM '.$p.$t.' WHERE STRATEGY_ID=?',[$id]));$ok=$ok&&$counts[$t]===($t==='marketing_strategy_mealtimes'?1:0);}$checks['owned_'.$id.'_retained']=$ok;$inventory[]=['id'=>$id,'marker'=>$row[0]['strategy_name']??null,'status'=>0,'is_delete'=>1,'relation_counts'=>$counts];}
$ref=json_decode((string)file_get_contents(__DIR__.'/private-reference.json'),true);$checks['department_unchanged']=$q('SELECT UUID,NAME FROM '.$p.'department WHERE UUID=?',[$ref['department']['uuid']])===[$ref['department']];$checks['identity_unchanged']=$q('SELECT ID,IDENTITY_TYPE FROM '.$p.'identity_type WHERE ID=?',[(int)$ref['identity']['id']])===[$ref['identity']];$checks['own_stall_unchanged']=$q('SELECT ID,UUID,NAME,CREATE_BY,STALL_TYPE FROM '.$p.'restaurants WHERE ID=?',[(int)$ref['stall']['id']])===[$ref['stall']];
$out=['lane'=>$lane,'independent_new_connection'=>true,'business_dml'=>0,'ddl'=>0,'all_passed'=>!in_array(false,$checks,true),'checks'=>$checks,'natural_original_projection'=>$evidence,'retained_inventory'=>$inventory,'source_sha256'=>['controller'=>hash_file('sha256',__DIR__.'/source/application/api/controller/MarketingStrategy.php'),'service'=>hash_file('sha256',__DIR__.'/source/application/api/service/MarketingStrategy.php')]];file_put_contents(__DIR__.'/audit-private.json',json_encode($out,JSON_PRETTY_PRINT));chmod(__DIR__.'/audit-private.json',0600);echo json_encode($out),"\n";
