<?php
declare(strict_types=1);

// Nonproduction C schema metadata only; no business row read or DML.
require __DIR__ . '/c_readonly_preflight.php';
$stage = 'meal_infos_index_metadata';
$sql = 'SELECT i.INDEX_NAME,i.UNIQUENESS,c.COLUMN_NAME,c.COLUMN_POSITION'
    . ' FROM USER_INDEXES i JOIN USER_IND_COLUMNS c'
    . ' ON i.INDEX_NAME=c.INDEX_NAME AND i.TABLE_NAME=c.TABLE_NAME'
    . " WHERE i.TABLE_NAME='YOSHOP_MEAL_INFOS'"
    . ' ORDER BY i.INDEX_NAME,c.COLUMN_POSITION';
$rows = $ai->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$indexes = [];
foreach ($rows as $row) {
    $row = array_change_key_case($row, CASE_UPPER);
    $name = strtoupper((string)$row['INDEX_NAME']);
    $indexes[$name]['unique'] = strtoupper((string)$row['UNIQUENESS']);
    $indexes[$name]['columns'][] = strtoupper((string)$row['COLUMN_NAME']);
}
$sameMealDishUnique = false;
foreach ($indexes as $index) {
    $unique = in_array($index['unique'], ['UNIQUE', 'Y', '1'], true);
    if ($unique && in_array('MEAL_ID', $index['columns'], true)
        && in_array('DISHES_ID', $index['columns'], true)) {
        $sameMealDishUnique = true;
    }
}
echo json_encode(['status' => 'c-meal-infos-index-readonly',
    'indexes' => $indexes,
    'unique_index_including_meal_and_dish' => $sameMealDishUnique,
    'business_rows_read' => 0, 'writes' => 0],
    JSON_UNESCAPED_SLASHES), PHP_EOL;
