<?php
declare(strict_types=1);

// Nonproduction C metadata and aggregate natural-catalog counts only.
require __DIR__ . '/../parallel-ai-health-read-batch/c_readonly_preflight.php';
$stage = 'candidate_table_access';
$aiTables = [
    'dishes_cate' => 'YOSHOP_DISHES_CATE',
    'dishes' => 'YOSHOP_DISHES',
    'dishes_tag' => 'YOSHOP_DISHES_TAG',
    'upload_file' => 'YOSHOP_UPLOAD_FILE',
    'meal_cart' => 'YOSHOP_MEAL_CART',
    'user' => 'YOSHOP_USER',
    'user_subsidy' => 'YOSHOP_USER_SUBSIDY',
    'user_balance_log' => 'YOSHOP_USER_BALANCE_LOG',
    'user_subsidy_log' => 'YOSHOP_USER_SUBSIDY_LOG',
    'user_gift_log' => 'YOSHOP_USER_GIFT_LOG',
    'cashout_order' => 'YOSHOP_CASHOUT_ORDER',
    'health_suggestion_work' => 'YOSHOP_HEALTH_SUGGESTION_WORK',
    'health_suggestion' => 'YOSHOP_HEALTH_SUGGESTION',
    'meal_suggestion' => 'YOSHOP_MEAL_SUGGESTION',
];
$storeTables = [
    'recipes' => 'YDY_RECIPES',
    'dishes' => 'YDY_DISHES',
    'restaurants' => 'YDY_RESTAURANTS',
    'meal_order' => 'YDY_MEAL_ORDER',
    'user' => 'YDY_USER',
    'recipes_log' => 'YDY_RECIPES_LOG',
];
$access = ['ai' => [], 'store' => []];
foreach (['ai' => [$ai, $aiTables], 'store' => [$store, $storeTables]] as $side => [$pdo, $tables]) {
    foreach ($tables as $label => $table) {
        try {
            $pdo->query('SELECT 1 FROM ' . $table . ' WHERE 1=0');
            $access[$side][$label] = true;
        } catch (Throwable $ignored) {
            $access[$side][$label] = false;
        }
    }
}
$stage = 'catalog_aggregate_counts';
$count = static function (PDO $pdo, string $sql, array $bind = []): ?int {
    try {
        $stmt = $pdo->prepare($sql);
        $stmt->execute($bind);
        return (int)$stmt->fetchColumn();
    } catch (Throwable $ignored) {
        return null;
    }
};
$start = date('Y-m-d');
$end = date('Y-m-d', strtotime($start . ' +7 days'));
$catalog = [
    'ai_top_level_categories' => $access['ai']['dishes_cate']
        ? $count($ai, 'SELECT COUNT(*) FROM YOSHOP_DISHES_CATE WHERE PID=0') : null,
    'ai_active_dishes' => $access['ai']['dishes']
        ? $count($ai, 'SELECT COUNT(*) FROM YOSHOP_DISHES WHERE IS_DELETE=0') : null,
    'store_active_restaurants' => $access['store']['restaurants']
        ? $count($store, 'SELECT COUNT(*) FROM YDY_RESTAURANTS'
            . ' WHERE STATUS=1 AND IS_DELETE=2') : null,
    'store_mapped_recipe_rows_next_7_days' =>
        $access['store']['recipes'] && $access['store']['dishes']
        ? $count($store, 'SELECT COUNT(*) FROM YDY_RECIPES r'
            . ' INNER JOIN YDY_DISHES d ON d.UUID=r.DISHES_UUID'
            . ' WHERE r.MENU_DATE>=? AND r.MENU_DATE<?'
            . ' AND d.IS_DELETE=0 AND d.AI_DISHES_ID>0', [$start, $end]) : null,
];
echo json_encode(['status' => 'catalog-ledger-c-readonly-inventory',
    'nonproduction_identity' => true, 'external_stubs' => true,
    'select_zero_row_access' => $access,
    'natural_catalog_aggregate_counts' => $catalog,
    'next_7_days_window' => [$start, $end],
    'business_http_count' => 0, 'business_rows_read' => 0,
    'writes' => 0], JSON_UNESCAPED_SLASHES), PHP_EOL;
