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

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

// These modifier templates were incorrectly deactivated
// We need to reactivate and link them to burgers
modifierTemplates = {
  // For burgers: need "Extras" with Add Cheese option
  "11196": { name: "Extras", children: "Add Cheese, Add Onions", linkToBurgers: true },
  // There's already an active "Onions" (11267), so we don't need 11220
};

// Burger items that need modifiers
burgerIds = [11286, 11287, 11288, 11289, 11290]; // Big Dean's, Single w/Cheese, Single, Beyond, Garden

actions = [];

// First, let's see what templates already exist and are active for burgers
qExistingLinks = queryTimed("
  SELECT tl.ItemID as MenuItemID, tl.TemplateItemID, t.Name as TemplateName
  FROM lt_ItemID_TemplateItemID tl
  JOIN Items t ON t.ItemID = tl.TemplateItemID
  WHERE tl.ItemID IN (:burgerIds)
", { burgerIds: { value: arrayToList(burgerIds), list: true } }, { datasource: "payfrit" });

for (row in qExistingLinks) {
  arrayAppend(actions, { action: "EXISTS", menuItemID: row.MenuItemID, templateID: row.TemplateItemID, templateName: row.TemplateName });
}

// Reactivate template 11196 (Extras with Add Cheese)
if (!dryRun) {
  queryTimed("UPDATE Items SET IsActive = 1 WHERE ItemID = 11196", {}, { datasource: "payfrit" });
  queryTimed("UPDATE Items SET IsActive = 1 WHERE ParentItemID = 11196", {}, { datasource: "payfrit" });
}
arrayAppend(actions, { action: dryRun ? "WOULD_REACTIVATE" : "REACTIVATED", itemID: 11196, name: "Extras (Add Cheese, Add Onions)" });

// Link template 11196 to all burgers
for (burgerId in burgerIds) {
  // Check if link already exists
  qCheck = queryTimed("
    SELECT COUNT(*) as cnt FROM lt_ItemID_TemplateItemID WHERE ItemID = :burgerId AND TemplateItemID = 11196
  ", { burgerId: burgerId }, { datasource: "payfrit" });

  if (qCheck.cnt EQ 0) {
    if (!dryRun) {
      queryTimed("
        INSERT INTO lt_ItemID_TemplateItemID (ItemID, TemplateItemID, SortOrder)
        VALUES (:burgerId, 11196, 2)
      ", { burgerId: burgerId }, { datasource: "payfrit" });
    }
    arrayAppend(actions, { action: dryRun ? "WOULD_LINK" : "LINKED", menuItemID: burgerId, templateID: 11196 });
  }
}

// Verify the result
if (!dryRun) {
  qVerify = queryTimed("
    SELECT mi.ID, mi.Name, GROUP_CONCAT(t.Name ORDER BY tl.SortOrder) as Templates
    FROM Items mi
    LEFT JOIN lt_ItemID_TemplateItemID tl ON tl.ItemID = mi.ID
    LEFT JOIN Items t ON t.ItemID = tl.TemplateItemID
    WHERE mi.ID IN (:burgerIds)
    GROUP BY mi.ID, mi.Name
  ", { burgerIds: { value: arrayToList(burgerIds), list: true } }, { datasource: "payfrit" });

  result = [];
  for (row in qVerify) {
    arrayAppend(result, { itemID: row.ID, name: row.Name, templates: row.Templates });
  }
} else {
  result = "Dry run - no changes made";
}

writeOutput(serializeJSON({
  "OK": true,
  "DryRun": dryRun,
  "Actions": actions,
  "BurgersAfterFix": result
}));
</cfscript>
