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

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

try {
    // Business IDs to delete (Lo/Cal Coffee and SANTA MONICA entries)
    businessIDs = [38, 39, 40, 41, 42];

    for (bizID in businessIDs) {
        // Delete lt_ItemID_TemplateItemID for items belonging to this business
        queryExecute("
            DELETE itl FROM lt_ItemID_TemplateItemID itl
            INNER JOIN Items i ON i.ID = itl.ItemID
            WHERE i.BusinessID = :bizID
        ", { bizID: bizID }, { datasource: "payfrit" });

        // Delete Items
        queryExecute("DELETE FROM Items WHERE BusinessID = :bizID", { bizID: bizID }, { datasource: "payfrit" });

        // Delete Categories
        queryExecute("DELETE FROM Categories WHERE BusinessID = :bizID", { bizID: bizID }, { datasource: "payfrit" });

        // Delete Hours
        queryExecute("DELETE FROM Hours WHERE BusinessID = :bizID", { bizID: bizID }, { datasource: "payfrit" });

        // Delete Addresses linked to this business
        queryExecute("DELETE FROM Addresses WHERE BusinessID = :bizID", { bizID: bizID }, { datasource: "payfrit" });

        // Delete the Business itself
        queryExecute("DELETE FROM Businesses WHERE ID = :bizID", { bizID: bizID }, { datasource: "payfrit" });

        response.steps.append("Deleted business " & bizID & " and all related data");
    }

    response.OK = true;
    response.message = "Cleared all Lo/Cal Coffee and SANTA MONICA businesses";

} catch (any e) {
    response.error = e.message;
    if (len(e.detail)) {
        response.detail = e.detail;
    }
}

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