/**
 * Ethiopia's first-level administrative divisions: regional states plus the two
 * chartered city administrations.
 *
 * NOTE — this list churned recently and should be re-confirmed with EDR before
 * being treated as final. SNNPR was progressively dissolved: Sidama split off in
 * 2020, South West Ethiopia Peoples' in 2021, and the remainder became Central
 * Ethiopia and South Ethiopia in 2023. Records created before those splits may
 * still carry "SNNPR" or "Southern Nations".
 */
export const ETHIOPIAN_REGIONS = [
  "Addis Ababa",
  "Afar",
  "Amhara",
  "Benishangul-Gumuz",
  "Central Ethiopia",
  "Dire Dawa",
  "Gambela",
  "Harari",
  "Oromia",
  "Sidama",
  "Somali",
  "South Ethiopia",
  "South West Ethiopia Peoples'",
  "Tigray",
] as const;

export type EthiopianRegion = (typeof ETHIOPIAN_REGIONS)[number];

/**
 * Free-text region values seen in the wild — eTrade returns uncoded uppercase
 * strings, and pre-dropdown rows were hand-typed. Keys are lowercased and
 * whitespace-collapsed before lookup, so only spelling/naming variants belong
 * here, not case variants.
 */
const REGION_ALIASES: Record<string, EthiopianRegion> = {
  // Spelling and transliteration variants.
  oromoia: "Oromia",
  oromiya: "Oromia",
  oromina: "Oromia",
  addisabeba: "Addis Ababa",
  "addis abeba": "Addis Ababa",
  "dire dawa city": "Dire Dawa",
  "benishangul gumuz": "Benishangul-Gumuz",
  "benshangul-gumuz": "Benishangul-Gumuz",
  "beneshangul-gumuz": "Benishangul-Gumuz",
  gambella: "Gambela",
  tigrai: "Tigray",
  tigre: "Tigray",
  "south west ethiopia": "South West Ethiopia Peoples'",
  "south west ethiopia peoples": "South West Ethiopia Peoples'",
  "southwest ethiopia": "South West Ethiopia Peoples'",

  // eTrade frequently returns a ZONE in the Region slot. These map the zone back
  // to its parent region; the zone itself is preserved in the `zone` field.
  "eastern tigray": "Tigray",
  "western tigray": "Tigray",
  "southern tigray": "Tigray",
  "central tigray": "Tigray",
  "north wollo": "Amhara",
  "south wollo": "Amhara",
  "east gojjam": "Amhara",
  "west gojjam": "Amhara",
  "north shewa": "Amhara",
  "east shewa": "Oromia",
  "west shewa": "Oromia",
  "arsi": "Oromia",
  "bale": "Oromia",
  "borena": "Oromia",
  "jimma": "Oromia",
  "gamo": "South Ethiopia",
  "gofa": "South Ethiopia",
  "wolayita": "South Ethiopia",
};

/** Collapse whitespace and case so lookups are tolerant of formatting noise. */
function canonicalKey(value: string): string {
  return value.trim().replace(/\s+/g, " ").toLowerCase();
}

/**
 * Map an arbitrary region string (eTrade payload, legacy row, hand entry) onto a
 * canonical region. Returns `null` when it cannot be resolved — callers should
 * leave the field empty and let the user pick, rather than persisting a guess.
 */
export function normalizeRegion(
  value: string | null | undefined,
): EthiopianRegion | null {
  if (!value) return null;
  const key = canonicalKey(value);
  if (!key) return null;

  const exact = ETHIOPIAN_REGIONS.find((r) => canonicalKey(r) === key);
  if (exact) return exact;

  return REGION_ALIASES[key] ?? null;
}
