<?php
declare(strict_types=1);
// Read-only investigation of the user-supplied request; no repair or replay.
$cfg = require '/Users/wiscourper/Documents/workspace/zhct/zhctproject/store/application/config/local/database.php';
$db = new PDO('mysql:host=' . $cfg['hostname'] . ';port=' . ($cfg['hostport'] ?? 3306)
    . ';dbname=' . $cfg['database'] . ';charset=utf8mb4', $cfg['username'], $cfg['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_CASE => PDO::CASE_LOWER]);
$identity = (string)$db->query("SELECT CONCAT(@@server_uuid,'|',@@hostname,'|',@@port,'|',DATABASE())")->fetchColumn();
if (hash('sha256', $identity) !== '003eb5aecf1cd4595b609c1f7f20b9ee325b578309fb9fe14fedaf77584fa401') {
    throw new RuntimeException('Not approved development database');
}
$stmt = $db->prepare('SELECT R.DISHES_UUID,D.NAME,D.MENU_QUANTITY,COUNT(*) AS SLOT_REFERENCES,'
    . 'GROUP_CONCAT(DISTINCT R.MEAL_TIMES ORDER BY R.MEAL_TIMES) AS MEALS'
    . ' FROM ydy_recipes R LEFT JOIN ydy_dishes D ON D.UUID=R.DISHES_UUID'
    . ' WHERE R.MENU_DATE=? AND R.RESTAURANT_ID=?'
    . ' GROUP BY R.DISHES_UUID,D.NAME,D.MENU_QUANTITY ORDER BY R.DISHES_UUID');
$stmt->execute(['2026-10-10', 1]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as &$row) {
    $row['subtract_would_underflow'] = $row['menu_quantity'] !== null
        && (int)$row['menu_quantity'] < (int)$row['slot_references'];
    $all = $db->prepare('SELECT COUNT(*) FROM ydy_recipes WHERE DISHES_UUID=?');
    $all->execute([$row['dishes_uuid']]);
    $row['all_recipe_references'] = (int)$all->fetchColumn();
}
unset($row);
$origin = $db->prepare('SELECT UUID,DISHES_UUID,MEAL_TIMES,CREATE_TIME,CREATE_BY'
    . ' FROM ydy_recipes WHERE MENU_DATE=? AND RESTAURANT_ID=? ORDER BY ID');
$origin->execute(['2026-10-10', 1]);
$sources = $origin->fetchAll(PDO::FETCH_ASSOC);
foreach ($sources as &$source) {
    $creator = (string)$source['create_by'];
    $source['creator_is_codex_marker'] = strpos(strtoupper($creator), 'CDX') === 0
        || strpos(strtoupper($creator), 'CODEX') === 0 || $creator === 'DeviceApi';
    $source['creator_sha12'] = substr(hash('sha256', $creator), 0, 12);
    unset($source['create_by']);
}
unset($source);
echo json_encode(['date' => '2026-10-10', 'restaurant_id' => 1, 'readonly' => true,
    'code_local_identity_verified' => true, 'old_slot_dishes' => $rows, 'recipe_origins' => $sources], JSON_UNESCAPED_UNICODE), "\n";
