<?php
declare(strict_types=1);

// B local MySQL schema metadata only; no business row read or DML.
if ((string)getenv('POC_HEALTH_TARGET') !== 'B') {
    throw new RuntimeException('B target only');
}
require __DIR__ . '/ab_readonly_preflight.php';
$stage = 'meal_infos_index_metadata';
$stmt = $ai->prepare('SELECT INDEX_NAME,NON_UNIQUE,SEQ_IN_INDEX,COLUMN_NAME'
    . ' FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=?'
    . " AND TABLE_NAME='yoshop_meal_infos'"
    . ' ORDER BY INDEX_NAME,SEQ_IN_INDEX');
$stmt->execute([$config['POC_AI_SCHEMA']]);
$indexes = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    $name = (string)$row['index_name'];
    $indexes[$name]['unique'] = (int)$row['non_unique'] === 0;
    $indexes[$name]['columns'][] = strtoupper((string)$row['column_name']);
}
$sameMealDishUnique = false;
foreach ($indexes as $index) {
    if ($index['unique'] && in_array('MEAL_ID', $index['columns'], true)
        && in_array('DISHES_ID', $index['columns'], true)) {
        $sameMealDishUnique = true;
    }
}
echo json_encode(['status' => 'b-meal-infos-index-readonly',
    'indexes' => $indexes,
    'unique_index_including_meal_and_dish' => $sameMealDishUnique,
    'business_rows_read' => 0, 'writes' => 0],
    JSON_UNESCAPED_SLASHES), PHP_EOL;
