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

<cfscript>
// Creates QuickTaskTemplates table if not exists
// Public endpoint for setup

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

try {
    // Create QuickTaskTemplates table
    queryTimed("
        CREATE TABLE IF NOT EXISTS QuickTaskTemplates (
            ID INT AUTO_INCREMENT PRIMARY KEY,
            BusinessID INT NOT NULL,
            Name VARCHAR(100) NOT NULL,
            TaskCategoryID INT NULL,
            TaskTypeID INT NULL,
            Title VARCHAR(255) NOT NULL,
            Details TEXT NULL,
            Icon VARCHAR(30) DEFAULT 'add_box',
            Color VARCHAR(20) DEFAULT '##6366f1',
            SortOrder INT DEFAULT 0,
            IsActive BIT(1) DEFAULT b'1',
            CreatedOn DATETIME DEFAULT CURRENT_TIMESTAMP,
            INDEX idx_business_active (BusinessID, IsActive),
            INDEX idx_sort (BusinessID, SortOrder)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    ", [], { datasource: "payfrit" });

    apiAbort({
        "OK": true,
        "MESSAGE": "QuickTaskTemplates table created/verified"
    });

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