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

<cfscript>
/**
 * TEST ONLY: Mark an order as submitted and paid without Stripe
 * This bypasses the payment flow for development testing
 *
 * POST: { OrderID: int }
 */

response = { "OK": false };

try {
    requestData = deserializeJSON(toString(getHttpRequestData().content));
    orderID = val(requestData.OrderID ?: 0);

    if (orderID == 0) {
        response["ERROR"] = "OrderID is required";
        writeOutput(serializeJSON(response));
        abort;
    }

    // Check order exists
    qOrder = queryExecute("
        SELECT ID, StatusID, PaymentStatus FROM Orders WHERE ID = :orderID
    ", { orderID: orderID }, { datasource: "payfrit" });

    if (qOrder.recordCount == 0) {
        response["ERROR"] = "Order not found";
        writeOutput(serializeJSON(response));
        abort;
    }

    // Mark as submitted and paid
    queryExecute("
        UPDATE Orders
        SET StatusID = 1,
            PaymentStatus = 'paid',
            PaymentCompletedOn = NOW(),
            SubmittedOn = COALESCE(SubmittedOn, NOW()),
            LastEditedOn = NOW()
        WHERE ID = :orderID
    ", { orderID: orderID }, { datasource: "payfrit" });

    response["OK"] = true;
    response["MESSAGE"] = "Order #orderID# marked as submitted and paid";

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

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