<?php

declare(strict_types=1);

$dialect = getenv('POC_DIALECT');
$host = getenv('POC_HOST');
$port = (int)getenv('POC_PORT');
$schema = getenv('POC_SCHEMA');
$user = getenv('POC_USER');
$password = getenv('POC_PASSWORD');
$storeId = (int)getenv('POC_STORE_ID');
if (!in_array($dialect, ['mysql', 'dm'], true) || !$host || $port < 1 || !$schema || !$user || !$password || $storeId < 900000000) exit(2);
$dsn = $dialect === 'dm' ? "dm:host=$host;port=$port;schema=$schema" : "mysql:host=$host;port=$port;dbname=$schema;charset=utf8mb4";
$pdo = new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$counts = [];
foreach (['USER', 'RECHARGE_GIFT_RULE', 'RECHARGE_ORDER', 'ACCOUNT_SETTLEMENT', 'USER_BALANCE_LOG', 'USER_SUBSIDY_LOG', 'USER_GIFT_LOG'] as $table) {
    $tableName = $dialect === 'dm' ? 'YOSHOP_' . $table : 'yoshop_' . strtolower($table);
    $stmt = $pdo->prepare('SELECT COUNT(*) FROM ' . $tableName . ' WHERE STORE_ID=?');
    $stmt->execute([$storeId]);
    $counts[$table] = (int)$stmt->fetchColumn();
}
echo json_encode(['dialect' => $dialect, 'store_id' => $storeId, 'counts' => $counts]) . PHP_EOL;
if (array_sum($counts) !== 0) exit(1);
