import { describe, it, expect } from "vitest";
import {
  POPULATION_1KK,
  POPULATION_500K,
  POPULATION_100K,
  POPULAR_RESORTS,
  getCityZoomLevel,
} from "./cityCategory.js";

describe("cityCategory — set sizes", () => {
  it("POPULATION_1KK has 30 entries (Angular parity)", () => {
    expect(POPULATION_1KK.size).toBe(30);
  });

  it("POPULATION_500K has 23 entries", () => {
    expect(POPULATION_500K.size).toBe(23);
  });

  it("POPULATION_100K has 39 entries", () => {
    expect(POPULATION_100K.size).toBe(39);
  });

  it("POPULAR_RESORTS has 15 entries", () => {
    expect(POPULAR_RESORTS.size).toBe(15);
  });
});

describe("cityCategory — membership spot checks", () => {
  it("MOW is in POPULATION_1KK", () => {
    expect(POPULATION_1KK.has("MOW")).toBe(true);
  });

  it("DXB is in POPULAR_RESORTS", () => {
    expect(POPULAR_RESORTS.has("DXB")).toBe(true);
  });

  it("TJM is in POPULATION_500K", () => {
    expect(POPULATION_500K.has("TJM")).toBe(true);
  });

  it("MMK is in POPULATION_100K", () => {
    expect(POPULATION_100K.has("MMK")).toBe(true);
  });
});

describe("getCityZoomLevel", () => {
  it("returns 2 for a 1kk-population city", () => {
    expect(getCityZoomLevel("MOW")).toBe(2);
  });

  it("returns 2 for a popular resort", () => {
    expect(getCityZoomLevel("DXB")).toBe(2);
  });

  it("returns 5 for a 500k-population city", () => {
    expect(getCityZoomLevel("TJM")).toBe(5);
  });

  it("returns 6 for a 100k-population city", () => {
    expect(getCityZoomLevel("MMK")).toBe(6);
  });

  it("returns 6 for an unknown city code", () => {
    expect(getCityZoomLevel("XXX")).toBe(6);
  });
});

// ---------------------------------------------------------------------------
// TZ §4.1.24.3 R23: city-category zoom breakpoints
// ---------------------------------------------------------------------------

describe("§4.1.24.3 R23: city zoom level → MapCanvas tier logic", () => {
  // TZ specifies 3 zoom levels:
  //   min zoom  → cities ≥1M + popular resorts only (zoomLevel 2)
  //   mid zoom  → + 500K–1M cities (zoomLevel 5)
  //   max zoom  → all (zoomLevel 6)
  // getCityZoomLevel maps to these tiers so MapCanvas shows them from that zoom up.

  it("4.1.24-R23: 1M+ cities get the lowest tier (2) — visible from min zoom", () => {
    // MOW, IST, BKK are 1M+ cities — must be visible even at minimum zoom
    expect(getCityZoomLevel("MOW")).toBe(2);
    expect(getCityZoomLevel("IST")).toBe(2);
    expect(getCityZoomLevel("BKK")).toBe(2);
  });

  it("4.1.24-R23: popular resorts also get tier 2 (visible at min zoom)", () => {
    expect(getCityZoomLevel("AER")).toBe(2);
    expect(getCityZoomLevel("HKT")).toBe(2);
    expect(getCityZoomLevel("DXB")).toBe(2);
  });

  it("4.1.24-R23: 500K–1M cities get tier 5 (visible at mid zoom)", () => {
    expect(getCityZoomLevel("TJM")).toBe(5);
    expect(getCityZoomLevel("DOH")).toBe(5);
  });

  it("4.1.24-R23: cities below 500K get tier 6 (visible at max zoom only)", () => {
    expect(getCityZoomLevel("MMK")).toBe(6);
    expect(getCityZoomLevel("GDX")).toBe(6);
    expect(getCityZoomLevel("XXX")).toBe(6); // unknown → max zoom
  });

  it("4.1.24-R23: no known cities are assigned tiers 3 or 4 (unused zoom bands)", () => {
    // TZ defines only 3 logical levels; our implementation uses 2/5/6
    const sampleCodes = ["MOW", "LED", "TJM", "MMK", "AER", "DXB", "DOH", "XXX"];
    for (const code of sampleCodes) {
      const z = getCityZoomLevel(code);
      expect([2, 5, 6]).toContain(z);
    }
  });
});
