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

<!--- Only allow from localhost --->
<cfif NOT (cgi.remote_addr EQ "127.0.0.1" OR cgi.remote_addr EQ "::1" OR findNoCase("localhost", cgi.server_name))>
    <cfoutput>#serializeJSON({"OK": false, "ERROR": "admin_only"})#</cfoutput>
    <cfabort>
</cfif>

<cfscript>
/**
 * Cleanup Categories - Final step after migration verification
 *
 * This script:
 * 1. Verifies all Items have BusinessID set
 * 2. Finds orphan items (ParentID=0, no children, not in links)
 * 3. Drops CategoryID column
 * 4. Drops IsModifierTemplate column (derived from lt_ItemID_TemplateItemID now)
 * 5. Drops Categories table
 *
 * Query param: ?confirm=YES to actually execute (otherwise shows verification only)
 */

response = { "OK": false, "verification": {}, "orphans": [], "steps": [] };

try {
    confirm = structKeyExists(url, "confirm") && url.confirm == "YES";

    // Verification Step 1: Check for items without BusinessID
    qNoBusinessID = queryExecute("
        SELECT COUNT(*) as cnt FROM Items
        WHERE BusinessID IS NULL OR BusinessID = 0
    ", {}, { datasource: "payfrit" });

    response.verification["itemsWithoutBusinessID"] = qNoBusinessID.cnt;

    // Verification Step 2: Check that all categories were converted
    qCategories = queryExecute("
        SELECT COUNT(*) as cnt FROM Categories
    ", {}, { datasource: "payfrit" });

    response.verification["categoriesRemaining"] = qCategories.cnt;

    // Verification Step 3: Check category Items exist (ParentID=0 with children)
    qCategoryItems = queryExecute("
        SELECT COUNT(DISTINCT p.ItemID) as cnt
        FROM Items p
        INNER JOIN Items c ON c.ParentItemID = p.ItemID
        WHERE p.ParentItemID = 0
          AND p.BusinessID > 0
          AND NOT EXISTS (
              SELECT 1 FROM lt_ItemID_TemplateItemID tl WHERE tl.TemplateItemID = p.ItemID
          )
    ", {}, { datasource: "payfrit" });

    response.verification["categoryItemsCreated"] = qCategoryItems.cnt;

    // Verification Step 4: Check templates exist (in lt_ItemID_TemplateItemID)
    qTemplates = queryExecute("
        SELECT COUNT(DISTINCT tl.TemplateItemID) as cnt
        FROM lt_ItemID_TemplateItemID tl
        INNER JOIN Items t ON t.ItemID = tl.TemplateItemID
    ", {}, { datasource: "payfrit" });

    response.verification["templatesInLinks"] = qTemplates.cnt;

    // Verification Step 5: Find orphans at ParentID=0
    // Orphan = ParentID=0, no children pointing to it, not in lt_ItemID_TemplateItemID
    qOrphans = queryExecute("
        SELECT i.ID, i.Name, i.BusinessID
        FROM Items i
        WHERE i.ParentItemID = 0
          AND NOT EXISTS (
              SELECT 1 FROM Items child WHERE child.ParentItemID = i.ID
          )
          AND NOT EXISTS (
              SELECT 1 FROM lt_ItemID_TemplateItemID tl WHERE tl.TemplateItemID = i.ID
          )
        ORDER BY i.BusinessID, i.Name
    ", {}, { datasource: "payfrit" });

    response.verification["orphanCount"] = qOrphans.recordCount;

    for (orphan in qOrphans) {
        arrayAppend(response.orphans, {
            "ItemID": orphan.ItemID,
            "Name": orphan.Name,
            "BusinessID": orphan.BusinessID
        });
    }

    // Summary
    safeToCleanup = (qNoBusinessID.cnt == 0);
    response.verification["safeToCleanup"] = safeToCleanup;

    if (!safeToCleanup) {
        arrayAppend(response.steps, "VERIFICATION FAILED - Cannot cleanup yet");
        arrayAppend(response.steps, "- " & qNoBusinessID.cnt & " items still missing BusinessID");
        response["OK"] = false;
        writeOutput(serializeJSON(response));
        abort;
    }

    if (qOrphans.recordCount > 0) {
        arrayAppend(response.steps, "WARNING: " & qOrphans.recordCount & " orphan items found (see orphans array)");
        arrayAppend(response.steps, "These will NOT be deleted - review and handle manually if needed");
    }

    arrayAppend(response.steps, "Verification passed - safe to cleanup");

    if (!confirm) {
        arrayAppend(response.steps, "Add ?confirm=YES to execute cleanup");
        response["OK"] = true;
        writeOutput(serializeJSON(response));
        abort;
    }

    // Execute cleanup
    arrayAppend(response.steps, "Executing cleanup...");

    // Step 1: Drop CategoryID column
    try {
        queryExecute("
            ALTER TABLE Items DROP COLUMN CategoryID
        ", {}, { datasource: "payfrit" });
        arrayAppend(response.steps, "Dropped CategoryID column from Items");
    } catch (any e) {
        if (findNoCase("check that column", e.message) || findNoCase("Unknown column", e.message)) {
            arrayAppend(response.steps, "CategoryID column already dropped");
        } else {
            arrayAppend(response.steps, "Warning dropping CategoryID: " & e.message);
        }
    }

    // Step 2: Drop IsModifierTemplate column (now derived from lt_ItemID_TemplateItemID)
    try {
        queryExecute("
            ALTER TABLE Items DROP COLUMN IsModifierTemplate
        ", {}, { datasource: "payfrit" });
        arrayAppend(response.steps, "Dropped IsModifierTemplate column from Items");
    } catch (any e) {
        if (findNoCase("check that column", e.message) || findNoCase("Unknown column", e.message)) {
            arrayAppend(response.steps, "IsModifierTemplate column already dropped");
        } else {
            arrayAppend(response.steps, "Warning dropping IsModifierTemplate: " & e.message);
        }
    }

    // Step 3: Drop Categories table
    try {
        queryExecute("
            DROP TABLE Categories
        ", {}, { datasource: "payfrit" });
        arrayAppend(response.steps, "Dropped Categories table");
    } catch (any e) {
        if (findNoCase("Unknown table", e.message)) {
            arrayAppend(response.steps, "Categories table already dropped");
        } else {
            arrayAppend(response.steps, "Warning dropping Categories: " & e.message);
        }
    }

    response["OK"] = true;
    arrayAppend(response.steps, "CLEANUP COMPLETE - Schema simplified");

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

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