<cfsetting showdebugoutput="false">
<cfcontent type="application/json" reset="true">
<cfscript>
// One-time fix: remove # prefix from BrandColor
qBefore = queryExecute("
  SELECT ID, BrandColor
  FROM Businesses
  WHERE BrandColor LIKE :pattern
", { pattern: { value: "##%", cfsqltype: "cf_sql_varchar" } }, { datasource: "payfrit" });

if (qBefore.recordCount > 0) {
  queryExecute("
    UPDATE Businesses
    SET BrandColor = SUBSTRING(BrandColor, 2)
    WHERE BrandColor LIKE :pattern
  ", { pattern: { value: "##%", cfsqltype: "cf_sql_varchar" } }, { datasource: "payfrit" });
}

qAfter = queryExecute("
  SELECT ID, BrandColor
  FROM Businesses
  WHERE BrandColor IS NOT NULL AND LENGTH(BrandColor) > 0
", {}, { datasource: "payfrit" });

rows = [];
for (i = 1; i <= qAfter.recordCount; i++) {
  arrayAppend(rows, {
    "BusinessID": qAfter.BusinessID[i],
    "BrandColor": qAfter.BrandColor[i]
  });
}

writeOutput(serializeJSON({
  "OK": true,
  "FIXED": qBefore.recordCount,
  "CURRENT": rows
}));
</cfscript>
