<?php
declare(strict_types=1);

// Current Store TP5 Dm connector, old PDO_DM; bounded synthetic SELECT only.
$phase = (string)($argv[1] ?? '');
if (!in_array($phase, ['--query', '--find', '--select', '--value', '--column'], true)) exit(78);
set_exception_handler(static function (Throwable $error) use ($phase): void {
    echo json_encode(['phase' => $phase, 'status' => 'blocked',
        'class' => get_class($error), 'code' => (string)$error->getCode(),
        'message_sha12' => substr(hash('sha256', $error->getMessage()), 0, 12)]), PHP_EOL;
    exit(2);
});
$root = '/tmp/zhct-dm-poc-20260928.fcpiiN/store';
$env = [];
foreach (explode("\0", (string)file_get_contents('/proc/112175/environ')) as $item) {
    $parts = explode('=', $item, 2);
    if (count($parts) === 2) $env[$parts[0]] = $parts[1];
}
if (($env['POC_TARGET_ROOT'] ?? '') !== $root
    || ($env['POC_HOST'] ?? '') !== '127.0.0.1'
    || ($env['POC_PORT'] ?? '') !== '5237'
    || ($env['POC_SCHEMA'] ?? '') !== 'ZHCT_APP'
    || ($env['POC_USER'] ?? '') !== 'ZHCT_APP'
    || !in_array('dm', PDO::getAvailableDrivers(), true)
    || hash_file('sha256', $root . '/application/cores/database/Dm.php')
        !== '275b64f12dd57f6fa5dd3fdb3ba2f7aeacf6b89049f81769f96b94ceed74c70d') {
    throw new RuntimeException('Approved Store connector/worker drift');
}
if (!defined('APP_PATH')) define('APP_PATH', $root . '/application/');
require_once $root . '/thinkphp/base.php';
require_once $root . '/application/cores/database/Dm.php';
$config = [
    'type' => \app\cores\database\Dm::class,
    'hostname' => '127.0.0.1', 'hostport' => 5237,
    'database' => 'ZHCT_APP', 'username' => $env['POC_USER'],
    'password' => $env['POC_PASSWORD'], 'prefix' => 'ydy_',
    'debug' => false,
    'params' => [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC],
];
$conn = \think\Db::connect($config, true);
$expr = "CAST(CAST('9007199254740993.01' AS DECIMAL(30,2)) AS VARCHAR(64))";
$field = $expr . ' AS dec30, 7 AS int_value';
$result = null;
$boundedRow = false;
if ($phase === '--query') {
    $rows = $conn->query('SELECT ' . $field . ' FROM DUAL');
    $result = $rows[0] ?? null;
} else {
    $rows = $conn->query('SELECT MIN(ID) AS N FROM YDY_USER');
    $row = array_change_key_case((array)($rows[0] ?? []), CASE_LOWER);
    $id = (int)($row['n'] ?? 0);
    if ($id <= 0) throw new RuntimeException('Bounded nonproduction user row absent');
    $boundedRow = true;
    if ($phase === '--find') {
        $result = $conn->table('ydy_user')->field($field)->where('id', $id)->find();
    } elseif ($phase === '--select') {
        $rows = $conn->table('ydy_user')->field($field)->where('id', $id)->select();
        $result = (array)($rows[0] ?? null);
    } elseif ($phase === '--value') {
        $result = ['dec30' => $conn->table('ydy_user')->where('id', $id)->value($expr)];
    } else {
        $result = ['dec30' => $conn->table('ydy_user')->where('id', $id)
            ->column($expr . ' AS dec30')];
    }
}
$result = is_array($result) ? array_change_key_case($result, CASE_LOWER) : [];
$value = $result['dec30'] ?? null;
$exact = $value === '9007199254740993.01';
if ($phase === '--column' && is_array($value)) {
    $exact = in_array('9007199254740993.01', array_values($value), true);
}
echo json_encode(['phase' => $phase, 'exact_dec30' => $exact,
    'dec30_php_type' => gettype($value), 'synthetic_dec30_value' => $value,
    'int_php_type' => gettype($result['int_value'] ?? null),
    'bounded_nonproduction_row_read' => $boundedRow, 'business_dml' => 0]), PHP_EOL;
exit($exact ? 0 : 3);
