<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">

<cfscript>
// Big Dean's Ocean Front Cafe - Full Menu Reimport
// Based on actual menu image analysis

bizId = 27;
dryRun = structKeyExists(url, "dryRun") ? true : false;

actions = [];

// ============================================================
// STEP 1: Clear existing Big Dean's menu data
// ============================================================
if (!dryRun) {
  // Delete template links first
  queryTimed("DELETE FROM lt_ItemID_TemplateItemID WHERE ItemID IN (SELECT ID FROM Items WHERE BusinessID = :bizId)", { bizId: bizId });
  // Delete all items
  queryTimed("DELETE FROM Items WHERE BusinessID = :bizId", { bizId: bizId });
}
arrayAppend(actions, { step: "CLEAR", message: dryRun ? "Would clear existing data" : "Cleared existing data" });

// ============================================================
// STEP 2: Create Modifier Templates (shared across items)
// ============================================================

// Helper function to insert item and return ID
function insertItem(name, description, parentId, price, isActive, isCheckedByDefault, requiresChild, maxSelections, isCollapsible, sortOrder) {
  if (dryRun) return 0;
  // Note: IsActive is BIT(1) type, use b'1' or b'0' syntax
  queryTimed("
    INSERT INTO Items (BusinessID, Name, Description, ParentItemID, Price,
                       IsActive, IsCheckedByDefault, RequiresChildSelection,
                       MaxNumSelectionReq, IsCollapsible, SortOrder, AddedOn)
    VALUES (:bizId, :name, :desc, :parentId, :price, b'#isActive#', :isDefault, :reqChild, :maxSel, :isCollapse, :sort, NOW())
  ", {
    bizId: bizId, name: name, desc: description, parentId: parentId, price: price,
    isDefault: isCheckedByDefault, reqChild: requiresChild,
    maxSel: maxSelections, isCollapse: isCollapsible, sort: sortOrder
  });

  qId = queryTimed("SELECT LAST_INSERT_ID() as id");
  return qId.id;
}

// Helper to link template to item
function linkTemplate(menuItemId, templateId, sortOrder) {
  if (dryRun) return;
  queryTimed("
    INSERT INTO lt_ItemID_TemplateItemID (ItemID, TemplateItemID, SortOrder)
    VALUES (:itemId, :templateId, :sort)
  ", { itemId: menuItemId, templateId: templateId, sort: sortOrder });
}

// ============================================================
// MODIFIER TEMPLATES
// ============================================================

// --- Burger Toppings Template: Lettuce ---
tplLettuce = insertItem("Lettuce", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("Regular", "", tplLettuce, 0, 1, 1, 0, 0, 0, 1);
insertItem("Extra", "", tplLettuce, 0, 1, 0, 0, 0, 0, 2);
insertItem("Light", "", tplLettuce, 0, 1, 0, 0, 0, 0, 3);
insertItem("None", "", tplLettuce, 0, 1, 0, 0, 0, 0, 4);
arrayAppend(actions, { step: "TEMPLATE", name: "Lettuce", id: tplLettuce });

// --- Burger Toppings Template: Tomato ---
tplTomato = insertItem("Tomato", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("Regular", "", tplTomato, 0, 1, 1, 0, 0, 0, 1);
insertItem("Extra", "", tplTomato, 0, 1, 0, 0, 0, 0, 2);
insertItem("Light", "", tplTomato, 0, 1, 0, 0, 0, 0, 3);
insertItem("None", "", tplTomato, 0, 1, 0, 0, 0, 0, 4);
arrayAppend(actions, { step: "TEMPLATE", name: "Tomato", id: tplTomato });

// --- Burger Toppings Template: Big Dean's Sauce ---
tplSauce = insertItem("Big Dean's Sauce", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("Regular", "", tplSauce, 0, 1, 1, 0, 0, 0, 1);
insertItem("Extra", "", tplSauce, 0, 1, 0, 0, 0, 0, 2);
insertItem("Light", "", tplSauce, 0, 1, 0, 0, 0, 0, 3);
insertItem("None", "", tplSauce, 0, 1, 0, 0, 0, 0, 4);
arrayAppend(actions, { step: "TEMPLATE", name: "Big Dean's Sauce", id: tplSauce });

// --- Burger Toppings Template: Onions ---
tplOnions = insertItem("Onions", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("None", "", tplOnions, 0, 1, 1, 0, 0, 0, 1);
insertItem("Grilled", "", tplOnions, 0, 1, 0, 0, 0, 0, 2);
insertItem("Raw", "", tplOnions, 0, 1, 0, 0, 0, 0, 3);
arrayAppend(actions, { step: "TEMPLATE", name: "Onions", id: tplOnions });

// --- Burger Toppings Template: Pickle Spear ---
tplPickle = insertItem("Pickle Spear", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("None", "", tplPickle, 0, 1, 1, 0, 0, 0, 1);
insertItem("Add Pickle", "", tplPickle, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Pickle Spear", id: tplPickle });

// --- Wing Style Template ---
tplWingStyle = insertItem("Wing Style", "", 0, 0, 1, 0, 1, 1, 1, 1);
insertItem("Bone-In", "", tplWingStyle, 0, 1, 1, 0, 0, 0, 1);
insertItem("Boneless", "", tplWingStyle, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Wing Style", id: tplWingStyle });

// --- Size Template (Fries/Rings) ---
tplSize = insertItem("Size", "", 0, 0, 1, 0, 1, 1, 1, 1);
insertItem("Regular", "", tplSize, 0, 1, 1, 0, 0, 0, 1);
insertItem("Small", "", tplSize, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Size", id: tplSize });

// --- Portion Size Template (Half/Full) ---
tplPortion = insertItem("Portion", "", 0, 0, 1, 0, 1, 1, 1, 1);
insertItem("Full Order", "", tplPortion, 0, 1, 1, 0, 0, 0, 1);
insertItem("Half Order", "", tplPortion, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Portion", id: tplPortion });

// --- Sides Extras Template ($.50 each) ---
tplSideExtras = insertItem("Add Extras", "$.50 each", 0, 0, 1, 0, 0, 0, 0, 1);
insertItem("Sour Cream", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 1);
insertItem("Jalapenos", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 2);
insertItem("BBQ Sauce", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 3);
insertItem("Ranch", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 4);
insertItem("Blue Cheese", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 5);
insertItem("Mayo", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 6);
insertItem("Aioli", "", tplSideExtras, 0.50, 1, 0, 0, 0, 0, 7);
arrayAppend(actions, { step: "TEMPLATE", name: "Add Extras", id: tplSideExtras });

// --- Guacamole Option ---
tplGuac = insertItem("Guacamole", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("Without Guacamole", "", tplGuac, 0, 1, 1, 0, 0, 0, 1);
insertItem("With Guacamole", "", tplGuac, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Guacamole", id: tplGuac });

// --- Quesadilla/Nachos Add-ins (multi-select checkboxes) ---
tplAddIns = insertItem("Add-ins", "", 0, 0, 1, 0, 0, 0, 0, 1);
insertItem("Chicken", "", tplAddIns, 0, 1, 0, 0, 0, 0, 1);
insertItem("Steak", "", tplAddIns, 0, 1, 0, 0, 0, 0, 2);
insertItem("Shrimp", "", tplAddIns, 0, 1, 0, 0, 0, 0, 3);
arrayAppend(actions, { step: "TEMPLATE", name: "Add-ins", id: tplAddIns });

// --- Chili Fries Extras ---
tplChiliExtras = insertItem("Extras", "", 0, 0, 1, 0, 0, 0, 0, 1);
insertItem("Add Cheese", "", tplChiliExtras, 0.50, 1, 0, 0, 0, 0, 1);
insertItem("Add Onions", "", tplChiliExtras, 0.50, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Extras (Chili)", id: tplChiliExtras });

// --- Salad Dressing ---
tplDressing = insertItem("Dressing", "", 0, 0, 1, 0, 1, 1, 1, 1);
insertItem("Italian", "", tplDressing, 0, 1, 0, 0, 0, 0, 1);
insertItem("Ranch", "", tplDressing, 0, 1, 1, 0, 0, 0, 2);
insertItem("Balsamic", "", tplDressing, 0, 1, 0, 0, 0, 0, 3);
insertItem("Caesar", "", tplDressing, 0, 1, 0, 0, 0, 0, 4);
insertItem("Blue Cheese", "", tplDressing, 0, 1, 0, 0, 0, 0, 5);
insertItem("Thousand Island", "", tplDressing, 0, 1, 0, 0, 0, 0, 6);
insertItem("French", "", tplDressing, 0, 1, 0, 0, 0, 0, 7);
arrayAppend(actions, { step: "TEMPLATE", name: "Dressing", id: tplDressing });

// --- Salad Add Protein ---
tplSaladProtein = insertItem("Add Protein", "", 0, 0, 1, 0, 0, 1, 1, 1);
insertItem("No Protein", "", tplSaladProtein, 0, 1, 1, 0, 0, 0, 1);
insertItem("Chicken Breast or Burger Patty", "", tplSaladProtein, 0, 1, 0, 0, 0, 0, 2);
insertItem("Grilled Shrimp", "", tplSaladProtein, 0, 1, 0, 0, 0, 0, 3);
insertItem("Grilled Mahi Mahi", "", tplSaladProtein, 0, 1, 0, 0, 0, 0, 4);
arrayAppend(actions, { step: "TEMPLATE", name: "Add Protein (Salad)", id: tplSaladProtein });

// --- Hot Dog Extras ---
tplHotDogExtras = insertItem("Extras", "", 0, 0, 1, 0, 0, 0, 0, 1);
insertItem("Add Sauerkraut", "", tplHotDogExtras, 0.50, 1, 0, 0, 0, 0, 1);
insertItem("Add Cheese", "", tplHotDogExtras, 0.50, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "TEMPLATE", name: "Extras (Hot Dog)", id: tplHotDogExtras });

// ============================================================
// CATEGORIES AND MENU ITEMS
// ============================================================

// --- CATEGORY: World Famous Burgers ---
catBurgers = insertItem("World Famous Burgers", "", 0, 0, 1, 0, 0, 0, 0, 1);
arrayAppend(actions, { step: "CATEGORY", name: "World Famous Burgers", id: catBurgers });

burger1 = insertItem("Big Dean's Cheeseburger", "Double meat double cheese - The burger that made Santa Monica famous!", catBurgers, 0, 1, 0, 0, 0, 0, 1);
burger2 = insertItem("Single Beef Burger with Cheese", "", catBurgers, 0, 1, 0, 0, 0, 0, 2);
burger3 = insertItem("Single Beef Burger", "", catBurgers, 0, 1, 0, 0, 0, 0, 3);
burger4 = insertItem("Beyond Burger", "Vegan burger patty w/ lettuce, tomato, and pickle spear", catBurgers, 0, 1, 0, 0, 0, 0, 4);
burger5 = insertItem("Garden Burger", "Vegetable patty prepared in the style of our burgers", catBurgers, 0, 1, 0, 0, 0, 0, 5);
burger6 = insertItem("Chili Size Burger", "Beef burger served open faced topped with all beef chili, cheese, and onions", catBurgers, 0, 1, 0, 0, 0, 0, 6);

// Link burger templates to all burgers
burgerIds = [burger1, burger2, burger3, burger4, burger5, burger6];
for (bid in burgerIds) {
  linkTemplate(bid, tplLettuce, 1);
  linkTemplate(bid, tplTomato, 2);
  linkTemplate(bid, tplSauce, 3);
  linkTemplate(bid, tplOnions, 4);
  linkTemplate(bid, tplPickle, 5);
}
arrayAppend(actions, { step: "ITEMS", category: "Burgers", count: 6 });

// --- CATEGORY: Snacks and Sides ---
catSides = insertItem("Snacks and Sides", "", 0, 0, 1, 0, 0, 0, 0, 2);
arrayAppend(actions, { step: "CATEGORY", name: "Snacks and Sides", id: catSides });

side1 = insertItem("Buffalo Wings", "", catSides, 0, 1, 0, 0, 0, 0, 1);
linkTemplate(side1, tplWingStyle, 1);
linkTemplate(side1, tplSideExtras, 2);

side2 = insertItem("Frings", "1/2 French fries and 1/2 onion rings", catSides, 0, 1, 0, 0, 0, 0, 2);
linkTemplate(side2, tplSideExtras, 1);

side3 = insertItem("French Fries", "", catSides, 0, 1, 0, 0, 0, 0, 3);
linkTemplate(side3, tplSize, 1);
linkTemplate(side3, tplSideExtras, 2);

side4 = insertItem("Onion Rings", "", catSides, 0, 1, 0, 0, 0, 0, 4);
linkTemplate(side4, tplSize, 1);
linkTemplate(side4, tplSideExtras, 2);

side5 = insertItem("Chili Fries", "", catSides, 0, 1, 0, 0, 0, 0, 5);
linkTemplate(side5, tplChiliExtras, 1);
linkTemplate(side5, tplSideExtras, 2);

side6 = insertItem("Chicken Fingers and Fries", "", catSides, 0, 1, 0, 0, 0, 0, 6);
linkTemplate(side6, tplPortion, 1);
linkTemplate(side6, tplSideExtras, 2);

side7 = insertItem("Chips and Salsa", "", catSides, 0, 1, 0, 0, 0, 0, 7);
linkTemplate(side7, tplGuac, 1);

side8 = insertItem("Cheese Quesadilla", "", catSides, 0, 1, 0, 0, 0, 0, 8);
linkTemplate(side8, tplAddIns, 1);

side9 = insertItem("Cheese Nachos", "", catSides, 0, 1, 0, 0, 0, 0, 9);
linkTemplate(side9, tplAddIns, 1);

side10 = insertItem("Chili Cheese Nachos", "", catSides, 0, 1, 0, 0, 0, 0, 10);
side11 = insertItem("Mozzarella Sticks", "", catSides, 0, 1, 0, 0, 0, 0, 11);
linkTemplate(side11, tplSideExtras, 1);

arrayAppend(actions, { step: "ITEMS", category: "Snacks and Sides", count: 11 });

// --- CATEGORY: Sandwiches ---
catSandwiches = insertItem("Sandwiches", "", 0, 0, 1, 0, 0, 0, 0, 3);
arrayAppend(actions, { step: "CATEGORY", name: "Sandwiches", id: catSandwiches });

insertItem("Cajun Mahi Mahi", "Lettuce, tomato, aioli, and guacamole", catSandwiches, 0, 1, 0, 0, 0, 0, 1);
insertItem("Philly Cheese Steak", "Grilled peppers and onions", catSandwiches, 0, 1, 0, 0, 0, 0, 2);
insertItem("Cajun Chicken", "Lettuce, tomato, and spicy aioli", catSandwiches, 0, 1, 0, 0, 0, 0, 3);
insertItem("Turkey", "Lettuce, tomato, and mayo", catSandwiches, 0, 1, 0, 0, 0, 0, 4);
insertItem("Turkey Club", "Bacon, lettuce, tomato, and mayo", catSandwiches, 0, 1, 0, 0, 0, 0, 5);
insertItem("Grilled Cheese", "Sourdough and American cheese", catSandwiches, 0, 1, 0, 0, 0, 0, 6);
insertItem("Grilled Ham and Cheese", "", catSandwiches, 0, 1, 0, 0, 0, 0, 7);
insertItem("Grilled Chicken", "Lettuce, tomato, and mayo", catSandwiches, 0, 1, 0, 0, 0, 0, 8);
insertItem("BBQ Chicken", "Lettuce, tomato, and BBQ sauce", catSandwiches, 0, 1, 0, 0, 0, 0, 9);
insertItem("Fried Chicken", "Lettuce, tomato, and mayo", catSandwiches, 0, 1, 0, 0, 0, 0, 10);
insertItem("Chicken Caesar Wrap", "", catSandwiches, 0, 1, 0, 0, 0, 0, 11);

arrayAppend(actions, { step: "ITEMS", category: "Sandwiches", count: 11 });

// --- CATEGORY: Soups & Salads ---
catSoups = insertItem("Soups & Salads", "", 0, 0, 1, 0, 0, 0, 0, 4);
arrayAppend(actions, { step: "CATEGORY", name: "Soups & Salads", id: catSoups });

insertItem("New England Clam Chowder", "", catSoups, 0, 1, 0, 0, 0, 0, 1);
insertItem("Chili Bowl", "With Cheese and Onion", catSoups, 0, 1, 0, 0, 0, 0, 2);

salad1 = insertItem("Beach Salad", "Spring mix greens, tomatoes, cucumbers, feta cheese, and carrots served in a crisp flour tortilla shell", catSoups, 0, 1, 0, 0, 0, 0, 3);
linkTemplate(salad1, tplDressing, 1);
linkTemplate(salad1, tplSaladProtein, 2);

salad2 = insertItem("Caesar Salad", "Romaine lettuce, croutons, and Caesar dressing", catSoups, 0, 1, 0, 0, 0, 0, 4);
linkTemplate(salad2, tplSaladProtein, 1);

arrayAppend(actions, { step: "ITEMS", category: "Soups & Salads", count: 4 });

// --- CATEGORY: Tacos ---
catTacos = insertItem("Tacos", "Served two per order on flour tortillas unless otherwise noted", 0, 0, 1, 0, 0, 0, 0, 5);
arrayAppend(actions, { step: "CATEGORY", name: "Tacos", id: catTacos });

insertItem("Grilled Chicken Tacos", "Taco sauce, cheese, lettuce, tomatoes, and ranch", catTacos, 0, 1, 0, 0, 0, 0, 1);
insertItem("Fried Fish Tacos", "Taco sauce, cheese, lettuce, tomatoes, and ranch", catTacos, 0, 1, 0, 0, 0, 0, 2);
insertItem("Mahi Mahi Tacos", "Cheese, cabbage, and tomatoes on corn tortillas with a side of guacamole and salsa", catTacos, 0, 1, 0, 0, 0, 0, 3);
insertItem("Steak Fajita Tacos", "Grilled with peppers, onions, and a side of cheese, tomatoes, and lettuce", catTacos, 0, 1, 0, 0, 0, 0, 4);
insertItem("Shrimp Fajita Tacos", "Grilled with peppers, onions, and a side of cheese, tomatoes, and lettuce", catTacos, 0, 1, 0, 0, 0, 0, 5);

arrayAppend(actions, { step: "ITEMS", category: "Tacos", count: 5 });

// --- CATEGORY: Seafood ---
catSeafood = insertItem("Seafood", "", 0, 0, 1, 0, 0, 0, 0, 6);
arrayAppend(actions, { step: "CATEGORY", name: "Seafood", id: catSeafood });

insertItem("Fried Fantail Shrimp", "With Fries", catSeafood, 0, 1, 0, 0, 0, 0, 1);
insertItem("Clam Boat with Fries", "", catSeafood, 0, 1, 0, 0, 0, 0, 2);

seafood3 = insertItem("Fish and Chips", "", catSeafood, 0, 1, 0, 0, 0, 0, 3);
linkTemplate(seafood3, tplPortion, 1);

insertItem("Grilled Shrimp & Baby Scallop Skewers", "", catSeafood, 0, 1, 0, 0, 0, 0, 4);

arrayAppend(actions, { step: "ITEMS", category: "Seafood", count: 4 });

// --- CATEGORY: Hot Dogs & Such ---
catHotDogs = insertItem("Hot Dogs & Such", "", 0, 0, 1, 0, 0, 0, 0, 7);
arrayAppend(actions, { step: "CATEGORY", name: "Hot Dogs & Such", id: catHotDogs });

hotdog1 = insertItem("Hot Dog", "Topped with onions, tomatoes, and relish", catHotDogs, 0, 1, 0, 0, 0, 0, 1);
linkTemplate(hotdog1, tplHotDogExtras, 1);

insertItem("Corn Dog", "", catHotDogs, 0, 1, 0, 0, 0, 0, 2);

hotdog3 = insertItem("Chili Dog", "Topped with all beef chili", catHotDogs, 0, 1, 0, 0, 0, 0, 3);
linkTemplate(hotdog3, tplHotDogExtras, 1);

arrayAppend(actions, { step: "ITEMS", category: "Hot Dogs & Such", count: 3 });

// ============================================================
// SUMMARY
// ============================================================

// Count totals
if (!dryRun) {
  qCount = queryTimed("SELECT COUNT(*) as cnt FROM Items WHERE BusinessID = :bizId", { bizId: bizId });
  totalItems = qCount.cnt;

  qTemplateCount = queryTimed("SELECT COUNT(*) as cnt FROM Items WHERE BusinessID = :bizId AND ParentItemID = 0 AND IsCollapsible = 1", { bizId: bizId });
  templateCount = qTemplateCount.cnt;

  qLinkCount = queryTimed("
    SELECT COUNT(*) as cnt FROM lt_ItemID_TemplateItemID tl
    JOIN Items i ON i.ID = tl.ItemID
    WHERE i.BusinessID = :bizId
  ", { bizId: bizId });
  linkCount = qLinkCount.cnt;
} else {
  totalItems = "N/A (dry run)";
  templateCount = "N/A (dry run)";
  linkCount = "N/A (dry run)";
}

writeOutput(serializeJSON({
  "OK": true,
  "DryRun": dryRun,
  "BusinessID": bizId,
  "TotalItems": totalItems,
  "TemplateCount": templateCount,
  "TemplateLinkCount": linkCount,
  "Actions": actions
}));
</cfscript>
