<?php
declare(strict_types=1);

if (getenv('POC_DIALECT') !== 'mysql') exit(2);
foreach (['' => 'store', 'AI_' => 'ai_api'] as $prefix => $label) {
    $started = microtime(true);
    try {
        $host = getenv('POC_' . $prefix . 'HOST'); $port = getenv('POC_' . $prefix . 'PORT');
        $schema = getenv('POC_' . $prefix . 'SCHEMA');
        $pdo = new PDO("mysql:host=$host;port=$port;dbname=$schema;charset=utf8mb4",
            getenv('POC_' . $prefix . 'USER'), getenv('POC_' . $prefix . 'PASSWORD'),
            [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 5]);
        echo json_encode(['repo' => $label, 'connected_ms' => round((microtime(true) - $started) * 1000)]) . PHP_EOL;
        $value = $pdo->query('SELECT 1')->fetchColumn();
        echo json_encode(['repo' => $label, 'select_one' => (int)$value === 1,
            'total_ms' => round((microtime(true) - $started) * 1000)]) . PHP_EOL;
        if ($label === 'store') {
            $count = $pdo->query("SELECT COUNT(*) FROM ydy_health_analysis_report WHERE report_name LIKE 'CDXHRCRUD%'")->fetchColumn();
            echo json_encode(['repo' => $label, 'health_report_test_residuals' => (int)$count]) . PHP_EOL;
        }
    } catch (Throwable $e) {
        // Never expose the connection DSN or raw PDO message.
        echo json_encode(['repo' => $label, 'error_code' => (string)$e->getCode(),
            'elapsed_ms' => round((microtime(true) - $started) * 1000)]) . PHP_EOL;
        exit(1);
    }
}
