/**
 * The provenance gate, which is the sharpest correction in CONTRACT.md and the
 * one with an actual licence behind it.
 *
 * The row that must be refused is the one that looks completely fine: correct
 * fields, plausible coordinates, and a provenance of `nominatim`. Serving it
 * would make this endpoint a Publicly Used Derivative Database and pull ODbL
 * §4.3 and §4.4 onto everything served next to it. See CONTRACT.md §8.
 */

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { DEFAULT_PROVENANCE_ALLOWLIST } from "../config.ts";
import { assertPublicShape } from "../markers/gate.ts";

const good = {
  id: "acme-hq",
  label: "Acme",
  colorKey: "sector.industrial",
  lat: 37.79,
  lng: -122.4,
  provenance: "us-census",
};

describe("the public-shape gate", () => {
  it("accepts a US Census row", () => {
    const { accepted, refused } = assertPublicShape([good], DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(accepted.length, 1);
    assert.deepEqual(refused, []);
    assert.equal(accepted[0]?.provenance, "us-census");
  });

  it("refuses an OSM-derived row that is otherwise perfect", () => {
    const row = { ...good, provenance: "nominatim" };
    const { accepted, refused } = assertPublicShape([row], DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(accepted.length, 0);
    assert.equal(refused.length, 1);
    assert.match(refused[0]?.reason ?? "", /allowlist/);
  });

  it("refuses a row with no provenance at all", () => {
    const { provenance: _omitted, ...row } = good;
    const { accepted } = assertPublicShape([row], DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(accepted.length, 0);
  });

  it("refuses commercial geocoders too — 'not OSM' is not the test", () => {
    for (const provenance of ["google", "mapbox", "here"]) {
      const { accepted } = assertPublicShape(
        [{ ...good, provenance }],
        DEFAULT_PROVENANCE_ALLOWLIST,
      );
      assert.equal(accepted.length, 0, `${provenance} must not pass`);
    }
  });

  it("refuses the whole row when it carries a field nobody reviewed", () => {
    const row = { ...good, ownerEmail: "someone@example.com" };
    const { accepted, refused } = assertPublicShape([row], DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(accepted.length, 0);
    assert.match(refused[0]?.reason ?? "", /unknown field "ownerEmail"/);
  });

  it("refuses malformed coordinates", () => {
    const rows = [
      { ...good, lat: 200 },
      { ...good, lng: "west" },
      { ...good, id: "" },
    ];
    const { accepted } = assertPublicShape(rows, DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(accepted.length, 0);
  });

  it("aggregates refusals so a broken sync is one line, not ten thousand", () => {
    const rows = Array.from({ length: 500 }, () => ({ ...good, provenance: "osm" }));
    const { refused } = assertPublicShape(rows, DEFAULT_PROVENANCE_ALLOWLIST);
    assert.equal(refused.length, 1);
    assert.equal(refused[0]?.count, 500);
  });
});
