<?php
declare(strict_types=1);

// User-approved local-development MySQL metadata and pure constants only.
if ((string)getenv('POC_HEALTH_TARGET') !== 'B') {
    throw new RuntimeException('B current runtime only');
}
require __DIR__ . '/../parallel-ai-health-read-batch/ab_readonly_preflight.php';
$stage = 'catalog_numeric_column_metadata';
$wanted = [
    ['ai', $ai, $config['POC_AI_SCHEMA'], 'yoshop_dishes',
        ['weight', 'energy', 'carbohydrate', 'protein', 'fat']],
    ['store', $store, $config['POC_SCHEMA'], 'ydy_dishes', ['price']],
];
$result = [];
foreach ($wanted as [$side, $pdo, $schema, $table, $columns]) {
    $stmt = $pdo->prepare('SELECT DATA_TYPE,NUMERIC_PRECISION,NUMERIC_SCALE,'
        . 'IS_NULLABLE FROM information_schema.COLUMNS'
        . ' WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=?');
    foreach ($columns as $column) {
        $stmt->execute([$schema, $table, $column]);
        $meta = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!is_array($meta)) throw new RuntimeException('Catalog column unavailable');
        $result[$side][$column] = [
            'type' => strtolower((string)$meta['data_type']),
            'precision' => (int)$meta['numeric_precision'],
            'scale' => (int)$meta['numeric_scale'],
            'nullable' => (string)$meta['is_nullable'],
        ];
    }
}
$stage = 'pure_numeric_cast_types';
$decimal = $ai->query("SELECT CAST('123.45' AS DECIMAL(10,2))")
    ->fetchColumn();
$float = $store->query("SELECT CAST('123.45' AS DECIMAL(10,2)) + 0e0")
    ->fetchColumn();
echo json_encode(['status' => 'mysql-catalog-numeric-metadata',
    'columns' => $result,
    'pure_decimal_php_type' => gettype($decimal),
    'pure_float_like_php_type' => gettype($float),
    'catalog_row_values_read' => 0, 'writes' => 0],
    JSON_UNESCAPED_SLASHES), PHP_EOL;
