<?php

declare(strict_types=1);

$configPath = getenv('POC_STORE_LOCAL_CONFIG');
$fixturePath = getenv('POC_PAYMENT_FIXTURE');
if (!$configPath || !is_file($configPath)
    || !$fixturePath || !is_file($fixturePath)) {
    exit(2);
}
$config = require $configPath;
if (!is_array($config) || !isset($config['ai_zhct'])) exit(2);
$maps = [
    'HOST' => ['hostname', $config],
    'PORT' => ['hostport', $config],
    'SCHEMA' => ['database', $config],
    'USER' => ['username', $config],
    'PASSWORD' => ['password', $config],
    'AI_HOST' => ['hostname', $config['ai_zhct']],
    'AI_PORT' => ['hostport', $config['ai_zhct']],
    'AI_SCHEMA' => ['database', $config['ai_zhct']],
    'AI_USER' => ['username', $config['ai_zhct']],
    'AI_PASSWORD' => ['password', $config['ai_zhct']],
];
foreach ($maps as $target => [$key, $source]) {
    $value = $source[$key] ?? ($key === 'hostport' ? '3306' : '');
    if ($value === '') exit(2);
    putenv('POC_' . $target . '=' . $value);
}
putenv('POC_DIALECT=mysql');
require $fixturePath;
