#!/usr/bin/env node
/** Generates resourceRegistry.ts and updates hospitality feature pages. */
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const MOD = path.join(ROOT, "modules", "hospitality");
const FEATURES = path.join(MOD, "features");
const REGISTRY = path.join(MOD, "constants", "resourceRegistry.ts");

const FEATURE_MAP = [
  ["venues", "BranchesPage", "venues"],
  ["branches", "BranchListPage", "branches"],
  ["diningAreas", "DiningAreasPage", "diningAreas"],
  ["tables", "TablesPage", "tables"],
  ["posFloorPlans", "FloorMapPage", "posFloorPlans"],
  ["reservations", "ReservationsPage", "reservations"],
  ["waitlist", "QueuePage", "waitlist"],
  ["connectorRegistrationsCrm", "CustomersPage", "connectorRegistrationsCrm"],
  ["qrMenuSessions", "GuestsPage", "qrMenuSessions"],
  ["menus", "DigitalMenuPage", "menus"],
  ["menuCategories", "MenuCategoriesPage", "menuCategories"],
  ["menuItems", "MenuItemsPage", "menuItems"],
  ["modifierGroups", "ModifierGroupsPage", "modifierGroups"],
  ["modifierOptions", "ModifierOptionsPage", "modifierOptions"],
  ["availabilityWindows", "AvailabilityPage", "availabilityWindows"],
  ["menuMediaRefs", "MediaLibraryPage", "menuMediaRefs"],
  ["qrCodes", "QrMenuPage", "qrCodes"],
  ["qrOrderingSessions", "QrOrderingPage", "qrOrderingSessions"],
  ["carts", "CartPage", "carts"],
  ["cartLines", "CheckoutPage", "cartLines"],
  ["kitchenTickets", "KitchenDisplayPage", "kitchenTickets"],
  ["kitchenTicketItems", "KitchenQueuePage", "kitchenTicketItems"],
  ["kitchenTicketsPrep", "KitchenPrepPage", "kitchenTicketsPrep"],
  ["posTickets", "PosLitePage", "posTickets"],
  ["posPayments", "PosProPage", "posPayments"],
  ["posRegisters", "CashRegisterPage", "posRegisters"],
  ["posPaymentsList", "PaymentsPage", "posPaymentsList"],
  ["posDiscounts", "DiscountsPage", "posDiscounts"],
  ["posDiscountsCoupons", "CouponsPage", "posDiscountsCoupons"],
  ["featureTogglesPromo", "PromotionsPage", "featureTogglesPromo"],
  ["connectorDelivery", "DeliveryIntegrationPage", "connectorDelivery"],
  ["pickupOrders", "PickupOrdersPage", "pickupOrders"],
  ["onlineOrders", "OnlineOrdersPage", "onlineOrders"],
  ["orderTimeline", "OrderTimelinePage", "orderTimeline"],
  ["invoices", "InvoicesPage", "invoices"],
  ["receipts", "ReceiptsPage", "receipts"],
  ["analyticsReports", "ReportsPage", "analyticsReports"],
  ["analyticsSnapshots", "AnalyticsPage", "analyticsSnapshots"],
  ["inventoryOverview", "InventoryPage", "inventoryOverview"],
  ["connectorCrm", "CrmIntegrationPage", "connectorCrm"],
  ["connectorLoyalty", "LoyaltyIntegrationPage", "connectorLoyalty"],
  ["connectorCommunication", "CommunicationIntegrationPage", "connectorCommunication"],
  ["connectorWebsite", "WebsiteIntegrationPage", "connectorWebsite"],
  ["connectorDispatches", "MarketplaceIntegrationPage", "connectorDispatches"],
  ["settings", "SettingsPage", "settings"],
  ["permissions", "PermissionsPage", "permissions"],
  ["roles", "RolesPage", "roles"],
  ["events", "AuditLogsPage", "events"],
  ["notifications", "NotificationsPage", "notifications"],
  ["tenantBundles", "BundlesPage", "tenantBundles"],
];

// Registry body is maintained in gen script — run to regenerate resourceRegistry.ts
const registryPath = path.join(ROOT, "scripts", "_hospitality-registry-body.ts.txt");
if (!fs.existsSync(registryPath)) {
  console.error("Missing", registryPath, "- run setup first");
  process.exit(1);
}
const registryBody = fs.readFileSync(registryPath, "utf8");
fs.mkdirSync(path.dirname(REGISTRY), { recursive: true });
fs.writeFileSync(REGISTRY, registryBody);

for (const [featureFile, exportName, registryKey] of FEATURE_MAP) {
  const content = `"use client";

import { createHospitalityListPage } from "@/modules/hospitality/components/HospitalityListCrudPage";
import { hospitalityResources } from "@/modules/hospitality/constants/resourceRegistry";

export const ${exportName} = createHospitalityListPage(hospitalityResources.${registryKey});

export default ${exportName};
`;
  fs.writeFileSync(path.join(FEATURES, `${featureFile}.tsx`), content);
}

console.log("Generated registry +", FEATURE_MAP.length, "features");
