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

<cfscript>
// Update Beacon 2 to point to In-N-Out (BusinessID 17)
beaconId = 2;
newBusinessId = 17;

queryExecute("
  UPDATE lt_Beacon_Businesses_ServicePoints
  SET BusinessID = :newBizId
  WHERE BeaconID = :beaconId
", { newBizId: newBusinessId, beaconId: beaconId }, { datasource: "payfrit" });

// Get current state
q = queryExecute("
  SELECT
    b.BeaconID,
    b.BeaconUUID,
    b.BeaconName,
    lt.BusinessID,
    lt.ServicePointID,
    biz.BusinessName,
    sp.ServicePointName
  FROM Beacons b
  LEFT JOIN lt_Beacon_Businesses_ServicePoints lt ON lt.BeaconID = b.BeaconID
  LEFT JOIN Businesses biz ON biz.BusinessID = lt.BusinessID
  LEFT JOIN ServicePoints sp ON sp.ServicePointID = lt.ServicePointID
  WHERE b.BeaconIsActive = 1
  ORDER BY b.BeaconID
", {}, { datasource: "payfrit" });

rows = [];
for (row in q) {
  arrayAppend(rows, {
    "BeaconID": row.BeaconID,
    "BeaconUUID": row.BeaconUUID,
    "BeaconName": row.BeaconName ?: "",
    "BusinessID": row.BusinessID ?: 0,
    "BusinessName": row.BusinessName ?: "",
    "ServicePointID": row.ServicePointID ?: 0,
    "ServicePointName": row.ServicePointName ?: ""
  });
}

writeOutput(serializeJSON({
  "OK": true,
  "MESSAGE": "Updated beacon #beaconId# to BusinessID #newBusinessId#",
  "BEACONS": rows
}));
</cfscript>
