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

<cfscript>
businessId = 27; // Big Dean's
response = { "OK": false };

try {
    // Check if Big Dean's already has stations
    existing = queryTimed("
        SELECT COUNT(*) as cnt FROM Stations WHERE BusinessID = :bizId
    ", { bizId: businessId });

    if (existing.cnt == 0) {
        // Insert Kitchen station
        queryTimed("
            INSERT INTO Stations (BusinessID, Name, Color, SortOrder, IsActive)
            VALUES (:bizId, 'Kitchen', :color1, 1, 1)
        ", { bizId: businessId, color1: "##FF5722" });

        // Insert Bar station
        queryTimed("
            INSERT INTO Stations (BusinessID, Name, Color, SortOrder, IsActive)
            VALUES (:bizId, 'Bar', :color2, 2, 1)
        ", { bizId: businessId, color2: "##2196F3" });

        response["ACTION"] = "inserted";
    } else {
        response["ACTION"] = "already_exists";
    }

    // Get current stations
    stations = queryTimed("
        SELECT ID, Name, Color, SortOrder
        FROM Stations
        WHERE BusinessID = :bizId AND IsActive = 1
        ORDER BY SortOrder
    ", { bizId: businessId });

    stationArr = [];
    for (s in stations) {
        arrayAppend(stationArr, {
            "StationID": s.ID,
            "Name": s.Name,
            "Color": s.Color
        });
    }

    response["OK"] = true;
    response["BUSINESS_ID"] = businessId;
    response["STATIONS"] = stationArr;

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

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