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

<cfscript>
businessId = 27; // Big Dean's

// Categories are items with ItemParentItemID=0 AND ItemIsCollapsible=0
// Modifier templates are items with ItemParentItemID=0 AND ItemIsCollapsible=1
// Menu items are children of categories
// Modifiers are children of menu items or modifier templates

// Get category IDs (NOT modifier templates)
categoryIds = queryExecute("
  SELECT ItemID
  FROM Items
  WHERE ItemBusinessID = :bizId
    AND ItemParentItemID = 0
    AND ItemIsCollapsible = 0
", { bizId: businessId }, { datasource: "payfrit" });

catIdList = "";
for (cat in categoryIds) {
  catIdList = listAppend(catIdList, cat.ItemID);
}

// Now get actual menu items (direct children of categories)
// Exclude items that are template options (their parent is a collapsible modifier group)
items = queryExecute("
  SELECT i.ItemID, i.ItemName
  FROM Items i
  WHERE i.ItemBusinessID = :bizId
    AND i.ItemParentItemID IN (#catIdList#)
    AND i.ItemIsCollapsible = 0
    AND NOT EXISTS (
      SELECT 1 FROM ItemTemplateLinks tl WHERE tl.TemplateItemID = i.ItemID
    )
", { bizId: businessId }, { datasource: "payfrit" });

updated = [];

for (item in items) {
  itemName = lcase(item.ItemName);
  newPrice = 0;

  // Drinks - $3-6
  if (findNoCase("beer", itemName) || findNoCase("ale", itemName) || findNoCase("lager", itemName) || findNoCase("ipa", itemName) || findNoCase("stout", itemName)) {
    newPrice = randRange(5, 9) - 0.01; // $4.99 - $8.99
  }
  else if (findNoCase("wine", itemName) || findNoCase("sangria", itemName)) {
    newPrice = randRange(8, 14) - 0.01; // $7.99 - $13.99
  }
  else if (findNoCase("soda", itemName) || findNoCase("coke", itemName) || findNoCase("pepsi", itemName) || findNoCase("sprite", itemName) || findNoCase("lemonade", itemName) || findNoCase("tea", itemName) || findNoCase("coffee", itemName)) {
    newPrice = randRange(3, 5) - 0.01; // $2.99 - $4.99
  }
  else if (findNoCase("margarita", itemName) || findNoCase("cocktail", itemName) || findNoCase("martini", itemName) || findNoCase("mojito", itemName)) {
    newPrice = randRange(10, 15) - 0.01; // $9.99 - $14.99
  }
  // Appetizers / Starters - $8-14
  else if (findNoCase("fries", itemName) || findNoCase("chips", itemName) || findNoCase("nachos", itemName) || findNoCase("wings", itemName) || findNoCase("calamari", itemName) || findNoCase("appetizer", itemName) || findNoCase("starter", itemName)) {
    newPrice = randRange(8, 14) - 0.01; // $7.99 - $13.99
  }
  // Salads - $10-16
  else if (findNoCase("salad", itemName)) {
    newPrice = randRange(11, 17) - 0.01; // $10.99 - $16.99
  }
  // Soups - $6-10
  else if (findNoCase("soup", itemName) || findNoCase("chowder", itemName)) {
    newPrice = randRange(7, 11) - 0.01; // $6.99 - $10.99
  }
  // Burgers - $14-19
  else if (findNoCase("burger", itemName)) {
    newPrice = randRange(15, 20) - 0.01; // $14.99 - $19.99
  }
  // Sandwiches - $12-17
  else if (findNoCase("sandwich", itemName) || findNoCase("wrap", itemName) || findNoCase("club", itemName) || findNoCase("blt", itemName)) {
    newPrice = randRange(13, 18) - 0.01; // $12.99 - $17.99
  }
  // Fish & Seafood - $16-28
  else if (findNoCase("fish", itemName) || findNoCase("salmon", itemName) || findNoCase("shrimp", itemName) || findNoCase("lobster", itemName) || findNoCase("crab", itemName) || findNoCase("scallop", itemName) || findNoCase("tuna", itemName) || findNoCase("halibut", itemName) || findNoCase("cod", itemName)) {
    newPrice = randRange(17, 29) - 0.01; // $16.99 - $28.99
  }
  // Tacos - $13-18
  else if (findNoCase("taco", itemName)) {
    newPrice = randRange(14, 19) - 0.01; // $13.99 - $18.99
  }
  // Kids meals - $8-12
  else if (findNoCase("kid", itemName) || findNoCase("child", itemName)) {
    newPrice = randRange(9, 13) - 0.01; // $8.99 - $12.99
  }
  // Desserts - $7-12
  else if (findNoCase("dessert", itemName) || findNoCase("cake", itemName) || findNoCase("pie", itemName) || findNoCase("sundae", itemName) || findNoCase("brownie", itemName) || findNoCase("cheesecake", itemName) || findNoCase("ice cream", itemName)) {
    newPrice = randRange(8, 13) - 0.01; // $7.99 - $12.99
  }
  // Default entrees - $14-22
  else {
    newPrice = randRange(15, 23) - 0.01; // $14.99 - $22.99
  }

  queryExecute("
    UPDATE Items
    SET ItemPrice = :price
    WHERE ItemID = :itemId
  ", { price: newPrice, itemId: item.ItemID }, { datasource: "payfrit" });

  arrayAppend(updated, {
    "ItemID": item.ItemID,
    "ItemName": item.ItemName,
    "NewPrice": newPrice
  });
}

// Update modifier prices (children of menu items, NOT direct children of categories)
// Modifiers are items whose parent is NOT a category (i.e., parent is a menu item or modifier group)
modifiers = queryExecute("
  SELECT ItemID, ItemName
  FROM Items
  WHERE ItemBusinessID = :bizId
    AND ItemParentItemID > 0
    AND ItemParentItemID NOT IN (#catIdList#)
", { bizId: businessId }, { datasource: "payfrit" });

for (mod in modifiers) {
  modName = lcase(mod.ItemName);
  modPrice = 0;

  // Proteins are expensive add-ons
  if (findNoCase("bacon", modName) || findNoCase("avocado", modName) || findNoCase("guac", modName)) {
    modPrice = randRange(2, 4) - 0.01; // $1.99 - $3.99
  }
  else if (findNoCase("chicken", modName) || findNoCase("shrimp", modName) || findNoCase("steak", modName) || findNoCase("salmon", modName)) {
    modPrice = randRange(4, 7) - 0.01; // $3.99 - $6.99
  }
  else if (findNoCase("cheese", modName) || findNoCase("egg", modName)) {
    modPrice = randRange(1, 3) - 0.01; // $0.99 - $2.99
  }
  // Size upgrades
  else if (findNoCase("large", modName) || findNoCase("extra", modName) || findNoCase("double", modName)) {
    modPrice = randRange(2, 4) - 0.01; // $1.99 - $3.99
  }
  // Most modifiers (toppings, sauces, etc.) are free or cheap
  else {
    // 70% free, 30% small charge
    if (randRange(1, 10) <= 7) {
      modPrice = 0;
    } else {
      modPrice = randRange(1, 2) - 0.01; // $0.99 - $1.99
    }
  }

  queryExecute("
    UPDATE Items
    SET ItemPrice = :price
    WHERE ItemID = :itemId
  ", { price: modPrice, itemId: mod.ItemID }, { datasource: "payfrit" });
}

// Reset category prices to $0 (shouldn't have prices for reporting)
queryExecute("
  UPDATE Items
  SET ItemPrice = 0
  WHERE ItemBusinessID = :bizId
    AND ItemParentItemID = 0
", { bizId: businessId }, { datasource: "payfrit" });

// Reset modifier group prices to $0 (only options have prices)
queryExecute("
  UPDATE Items
  SET ItemPrice = 0
  WHERE ItemBusinessID = :bizId
    AND ItemIsCollapsible = 1
", { bizId: businessId }, { datasource: "payfrit" });

writeOutput(serializeJSON({
  "OK": true,
  "MESSAGE": "Updated prices for #arrayLen(updated)# menu items and #modifiers.recordCount# modifiers. Reset category and group prices to $0.",
  "ITEMS": updated
}));
</cfscript>
