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

<!--- Only allow from localhost --->
<cfif NOT (cgi.remote_addr EQ "127.0.0.1" OR cgi.remote_addr EQ "::1" OR findNoCase("localhost", cgi.server_name))>
    <cfoutput>#serializeJSON({"OK": false, "ERROR": "admin_only"})#</cfoutput>
    <cfabort>
</cfif>

<cfscript>
/**
 * Fix Real Ice Cream Shake modifier structure
 *
 * Current (wrong):
 *   Real Ice Cream Shake (5808)
 *     ├── Chocolate (6294) - marked as template
 *     ├── Strawberry (6292) - marked as template
 *     └── Vanilla (6293) - marked as template
 *
 * Correct:
 *   Real Ice Cream Shake (5808)
 *     └── Choose Flavor (NEW - modifier group, REQUIRED)
 *           ├── Chocolate
 *           ├── Strawberry
 *           └── Vanilla
 *
 * Steps:
 * 1. Create "Choose Flavor" modifier group under Shake (5808)
 * 2. Re-parent the three flavors under the new group
 * 3. Remove template flag from flavors
 * 4. Mark "Choose Flavor" as the template
 * 5. Create lt_ItemID_TemplateItemID entry for Shake -> Choose Flavor
 * 6. Set RequiresChildSelection=1 on the shake item
 */

response = { "OK": false, "steps": [] };

try {
    shakeItemID = 5808;
    chocolateID = 6294;
    strawberryID = 6292;
    vanillaID = 6293;
    categoryID = 46; // Fries and Shakes

    // Step 1: Create "Choose Flavor" modifier group under Shake
    queryExecute("
        INSERT INTO Items (
            CategoryID,
            Name,
            Description,
            ParentItemID,
            Price,
            IsActive,
            IsCheckedByDefault,
            RequiresChildSelection,
            MaxNumSelectionReq,
            IsCollapsible,
            SortOrder,
            IsModifierTemplate,
            AddedOn
        ) VALUES (
            :categoryID,
            'Choose Flavor',
            '',
            :shakeItemID,
            0,
            1,
            0,
            1,
            1,
            1,
            0,
            1,
            NOW()
        )
    ", {
        categoryID: categoryID,
        shakeItemID: shakeItemID
    }, { datasource: "payfrit" });

    // Get the new Choose Flavor ID
    qNewGroup = queryExecute("
        SELECT ID FROM Items
        WHERE Name = 'Choose Flavor'
          AND ParentItemID = :shakeItemID
        ORDER BY ID DESC
        LIMIT 1
    ", { shakeItemID: shakeItemID }, { datasource: "payfrit" });

    chooseFlavorID = qNewGroup.ID;
    arrayAppend(response.steps, "Created 'Choose Flavor' group with ID: " & chooseFlavorID);

    // Step 2: Re-parent the three flavors under Choose Flavor
    queryExecute("
        UPDATE Items
        SET ParentItemID = :chooseFlavorID,
            IsModifierTemplate = 0,
            IsCheckedByDefault = 0
        WHERE ItemID IN (:chocolateID, :strawberryID, :vanillaID)
    ", {
        chooseFlavorID: chooseFlavorID,
        chocolateID: chocolateID,
        strawberryID: strawberryID,
        vanillaID: vanillaID
    }, { datasource: "payfrit" });

    arrayAppend(response.steps, "Re-parented Chocolate, Strawberry, Vanilla under Choose Flavor");

    // Step 3: Set Vanilla as default (common choice)
    queryExecute("
        UPDATE Items SET IsCheckedByDefault = 1 WHERE ItemID = :vanillaID
    ", { vanillaID: vanillaID }, { datasource: "payfrit" });

    arrayAppend(response.steps, "Set Vanilla as default flavor");

    // Step 4: Remove old template links for the flavors
    queryExecute("
        DELETE FROM lt_ItemID_TemplateItemID
        WHERE TemplateItemID IN (:chocolateID, :strawberryID, :vanillaID)
    ", {
        chocolateID: chocolateID,
        strawberryID: strawberryID,
        vanillaID: vanillaID
    }, { datasource: "payfrit" });

    arrayAppend(response.steps, "Removed old template links for flavor items");

    // Step 5: Create lt_ItemID_TemplateItemID for Shake -> Choose Flavor
    queryExecute("
        INSERT INTO lt_ItemID_TemplateItemID (ItemID, TemplateItemID, SortOrder)
        VALUES (:shakeItemID, :chooseFlavorID, 0)
        ON DUPLICATE KEY UPDATE SortOrder = 0
    ", {
        shakeItemID: shakeItemID,
        chooseFlavorID: chooseFlavorID
    }, { datasource: "payfrit" });

    arrayAppend(response.steps, "Created template link: Shake -> Choose Flavor");

    // Step 6: Set RequiresChildSelection on shake
    queryExecute("
        UPDATE Items
        SET RequiresChildSelection = 1
        WHERE ItemID = :shakeItemID
    ", { shakeItemID: shakeItemID }, { datasource: "payfrit" });

    arrayAppend(response.steps, "Set RequiresChildSelection=1 on Shake item");

    response["OK"] = true;
    response["chooseFlavorID"] = chooseFlavorID;

} catch (any e) {
    response["ERROR"] = e.message;
    response["DETAIL"] = e.detail;
}

writeOutput(serializeJSON(response));
</cfscript>
