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

<cfscript>
// Delete (deactivate) a task category
// Input: BusinessID, TaskCategoryID
// Output: { OK: true }

function apiAbort(required struct payload) {
    writeOutput(serializeJSON(payload));
    abort;
}

function readJsonBody() {
    var raw = getHttpRequestData().content;
    if (isNull(raw)) raw = "";
    if (!len(trim(raw))) return {};
    try {
        var data = deserializeJSON(raw);
        if (isStruct(data)) return data;
    } catch (any e) {}
    return {};
}

try {
    data = readJsonBody();

    // Get BusinessID
    businessID = 0;
    httpHeaders = getHttpRequestData().headers;

    if (structKeyExists(url, "bid") && isNumeric(url.bid)) {
        businessID = int(url.bid);
    } else if (structKeyExists(data, "BusinessID") && isNumeric(data.BusinessID)) {
        businessID = int(data.BusinessID);
    } else if (structKeyExists(httpHeaders, "X-Business-ID") && isNumeric(httpHeaders["X-Business-ID"])) {
        businessID = int(httpHeaders["X-Business-ID"]);
    }

    if (businessID == 0) {
        apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "BusinessID is required" });
    }

    // Get CategoryID
    categoryID = structKeyExists(data, "TaskCategoryID") && isNumeric(data.TaskCategoryID) ? int(data.TaskCategoryID) : 0;

    if (categoryID == 0) {
        apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "TaskCategoryID is required" });
    }

    // Verify ownership
    qCheck = queryTimed("
        SELECT ID FROM TaskCategories
        WHERE ID = :id AND BusinessID = :businessID
    ", {
        id: { value: categoryID, cfsqltype: "cf_sql_integer" },
        businessID: { value: businessID, cfsqltype: "cf_sql_integer" }
    }, { datasource: "payfrit" });

    if (qCheck.recordCount == 0) {
        apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Category not found" });
    }

    // Check if any tasks use this category
    qTasks = queryTimed("
        SELECT COUNT(*) as cnt FROM Tasks
        WHERE CategoryID = :id
    ", {
        id: { value: categoryID, cfsqltype: "cf_sql_integer" }
    }, { datasource: "payfrit" });

    if (qTasks.cnt > 0) {
        // Soft delete - just deactivate
        queryTimed("
            UPDATE TaskCategories SET IsActive = 0
            WHERE ID = :id
        ", {
            id: { value: categoryID, cfsqltype: "cf_sql_integer" }
        }, { datasource: "payfrit" });

        apiAbort({
            "OK": true,
            "MESSAGE": "Category deactivated (has " & qTasks.cnt & " tasks)"
        });
    } else {
        // Hard delete - no tasks use it
        queryTimed("
            DELETE FROM TaskCategories WHERE ID = :id
        ", {
            id: { value: categoryID, cfsqltype: "cf_sql_integer" }
        }, { datasource: "payfrit" });

        apiAbort({
            "OK": true,
            "MESSAGE": "Category deleted"
        });
    }

} catch (any e) {
    apiAbort({
        "OK": false,
        "ERROR": "server_error",
        "MESSAGE": e.message
    });
}
</cfscript>
