<?php

declare(strict_types=1);

// C-only, SELECT-only structure gate. Does not create a fixture.
if ((string)getenv('CDX_NEXT5_LANE') !== 'C' || ($argv[1] ?? '') !== '--preflight') {
    fwrite(STDERR, "C nutrition fixture probe disabled\n");
    exit(78);
}
$preflight = '/tmp/zhct-next5-readonly-20261004/preflight_next5.php';
if (!is_file($preflight) || is_link($preflight)
    || hash_file('sha256', $preflight)
        !== '8f262f3645d6c7e082dc5a2d7c32d893d32eb91a3dd2365f7d88cec0db25c4d6') {
    throw new RuntimeException('Frozen C boundary gate absent/drifted');
}
require $preflight;
$tables = ['YDY_NUTRITION_CONSUME', 'YDY_NUTRITION_STAFF',
    'YDY_NUTRITION_SCHEME', 'YDY_NUTRITION_SCHEME_EXT',
    'YDY_NUTRITION', 'YDY_NUTRITION_SET', 'YDY_STAFF'];
$out = [];
foreach ($tables as $name) {
    $columns = $db->prepare('SELECT COLUMN_NAME,DATA_TYPE,NULLABLE,DATA_DEFAULT'
        . ' FROM USER_TAB_COLUMNS WHERE TABLE_NAME=? ORDER BY COLUMN_ID');
    $columns->execute([$name]);
    $required = [];
    $columnNames = [];
    $nullable = [];
    foreach ($columns->fetchAll(PDO::FETCH_ASSOC) as $raw) {
        $row = array_change_key_case($raw, CASE_LOWER);
        $column = (string)$row['column_name'];
        $columnNames[] = $column;
        $default = $row['data_default'];
        if (is_resource($default)) $default = stream_get_contents($default);
        if ((string)$row['nullable'] === 'N' && ($default === null || trim((string)$default) === '')) {
            $required[] = [$column, (string)$row['data_type']];
        }
        if ((string)$row['nullable'] === 'Y') $nullable[] = $column;
    }
    $constraints = $db->prepare('SELECT CONSTRAINT_NAME,CONSTRAINT_TYPE,R_OWNER,R_CONSTRAINT_NAME'
        . ' FROM USER_CONSTRAINTS WHERE TABLE_NAME=? ORDER BY CONSTRAINT_NAME');
    $constraints->execute([$name]);
    $constraintTypes = [];
    foreach ($constraints->fetchAll(PDO::FETCH_ASSOC) as $raw) {
        $row = array_change_key_case($raw, CASE_LOWER);
        $type = (string)$row['constraint_type'];
        $constraintTypes[$type] = ($constraintTypes[$type] ?? 0) + 1;
    }
    $trigger = $db->prepare('SELECT COUNT(*) FROM USER_TRIGGERS WHERE TABLE_NAME=?');
    $trigger->execute([$name]);
    $count = (int)$db->query('SELECT COUNT(*) FROM ' . $name)->fetchColumn();
    $definition = $db->prepare('CALL SP_TABLEDEF(?, ?)');
    $definition->execute(['ZHCT_APP', $name]);
    $ddl = $definition->fetchColumn();
    if (is_resource($ddl)) $ddl = stream_get_contents($ddl);
    $idAuto = preg_match('/"ID"\s+[A-Z0-9]+\s+AUTO_INCREMENT\b/i', (string)$ddl) === 1;
    $out[$name] = ['rows' => $count, 'column_count' => count($columnNames),
        'required_no_default' => $required, 'constraint_types' => $constraintTypes,
        'nullable_columns' => $nullable,
        'id_auto_increment' => $idAuto,
        'trigger_count' => (int)$trigger->fetchColumn()];
}
echo json_encode(['lane' => 'C', 'source' => $out, 'business_http' => 0,
    'dml' => 0], JSON_UNESCAPED_SLASHES), PHP_EOL;
