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

<cfscript>
// Fix Big Dean's - remove the fake "category" items that are actually modifier templates
// The real categories start at ItemID 11271 (World Famous Burgers)
bizId = 27;

// These are the modifier template parent items that got created incorrectly as root items
// They should NOT be categories - they're modifier group headers
fakeCategories = [11177, 11180, 11183, 11186, 11190, 11193, 11196, 11199, 11204, 11212, 11220, 11259];

// Deactivate these items (or we could delete them, but deactivate is safer)
for (itemId in fakeCategories) {
  queryExecute("
    UPDATE Items SET ItemIsActive = 0 WHERE ItemID = :itemId AND ItemBusinessID = :bizId
  ", { itemId: itemId, bizId: bizId }, { datasource: "payfrit" });
}

// Also deactivate their children (modifier options that belong to these fake parents)
for (itemId in fakeCategories) {
  queryExecute("
    UPDATE Items SET ItemIsActive = 0 WHERE ItemParentItemID = :itemId AND ItemBusinessID = :bizId
  ", { itemId: itemId, bizId: bizId }, { datasource: "payfrit" });
}

// Now verify what categories remain
qCategories = queryExecute("
  SELECT i.ItemID, i.ItemName
  FROM Items i
  WHERE i.ItemBusinessID = :bizId
    AND i.ItemParentItemID = 0
    AND i.ItemIsActive = 1
    AND i.ItemIsCollapsible = 0
    AND NOT EXISTS (SELECT 1 FROM ItemTemplateLinks tl WHERE tl.TemplateItemID = i.ItemID)
  ORDER BY i.ItemSortOrder
", { bizId: bizId }, { datasource: "payfrit" });

categories = [];
for (row in qCategories) {
  arrayAppend(categories, { "ItemID": row.ItemID, "ItemName": row.ItemName });
}

writeOutput(serializeJSON({
  "OK": true,
  "MESSAGE": "Deactivated #arrayLen(fakeCategories)# fake category items and their children",
  "remainingCategories": categories
}));
</cfscript>
