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

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

try {
    // Keep only Lazy Daisy (BusinessID 37)
    keepBusinessID = 37;

    // Unassign all beacons from service points of other businesses
    queryTimed("
        UPDATE ServicePoints
        SET BeaconID = NULL, AssignedByUserID = NULL
        WHERE BusinessID != :keepID AND BeaconID IS NOT NULL
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Unassigned beacons from other businesses' service points");

    // Get list of businesses to delete
    qBiz = queryTimed("
        SELECT ID, Name FROM Businesses WHERE ID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });

    deletedBusinesses = [];
    for (i = 1; i <= qBiz.recordCount; i++) {
        arrayAppend(deletedBusinesses, qBiz.Name[i]);
    }
    response.steps.append("Found " & qBiz.recordCount & " businesses to delete");

    // Delete related data first (foreign key constraints)
    // Delete lt_ItemID_TemplateItemID for items from other businesses
    queryTimed("
        DELETE itl FROM lt_ItemID_TemplateItemID itl
        JOIN Items i ON i.ID = itl.ItemID
        WHERE i.BusinessID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Deleted lt_ItemID_TemplateItemID for other businesses");

    // Delete Items for other businesses
    qItems = queryTimed("
        SELECT COUNT(*) as cnt FROM Items WHERE BusinessID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    queryTimed("
        DELETE FROM Items WHERE BusinessID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Deleted " & qItems.cnt & " items from other businesses");

    // Delete Categories for other businesses
    queryTimed("
        DELETE FROM Categories WHERE BusinessID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Deleted categories from other businesses");

    // Delete Hours for other businesses
    queryTimed("
        DELETE FROM Hours WHERE BusinessID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Deleted hours from other businesses");

    // Delete Employees for other businesses
    try {
        queryTimed("
            DELETE FROM Employees WHERE BusinessID != :keepID
        ", { keepID: keepBusinessID }, { datasource: "payfrit" });
        response.steps.append("Deleted employees from other businesses");
    } catch (any e) {
        response.steps.append("Skipped employees (table may not exist)");
    }

    // Delete ServicePoints for other businesses
    try {
        queryTimed("
            DELETE FROM ServicePoints WHERE BusinessID != :keepID
        ", { keepID: keepBusinessID }, { datasource: "payfrit" });
        response.steps.append("Deleted service points from other businesses");
    } catch (any e) {
        response.steps.append("Skipped service points (table may not exist)");
    }

    // Delete Stations for other businesses
    try {
        queryTimed("
            DELETE FROM Stations WHERE BusinessID != :keepID
        ", { keepID: keepBusinessID }, { datasource: "payfrit" });
        response.steps.append("Deleted stations from other businesses");
    } catch (any e) {
        response.steps.append("Skipped stations (table may not exist)");
    }

    // Delete beacon-business mappings for other businesses
    try {
        queryTimed("
            DELETE FROM lt_BeaconsID_BusinessesID WHERE BusinessID != :keepID
        ", { keepID: keepBusinessID }, { datasource: "payfrit" });
        response.steps.append("Deleted beacon mappings for other businesses");
    } catch (any e) {
        response.steps.append("Skipped beacon mappings (table may not exist)");
    }

    // Finally delete the businesses themselves
    queryTimed("
        DELETE FROM Businesses WHERE ID != :keepID
    ", { keepID: keepBusinessID }, { datasource: "payfrit" });
    response.steps.append("Deleted " & arrayLen(deletedBusinesses) & " businesses");

    // Get beacon status
    qBeacons = queryTimed("
        SELECT sp.ID AS ServicePointID, sp.BeaconID, sp.BusinessID AS BusinessID,
               b.UUID, biz.Name AS BusinessName, sp.Name AS ServicePointName
        FROM ServicePoints sp
        JOIN Beacons b ON b.ID = sp.BeaconID
        JOIN Businesses biz ON biz.ID = sp.BusinessID
        WHERE sp.BeaconID IS NOT NULL
    ", {}, { datasource: "payfrit" });

    beacons = [];
    for (i = 1; i <= qBeacons.recordCount; i++) {
        arrayAppend(beacons, {
            "BeaconID": qBeacons.BeaconID[i],
            "UUID": qBeacons.UUID[i],
            "BusinessID": qBeacons.BusinessID[i],
            "BusinessName": qBeacons.BusinessName[i],
            "ServicePointID": qBeacons.ServicePointID[i],
            "ServicePointName": qBeacons.ServicePointName[i]
        });
    }

    response.OK = true;
    response.deletedBusinesses = deletedBusinesses;
    response.beacons = beacons;

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

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