import { describe, expect, it } from "vitest";
import {
  CATEGORY_TABLES,
  CATEGORY_VALUES,
  LANDLORD_CATEGORIES,
  isLandlordCategory,
} from "./categories";
import { CLASSIFIER_FIELD_VALUES } from "@/lib/bulkUpload/columnFields";
import { OverrideComponentValues } from "@/app/db/schema/property_overrides";

// These tests guard the exact failure that left five categories rendering
// "not classified": a category defined in one place (schema table, mapping
// field, override component) but missing from the app-layer registry. If any of
// these sets drift apart, wiring is incomplete somewhere — fail loudly here
// rather than silently in the UI.
describe("landlord override category registry", () => {
  const expected = [...LANDLORD_CATEGORIES].sort();

  it("registry, value map, and table map cover the identical category set", () => {
    expect(Object.keys(CATEGORY_TABLES).sort()).toEqual(expected);
    expect(Object.keys(CATEGORY_VALUES).sort()).toEqual(expected);
  });

  it("covers exactly the classifier mapping fields", () => {
    // Every classifier column the user can map must be wired, and vice versa —
    // otherwise a mapped column classifies into a table nothing reads.
    expect([...CLASSIFIER_FIELD_VALUES].sort()).toEqual(expected);
  });

  it("matches the property_overrides override_component enum", () => {
    // The finaliser maps classifier category -> override_component with no
    // translation, so the two vocabularies must be identical.
    expect([...OverrideComponentValues].sort()).toEqual(expected);
  });

  it("every category has a non-empty value list that includes Unknown", () => {
    // 'Unknown' is the classifier's fallback and the token the finalise gate
    // (ADR-0006) blocks on; a category missing it would let Unknowns slip past.
    for (const category of LANDLORD_CATEGORIES) {
      expect(CATEGORY_VALUES[category].length).toBeGreaterThan(0);
      expect(CATEGORY_VALUES[category]).toContain("Unknown");
    }
  });

  it("isLandlordCategory accepts wired categories and rejects others", () => {
    for (const category of LANDLORD_CATEGORIES) {
      expect(isLandlordCategory(category)).toBe(true);
    }
    expect(isLandlordCategory("address_1")).toBe(false);
    expect(isLandlordCategory("not_a_category")).toBe(false);
  });
});
