<?php

declare(strict_types=1);

// Read-only constant expression; no table, DDL, DML, license or credential output.
$root = (string)getenv('POC_APP_ROOT');
if (getenv('POC_DIALECT') !== 'dm' || strpos(realpath($root) ?: '', '/tmp/zhct-') !== 0
    || !is_file($root . '/application/database.php')) {
    throw new RuntimeException('Only isolated DM runtime is allowed');
}
$config = require $root . '/application/database.php';
$sql = "SELECT CAST('9007199254740993.01' AS DECIMAL(30,2)) AS V FROM DUAL";
$expected = '9007199254740993.01';
$dsn = 'dm:host=' . $config['hostname'] . ';port=' . $config['hostport']
    . ';schema=' . $config['database'];
$modes = [];
foreach ([false, true] as $stringify) {
    $pdo = new PDO($dsn, $config['username'], $config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_STRINGIFY_FETCHES => $stringify,
    ]);
    $stmt = $pdo->query($sql);
    $meta = null;
    try { $meta = $stmt->getColumnMeta(0); } catch (Throwable $ignored) {}
    $value = $stmt->fetchColumn();
    $modes[$stringify ? 'pdo_stringify_true' : 'pdo_stringify_false'] = [
        'php_type' => gettype($value), 'value' => (string)$value,
        'exact' => is_string($value) && $value === $expected,
        'column_meta_native_type' => is_array($meta) ? ($meta['native_type'] ?? null) : null,
    ];
}
$pdo = new PDO($dsn, $config['username'], $config['password'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$stmt = $pdo->prepare($sql);
$stmt->execute();
$bound = null;
$stmt->bindColumn(1, $bound, PDO::PARAM_STR);
$stmt->fetch(PDO::FETCH_BOUND);
$modes['pdo_bound_string'] = ['php_type' => gettype($bound), 'value' => (string)$bound,
    'exact' => is_string($bound) && $bound === $expected];
$varcharSql = "SELECT CAST(CAST('9007199254740993.01' AS DECIMAL(30,2)) AS VARCHAR(64)) AS V FROM DUAL";
$varcharValue = $pdo->query($varcharSql)->fetchColumn();
$modes['pdo_server_cast_varchar'] = ['php_type' => gettype($varcharValue),
    'value' => (string)$varcharValue,
    'exact' => is_string($varcharValue) && $varcharValue === $expected];

$modes['native_dm_fetch_array'] = ['available' => function_exists('dm_connect')
    && function_exists('dm_exec') && function_exists('dm_fetch_array')];
if ($modes['native_dm_fetch_array']['available']) {
    $server = $config['hostname'] . ':' . $config['hostport'] . ':' . $config['database'];
    $link = dm_connect($server, $config['username'], $config['password']);
    if ($link !== false) {
        $result = dm_exec($link, $sql);
        if ($result !== false) {
            $row = dm_fetch_array($result);
            $value = is_array($row) ? (array_change_key_case($row, CASE_LOWER)['v'] ?? null) : null;
            $modes['native_dm_fetch_array'] += [
                'php_type' => gettype($value), 'value' => (string)$value,
                'exact' => is_string($value) && $value === $expected,
            ];
        } else $modes['native_dm_fetch_array']['query_failed'] = true;
        dm_close($link);
    } else $modes['native_dm_fetch_array']['connect_failed'] = true;
}
echo json_encode([
    'php_version' => PHP_VERSION,
    'pdo_dm_module_version' => phpversion('PDO_DM'),
    'native_dm_module_version' => phpversion('DM'),
    'dm_fetch_number_ini' => ini_get('dm.fetch_number'),
    'pdo_extra_decimal_constant' => defined('PDO::EXTRA_DECIMAL'),
    'expected' => $expected,
    'modes' => $modes,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
