<?php
declare(strict_types=1);

// Current AI Dm/DmBuilder and installed PDO_DM. Synthetic DUAL SELECT only.
$phase = (string)($argv[1] ?? '');
if (!in_array($phase, ['--query', '--find', '--value', '--column', '--cursor'], true)) {
    fwrite(STDERR, "DENIED: exact SELECT-only ORM phase required\n");
    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);
});
$pid = 53772;
$root = '/tmp/zhct-dm-poc-20260928.fcpiiN/ai_api';
$env = [];
foreach (explode("\0", (string)file_get_contents('/proc/' . $pid . '/environ')) as $item) {
    $parts = explode('=', $item, 2);
    if (count($parts) === 2) $env[$parts[0]] = $parts[1];
}
if (($env['POC_DIALECT'] ?? '') !== 'dm'
    || ($env['POC_AI_HOST'] ?? '') !== '127.0.0.1'
    || ($env['POC_AI_PORT'] ?? '') !== '5238'
    || ($env['POC_AI_SCHEMA'] ?? '') !== 'AIZHCT_APP'
    || ($env['POC_AI_USER'] ?? '') !== 'AIZHCT_APP'
    || !in_array('dm', PDO::getAvailableDrivers(), true)) {
    throw new RuntimeException('Approved C AI worker/database drift');
}
require $root . '/vendor/autoload.php';
foreach (['app\\common\\database\\Dm' =>
        'ab5d44a9915af0f0a01186b0c72fc759075ebe9651a6c28cee40123a65218727',
    'app\\common\\database\\DmBuilder' =>
        'b8afe5f37fe7ee6307fcfc1e902bd0bcac246f7153c1dfb56fc5883c3e510f24'] as $class => $sha) {
    $file = (new ReflectionClass($class))->getFileName();
    if (!is_string($file) || hash_file('sha256', $file) !== $sha) {
        throw new RuntimeException('Actual Composer connector source drift');
    }
}
$manager = new \think\DbManager();
$manager->setConfig(['default' => 'approved_dm', 'connections' => [
    'approved_dm' => [
        'type' => '\\app\\common\\database\\Dm',
        'hostname' => '127.0.0.1', 'hostport' => 5238,
        'database' => 'AIZHCT_APP', 'username' => $env['POC_AI_USER'],
        'password' => $env['POC_AI_PASSWORD'], 'prefix' => 'yoshop_',
        'trigger_sql' => false,
    ],
]]);
$connection = $manager->connect();
$expr = "CAST(CAST('9007199254740993.01' AS DECIMAL(30,2)) AS VARCHAR(64))";
$field = $expr . " AS dec30, CAST('7' AS INT) AS int_value";
$row = null;
if ($phase === '--query') {
    $result = $connection->query('SELECT ' . $field . ' FROM DUAL');
    $row = $result[0] ?? null;
} else {
    // A known nonproduction enum row supplies ORM metadata; only synthetic constants are projected.
    $source = $connection->query('SELECT COUNT(*) AS N FROM YOSHOP_CONFIG_ENUM'
        . ' WHERE ID=6 AND ENUM_TYPE=1 AND IS_DELETE=0');
    if ((int)($source[0]['n'] ?? 0) !== 1) {
        throw new RuntimeException('Bounded enum metadata row absent');
    }
    if ($phase === '--find') {
        $row = $connection->table('YOSHOP_CONFIG_ENUM')->fieldRaw($field)
            ->where('ID', 6)->find();
    } elseif ($phase === '--value') {
        $row = ['dec30' => $connection->table('YOSHOP_CONFIG_ENUM')
            ->where('ID', 6)->value($expr)];
    } elseif ($phase === '--column') {
        $row = ['dec30' => $connection->table('YOSHOP_CONFIG_ENUM')
            ->where('ID', 6)->column($expr . ' AS dec30')];
    } else {
        $rows = iterator_to_array($connection->table('YOSHOP_CONFIG_ENUM')
            ->fieldRaw($field)->where('ID', 6)->cursor(), false);
        $row = $rows[0] ?? null;
    }
}
$exact = is_array($row) && (
    ($phase === '--column' && ($row['dec30'][0]['dec30'] ?? null) === '9007199254740993.01')
    || ($phase !== '--column' && ($row['dec30'] ?? null) === '9007199254740993.01')
);
echo json_encode(['phase' => $phase, 'exact_dec30' => $exact,
    'dec30_php_type' => gettype($row['dec30'] ?? null),
    'synthetic_dec30_value' => $row['dec30'] ?? null,
    'int_php_type' => gettype($row['int_value'] ?? null),
    'bounded_enum_row_read' => $phase !== '--query', 'business_dml' => 0]), PHP_EOL;
exit($exact ? 0 : 3);
