<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;
}

function readJsonBody() {
  raw = toString(getHttpRequestData().content);
  if (isNull(raw) || len(trim(raw)) EQ 0) return {};
  try {
    parsed = deserializeJSON(raw);
  } catch(any e) {
    apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" });
  }
  // We only accept object bodies
  if (!isStruct(parsed)) return {};
  return parsed;
}

data = readJsonBody();

// Auth/business gating should already happen in /api/Application.cfm,
// but keep this defensive so the file is safe standalone.
if (!structKeyExists(request, "UserID") || !isNumeric(request.UserID) || request.UserID LTE 0) {
  apiAbort({ OK=false, ERROR="not_logged_in" });
}
if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) {
  apiAbort({ OK=false, ERROR="no_business_selected" });
}

// Default behavior: only active beacons unless onlyActive is explicitly false/0
onlyActive = true;
if (structKeyExists(data, "onlyActive")) {
  if (isBoolean(data.onlyActive)) {
    onlyActive = data.onlyActive;
  } else if (isNumeric(data.onlyActive)) {
    onlyActive = (int(data.onlyActive) EQ 1);
  } else if (isSimpleValue(data.onlyActive)) {
    onlyActive = (lcase(trim(toString(data.onlyActive))) EQ "true");
  }
}
</cfscript>

<cfquery name="q" datasource="#application.datasource#">
  SELECT
    BeaconID,
    BusinessID,
    BeaconName,
    UUID,
    NamespaceId,
    InstanceId,
    IsActive,
    CreatedAt,
    UpdatedAt
  FROM Beacons
  WHERE BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
  <cfif onlyActive>
    AND IsActive = 1
  </cfif>
  ORDER BY BeaconName, BeaconID
</cfquery>

<cfset beacons = []>
<cfloop query="q">
  <cfset arrayAppend(beacons, {
    "BeaconID"    = q.BeaconID,
    "BusinessID"  = q.BusinessID,
    "BeaconName"  = q.BeaconName,
    "UUID"        = q.UUID,
    "NamespaceId" = q.NamespaceId,
    "InstanceId"  = q.InstanceId,
    "IsActive"    = q.IsActive,
    "CreatedAt"   = q.CreatedAt,
    "UpdatedAt"   = q.UpdatedAt
  })>
</cfloop>

<cfoutput>#serializeJSON({ OK=true, ERROR="", BusinessID=request.BusinessID, COUNT=arrayLen(beacons), BEACONS=beacons })#</cfoutput>
