<?php
declare(strict_types=1);

// Local read-only runtime identity check. Never print process env or credentials.
$target = (string)($argv[1] ?? '');
$targets = [
    'A' => [
        'pid' => 85930, 'port' => 18105,
        'root' => '/private/tmp/zhct-http-ai-macos.zjaU8h',
        'source' => '/Users/wiscourper/Documents/workspace/local/'
            . 'dm8-full-acceptance.fF5yTy/baseline-a-ai-api-V4.0.40',
        'restaurant_sha' => '11ef8be01a99a64efb64fceadd2b73b4d855c4f62ebada814245ab079d458f6e',
    ],
    'B' => [
        'pid' => 10732, 'port' => 18101,
        'root' => '/private/tmp/zhct-http-ai-macos.x2CCZX',
        'source' => '/Users/wiscourper/Documents/workspace/local/'
            . 'dm8-full-acceptance.fF5yTy/ai_api',
        'restaurant_sha' => '7d91cc13b226732a3f53293b97a18384136826a3aab27e7dc8715e29cf28d9fc',
    ],
];
if (!isset($targets[$target])) throw new RuntimeException('Explicit A/B target required');
$spec = $targets[$target];
$envPath = '/Users/wiscourper/Documents/workspace/zhct/zhctproject/ai_api/.env.local';
$local = parse_ini_file($envPath, true, INI_SCANNER_RAW);
if (!is_array($local) || !isset($local['DATABASE'], $local['DATABASE_ZHCT'])) {
    throw new RuntimeException('Local development config unavailable');
}
$command = shell_exec('/bin/ps eww -p ' . $spec['pid'] . ' -o command=');
if (!is_string($command) || $command === '') {
    throw new RuntimeException('Isolated A/B process unavailable');
}
$hasEnv = static function (string $key, string $value) use ($command): bool {
    if ($value === '' || preg_match('/\s/', $value)) return false;
    return preg_match('/(?:^|\s)' . preg_quote($key . '=' . $value, '/')
        . '(?=\s|$)/', $command) === 1;
};
$matchesConfig = static function (array $source, string $prefix) use ($hasEnv): bool {
    foreach (['HOSTNAME' => 'HOST', 'HOSTPORT' => 'PORT',
        'DATABASE' => 'SCHEMA', 'USERNAME' => 'USER',
        'PASSWORD' => 'PASSWORD'] as $from => $to) {
        if (!isset($source[$from]) || !$hasEnv($prefix . $to, (string)$source[$from])) {
            return false;
        }
    }
    return true;
};
$runtime = realpath($spec['root']);
$source = realpath($spec['source']);
$actualSource = realpath($spec['root'] . '/app/api/model/Restaurant.php');
$expectedSource = realpath($spec['source'] . '/app/api/model/Restaurant.php');
$displayRoot = str_replace('/private/tmp/', '/tmp/', $spec['root']);
$checks = [
    'listener_root' => strpos($command, '-S 127.0.0.1:' . $spec['port']) !== false
        && (strpos($command, $spec['root'] . '/public') !== false
            || strpos($command, $displayRoot . '/public') !== false),
    'root_not_symlink' => $runtime === $spec['root'],
    'actual_source' => $actualSource !== false
        && $actualSource === $expectedSource && $source !== false,
    'restaurant_sha' => $actualSource !== false
        && hash_file('sha256', $actualSource) === $spec['restaurant_sha'],
    'main_local_dev_config' => $matchesConfig($local['DATABASE'], 'POC_AI_'),
    'store_named_local_dev_config' => $matchesConfig($local['DATABASE_ZHCT'], 'POC_'),
];
$pass = !in_array(false, $checks, true);
echo json_encode(['status' => $pass ? 'ab-runtime-boundary-pass'
    : 'ab-runtime-boundary-fail', 'target' => $target,
    'checks' => $checks], JSON_UNESCAPED_SLASHES), PHP_EOL;
exit($pass ? 0 : 3);
