<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfheader name="Cache-Control" value="no-store">

<cfscript>
/**
 * Copy drinks from In-N-Out (BusinessID 17) to Big Dean's (BusinessID 27)
 */

response = { "OK": false, "ItemsCreated": 0, "CategoryCreated": false };

try {
    bigDeansBusinessId = 27;

    // First, check if Big Dean's has a Beverages/Drinks category
    qExistingCat = queryTimed("
        SELECT ID, Name FROM Categories
        WHERE BusinessID = :bizId AND (Name LIKE '%Drink%' OR Name LIKE '%Beverage%')
    ", { bizId: bigDeansBusinessId }, { datasource: "payfrit" });

    if (qExistingCat.recordCount > 0) {
        drinksCategoryId = qExistingCat.ID;
        response["CategoryNote"] = "Using existing category: " & qExistingCat.Name;
    } else {
        // Create a new Beverages category for Big Dean's
        qMaxCat = queryTimed("SELECT COALESCE(MAX(ID), 0) + 1 as nextId FROM Categories", {}, { datasource: "payfrit" });
        drinksCategoryId = qMaxCat.nextId;

        qMaxSort = queryTimed("
            SELECT COALESCE(MAX(SortOrder), 0) + 1 as nextSort FROM Categories WHERE BusinessID = :bizId
        ", { bizId: bigDeansBusinessId }, { datasource: "payfrit" });

        queryTimed("
            INSERT INTO Categories (ID, BusinessID, ParentCategoryID, Name, SortOrder, AddedOn)
            VALUES (:catId, :bizId, 0, 'Beverages', :sortOrder, NOW())
        ", {
            catId: drinksCategoryId,
            bizId: bigDeansBusinessId,
            sortOrder: qMaxSort.nextSort
        }, { datasource: "payfrit" });

        response["CategoryCreated"] = true;
        response["CategoryNote"] = "Created new category: Beverages (ID: " & drinksCategoryId & ")";
    }

    // Drinks to add (from In-N-Out)
    drinks = [
        { name: "Fountain Soda", price: 2.10, desc: "Coca-Cola, Diet Coke, Sprite, Fanta Orange, Lemonade" },
        { name: "Bottled Water", price: 1.50, desc: "" },
        { name: "Iced Tea", price: 2.25, desc: "Freshly brewed" },
        { name: "Coffee", price: 1.95, desc: "Hot brewed coffee" },
        { name: "Hot Cocoa", price: 2.25, desc: "" },
        { name: "Milk", price: 1.25, desc: "2% milk" },
        { name: "Orange Juice", price: 2.50, desc: "Fresh squeezed" },
        { name: "Milkshake", price: 4.95, desc: "Chocolate, Vanilla, or Strawberry - made with real ice cream", requiresChild: 1 }
    ];

    itemsCreated = 0;

    for (drink in drinks) {
        // Check if item already exists
        qExists = queryTimed("
            SELECT ID FROM Items
            WHERE BusinessID = :bizId AND Name = :name AND CategoryID = :catId
        ", { bizId: bigDeansBusinessId, name: drink.name, catId: drinksCategoryId }, { datasource: "payfrit" });

        if (qExists.recordCount == 0) {
            // Get next ItemID
            qMaxItem = queryTimed("SELECT COALESCE(MAX(ID), 0) + 1 as nextId FROM Items", {}, { datasource: "payfrit" });
            newItemId = qMaxItem.nextId;

            queryTimed("
                INSERT INTO Items (
                    ID, BusinessID, CategoryID, ParentItemID,
                    Name, Description, Price, IsActive,
                    SortOrder, IsCollapsible, RequiresChildSelection,
                    AddedOn
                ) VALUES (
                    :itemId, :bizId, :catId, 0,
                    :name, :desc, :price, 1,
                    :sortOrder, 0, :requiresChild,
                    NOW()
                )
            ", {
                itemId: newItemId,
                bizId: bigDeansBusinessId,
                catId: drinksCategoryId,
                name: drink.name,
                desc: structKeyExists(drink, "desc") ? drink.desc : "",
                price: drink.price,
                sortOrder: itemsCreated,
                requiresChild: structKeyExists(drink, "requiresChild") ? drink.requiresChild : 0
            }, { datasource: "payfrit" });

            itemsCreated++;

            // If milkshake, add flavor options
            if (drink.name == "Milkshake") {
                flavors = ["Chocolate", "Vanilla", "Strawberry"];
                flavorSort = 0;
                for (flavor in flavors) {
                    qMaxOpt = queryTimed("SELECT COALESCE(MAX(ID), 0) + 1 as nextId FROM Items", {}, { datasource: "payfrit" });
                    queryTimed("
                        INSERT INTO Items (
                            ID, BusinessID, CategoryID, ParentItemID,
                            Name, Description, Price, IsActive,
                            SortOrder, IsCollapsible, IsCheckedByDefault,
                            AddedOn
                        ) VALUES (
                            :itemId, :bizId, 0, :parentId,
                            :name, '', 0, 1,
                            :sortOrder, 0, :isDefault,
                            NOW()
                        )
                    ", {
                        itemId: qMaxOpt.nextId,
                        bizId: bigDeansBusinessId,
                        parentId: newItemId,
                        name: flavor,
                        sortOrder: flavorSort,
                        isDefault: (flavor == "Chocolate") ? 1 : 0
                    }, { datasource: "payfrit" });
                    flavorSort++;
                }
            }
        }
    }

    response["OK"] = true;
    response["ItemsCreated"] = itemsCreated;
    response["CategoryID"] = drinksCategoryId;

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

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