<?php

declare(strict_types=1);

$runtime = '/tmp/zhct-http-ai-macos.x2CCZX';
$port = (int)(getenv('POC_LISTEN_PORT') ?: 18111);
if (!in_array($port, [18101, 18111], true)) {
    throw new RuntimeException('Only the approved loopback B test ports are allowed');
}
$configFile = '/Users/wiscourper/Documents/workspace/zhct/zhctproject/ai_api/.env.local';
$config = parse_ini_file($configFile, true, INI_SCANNER_RAW);
if (!is_array($config) || !isset($config['DATABASE'], $config['DATABASE_ZHCT'])
    || !is_file($runtime . '/public/index.php')) {
    throw new RuntimeException('Selected local test runtime/config unavailable');
}
foreach (['DATABASE' => 'AI_', 'DATABASE_ZHCT' => ''] as $section => $prefix) {
    foreach (['HOSTNAME' => 'HOST', 'HOSTPORT' => 'PORT', 'DATABASE' => 'SCHEMA',
        'USERNAME' => 'USER', 'PASSWORD' => 'PASSWORD'] as $from => $to) {
        putenv('POC_' . $prefix . $to . '=' . (string)$config[$section][$from]);
    }
}
putenv('POC_DIALECT=mysql');
putenv('POC_EXTERNAL_STUBS=1');
putenv('POC_TARGET_ROOT=' . $runtime);
$args = ['-S', '127.0.0.1:' . $port, '-t', $runtime . '/public',
    $runtime . '/public/index.php'];
pcntl_exec(PHP_BINARY, $args);
throw new RuntimeException('Unable to launch isolated B HTTP test runtime');
