<?php

declare(strict_types=1);

// REVIEW DRAFT. No DB/HTTP access without approval of this exact launcher SHA.
if ((string)getenv('CDXARTICLE_AB_LAUNCH') !== 'ROOT_APPROVED_EXACT_SHA'
    || ($argv[1] ?? '') !== '--execute-article-ab') {
    fwrite(STDERR, "Review draft; launcher disabled\n");
    exit(2);
}

$lanes = [
    'A' => [
        'root' => '/tmp/zhct-http-store-a.l5omH8',
        'base' => 'http://127.0.0.1:18097',
        'pid' => '21023',
    ],
    'B' => [
        'root' => '/tmp/zhct-http-store-mysql.dQTTBP',
        'base' => 'http://127.0.0.1:18081',
        'pid' => '10001',
    ],
];
$lane = (string)getenv('CDXARTICLE_AB_LANE');
if (!isset($lanes[$lane])) {
    fwrite(STDERR, "ARTICLE2_LAUNCHER_PRECHECK_FAILED lane\n");
    exit(2);
}

$articleScript = __DIR__ . '/draft_article_anon_read2.php';
$localConfig = '/workspace/zhct/zhctproject/store/application/config/local/database.php';
try {
    if (!is_file($articleScript) || is_link($articleScript)
        || hash_file('sha256', $articleScript)
            !== '55f1faf5e6cfcd650c5a44f66a6e610b6a126b5ecab12efd3b39bb45643d7757'
        || !is_file($localConfig) || is_link($localConfig)) {
        throw new RuntimeException('Reviewed Article2 or code-local file missing');
    }
    $config = require $localConfig;
    if (!is_array($config)) {
        throw new RuntimeException('Code-local MySQL configuration required');
    }
    foreach (['hostname', 'database', 'username', 'password'] as $field) {
        if (!isset($config[$field]) || (string)$config[$field] === '') {
            throw new RuntimeException('Incomplete code-local MySQL configuration');
        }
    }
    $port = (int)($config['hostport'] ?? 3306);
    if ($port < 1 || $port > 65535) {
        throw new RuntimeException('Invalid code-local MySQL port');
    }
    $dbEnv = [
        'POC_HOST' => (string)$config['hostname'],
        'POC_PORT' => (string)$port,
        'POC_SCHEMA' => (string)$config['database'],
        'POC_USER' => (string)$config['username'],
        'POC_PASSWORD' => (string)$config['password'],
    ];
    foreach ($dbEnv as $name => $value) {
        putenv($name . '=' . $value);
    }
    putenv('POC_DIALECT=mysql');
    putenv('POC_APP_ROOT=' . $lanes[$lane]['root']);
    putenv('POC_BASE_URL=' . $lanes[$lane]['base']);
    putenv('POC_RUNTIME_PID=' . $lanes[$lane]['pid']);
    putenv('CDXARTICLE2_LANE=' . $lane);
    putenv('CDXARTICLE2_SOURCE_DIGEST=5b83511bc18baaea05976b83f797e2fedb32114ac62e3a76812283a975d000e1');
    putenv('CDXARTICLE2_TOKEN');
    putenv('HTTP_COOKIE');
    $_COOKIE = [];
    putenv('CDXARTICLE2_EXECUTE=ROOT_APPROVED_EXACT_SHA');
} catch (Throwable $error) {
    fwrite(STDERR, 'ARTICLE2_LAUNCHER_PRECHECK_FAILED '
        . get_class($error) . ' code=' . (string)$error->getCode() . PHP_EOL);
    exit(2);
}

// Intentionally outside launcher try/catch: Article2's own exception handler
// must distinguish its runtime gate/DB/HTTP failure from launcher preflight.
$argv = [$articleScript, '--execute-article2'];
require $articleScript;
