<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">

<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
  <cfset var raw = getHttpRequestData().content>
  <cfif isNull(raw) OR len(trim(raw)) EQ 0>
    <cfreturn {}>
  </cfif>
  <cftry>
    <cfset var data = deserializeJSON(raw)>
    <cfif isStruct(data)>
      <cfreturn data>
    <cfelse>
      <cfreturn {}>
    </cfif>
    <cfcatch>
      <cfreturn {}>
    </cfcatch>
  </cftry>
</cffunction>

<cffunction name="apiAbort" access="public" returntype="void" output="true">
  <cfargument name="payload" type="struct" required="true">
  <cfcontent type="application/json; charset=utf-8">
  <cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
  <cfabort>
</cffunction>

<cfset data = readJsonBody()>
<cfset OrderID = val( structKeyExists(data,"OrderID") ? data.OrderID : 0 )>

<cfif OrderID LTE 0>
  <cfset apiAbort({ "OK": false, "ERROR": "missing_orderid", "MESSAGE": "OrderID is required.", "DETAIL": "" })>
</cfif>

<cftry>
  <cfset qOrder = queryExecute(
    "
      SELECT
        OrderID,
        OrderUUID,
        OrderUserID,
        OrderBusinessID,
        OrderBusinessDeliveryMultiplier,
        OrderTypeID,
        OrderDeliveryFee,
        OrderStatusID,
        OrderAddressID,
        OrderPaymentID,
        OrderRemarks,
        OrderAddedOn,
        OrderLastEditedOn,
        OrderSubmittedOn,
        OrderServicePointID
      FROM Orders
      WHERE OrderID = ?
      LIMIT 1
    ",
    [ { value = OrderID, cfsqltype = "cf_sql_integer" } ],
    { datasource = "payfrit" }
  )>

  <cfif qOrder.recordCount EQ 0>
    <cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Order not found.", "DETAIL": "" })>
  </cfif>

  <!--- Get business delivery fee for display in cart --->
  <cfset qBusiness = queryExecute(
    "SELECT BusinessDeliveryFlatFee FROM Businesses WHERE BusinessID = ? LIMIT 1",
    [ { value = qOrder.OrderBusinessID, cfsqltype = "cf_sql_integer" } ],
    { datasource = "payfrit" }
  )>
  <cfset businessDeliveryFee = qBusiness.recordCount GT 0 ? qBusiness.BusinessDeliveryFlatFee : 0>

  <cfset qLI = queryExecute(
    "
      SELECT
        OrderLineItemID,
        OrderLineItemParentOrderLineItemID,
        OrderLineItemOrderID,
        OrderLineItemItemID,
        OrderLineItemStatusID,
        OrderLineItemPrice,
        OrderLineItemQuantity,
        OrderLineItemRemark,
        OrderLineItemIsDeleted,
        OrderLineItemAddedOn
      FROM OrderLineItems
      WHERE OrderLineItemOrderID = ?
      ORDER BY OrderLineItemID
    ",
    [ { value = OrderID, cfsqltype = "cf_sql_integer" } ],
    { datasource = "payfrit" }
  )>

  <cfset rows = []>
  <cfloop query="qLI">
    <cfset arrayAppend(rows, {
      "OrderLineItemID": qLI.OrderLineItemID,
      "OrderLineItemParentOrderLineItemID": qLI.OrderLineItemParentOrderLineItemID,
      "OrderLineItemOrderID": qLI.OrderLineItemOrderID,
      "OrderLineItemItemID": qLI.OrderLineItemItemID,
      "OrderLineItemStatusID": qLI.OrderLineItemStatusID,
      "OrderLineItemPrice": qLI.OrderLineItemPrice,
      "OrderLineItemQuantity": qLI.OrderLineItemQuantity,
      "OrderLineItemRemark": qLI.OrderLineItemRemark,
      "OrderLineItemIsDeleted": qLI.OrderLineItemIsDeleted,
      "OrderLineItemAddedOn": qLI.OrderLineItemAddedOn
    })>
  </cfloop>

  <cfset apiAbort({
    "OK": true,
    "ERROR": "",
    "ORDER": {
      "OrderID": qOrder.OrderID,
      "OrderUUID": qOrder.OrderUUID,
      "OrderUserID": qOrder.OrderUserID,
      "OrderBusinessID": qOrder.OrderBusinessID,
      "OrderBusinessDeliveryMultiplier": qOrder.OrderBusinessDeliveryMultiplier,
      "OrderTypeID": qOrder.OrderTypeID,
      "OrderDeliveryFee": qOrder.OrderDeliveryFee,
      "BusinessDeliveryFee": businessDeliveryFee,
      "OrderStatusID": qOrder.OrderStatusID,
      "OrderAddressID": qOrder.OrderAddressID,
      "OrderPaymentID": qOrder.OrderPaymentID,
      "OrderRemarks": qOrder.OrderRemarks,
      "OrderAddedOn": qOrder.OrderAddedOn,
      "OrderLastEditedOn": qOrder.OrderLastEditedOn,
      "OrderSubmittedOn": qOrder.OrderSubmittedOn,
      "OrderServicePointID": qOrder.OrderServicePointID
    },
    "ORDERLINEITEMS": rows
  })>

  <cfcatch>
    <cfset apiAbort({
      "OK": false,
      "ERROR": "server_error",
      "MESSAGE": "DB error loading cart",
      "DETAIL": cfcatch.message
    })>
  </cfcatch>
</cftry>
