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

<cfcontent type="application/json; charset=utf-8" reset="true">
<cfheader name="Cache-Control" value="no-store">

<cfscript>
function apiAbort(obj) {
  writeOutput(serializeJSON(obj));
  abort;
}

if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) {
  apiAbort({ OK=false, ERROR="no_business_selected" });
}
</cfscript>

<!--- Check if this business uses beacon sharding --->
<cfquery name="qBiz" datasource="payfrit">
  SELECT BeaconShardID, BeaconMajor
  FROM Businesses
  WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
  LIMIT 1
</cfquery>

<cfset usesSharding = qBiz.recordCount GT 0 AND val(qBiz.BeaconShardID) GT 0>
<cfset assignments = []>

<cfif usesSharding>
  <!--- Sharding: assignments are ServicePoints with BeaconMinor --->
  <cfquery name="qShard" datasource="payfrit">
    SELECT UUID FROM BeaconShards WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#qBiz.BeaconShardID#">
  </cfquery>
  <cfset shardUUID = qShard.recordCount GT 0 ? qShard.UUID : "">

  <cfquery name="qSharding" datasource="payfrit">
    SELECT
      sp.ID AS ServicePointID,
      sp.BeaconMinor,
      sp.Name AS ServicePointName
    FROM ServicePoints sp
    WHERE sp.BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
      AND sp.BeaconMinor IS NOT NULL
      AND sp.IsActive = 1
    ORDER BY sp.BeaconMinor, sp.Name
  </cfquery>

  <cfloop query="qSharding">
    <cfset arrayAppend(assignments, {
      "ServicePointID"  = qSharding.ServicePointID,
      "BeaconID"        = 0,
      "BusinessID"      = request.BusinessID,
      "BeaconName"      = qSharding.ServicePointName & " (Minor " & qSharding.BeaconMinor & ")",
      "UUID"            = shardUUID,
      "Major"           = qBiz.BeaconMajor,
      "Minor"           = qSharding.BeaconMinor,
      "ServicePointName"= qSharding.ServicePointName,
      "IsSharding"      = true
    })>
  </cfloop>

<cfelse>
  <!--- Legacy: assignments via ServicePoints.BeaconID -> Beacons --->
  <cfquery name="q" datasource="payfrit">
    SELECT
      sp.ID AS ServicePointID,
      sp.BeaconID,
      sp.BusinessID AS BusinessID,
      sp.AssignedByUserID,
      b.Name AS BeaconName,
      b.UUID,
      sp.Name AS ServicePointName
    FROM ServicePoints sp
    JOIN Beacons b ON b.ID = sp.BeaconID
    WHERE sp.BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
      AND sp.BeaconID IS NOT NULL
    ORDER BY b.Name, sp.Name
  </cfquery>

  <cfloop query="q">
    <cfset arrayAppend(assignments, {
      "ServicePointID"  = q.ServicePointID,
      "BeaconID"        = q.BeaconID,
      "BusinessID"      = q.BusinessID,
      "BeaconName"      = q.BeaconName,
      "UUID"            = q.UUID,
      "ServicePointName"= q.ServicePointName,
      "IsSharding"      = false
    })>
  </cfloop>
</cfif>

<cfoutput>#serializeJSON({ OK=true, ERROR="", COUNT=arrayLen(assignments), ASSIGNMENTS=assignments, USES_SHARDING=usesSharding })#</cfoutput>
