<?php
declare(strict_types=1);
$lane=$argv[1]??'';if(!in_array($lane,['B','C'],true))exit(78);$env=getenv();
if($lane==='B'){$ini=parse_ini_file('/Users/wiscourper/Documents/workspace/zhct/zhctproject/ai_api/.env.local',true,INI_SCANNER_RAW);
    foreach(['HOSTNAME'=>'HOST','HOSTPORT'=>'PORT','DATABASE'=>'SCHEMA','USERNAME'=>'USER','PASSWORD'=>'PASSWORD'] as $from=>$to){if(!isset($ini['DATABASE'][$from]))throw new RuntimeException('Incomplete local config');$env['POC_AI_'.$to]=$ini['DATABASE'][$from];}
    $env['POC_DIALECT']='mysql';$php=['/opt/homebrew/Cellar/php@7.4/7.4.33_13/bin/php'];
}else{$cmd=(string)file_get_contents('/proc/53772/cmdline');if(strpos($cmd,'127.0.0.1:18102')===false)throw new RuntimeException('C parent identity drift');
    $env=[];foreach(explode("\0",(string)file_get_contents('/proc/53772/environ')) as $entry){$p=explode('=',$entry,2);if(count($p)===2)$env[$p[0]]=$p[1];}
    if(($env['POC_AI_HOST']??'')!=='127.0.0.1'||($env['POC_AI_PORT']??'')!=='5238'||($env['POC_AI_SCHEMA']??'')!=='AIZHCT_APP')throw new RuntimeException('DM target drift');
    $php=['/www/server/php/74/bin/php','-c','/www/server/php/74/etc/php-cli.ini','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/libphp74_dm.so','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/php74_pdo_dm.so'];
}
$env['META_LANE']=$lane;$process=proc_open(array_merge($php,[__DIR__.'/read_metadata.php']),[0=>STDIN,1=>['pipe','w'],2=>STDERR],$pipes,null,$env);$raw=stream_get_contents($pipes[1]);fclose($pipes[1]);$code=proc_close($process);$data=json_decode($raw,true);if($code!==0||!is_array($data)||$data['lane']!==$lane)throw new RuntimeException('Metadata gate failed');file_put_contents(__DIR__.'/structure-'.$lane.'.json',$raw);echo json_encode(['lane'=>$lane,'FK'=>$data['foreign_keys'],'triggers'=>$data['triggers'],'delivery_columns'=>count($data['metadata']['delivery']['columns']),'rule_columns'=>count($data['metadata']['delivery_rule']['columns']),'store_collisions'=>array_map(static function($v){return $v['synthetic_store_collision_rows'];},$data['metadata'])]),"\n";
