/**
 * ePay county/UAT mapping — bridges eTerra WORKSPACE_ID to ePay county indices.
 *
 * ePay uses alphabetical indices 0-41 for counties.
 * eTerra uses WORKSPACE_IDs (10, 29, 38, ...).
 * Our DB (GisUat) stores county names with diacritics ("Cluj", "Argeș").
 *
 * Resolution chain:
 *   SIRUTA → GisUat.county → resolveEpayCountyIndex() → ePay countyIdx
 */

/** ePay county index → county name (uppercase, no diacritics) */
export const EPAY_COUNTIES: Record<number, string> = {
  0: "ALBA",
  1: "ARAD",
  2: "ARGES",
  3: "BACAU",
  4: "BIHOR",
  5: "BISTRITA NASAUD",
  6: "BOTOSANI",
  7: "BRAILA",
  8: "BRASOV",
  9: "BUCURESTI",
  10: "BUZAU",
  11: "CALARASI",
  12: "CARASSEVERIN",
  13: "CLUJ",
  14: "CONSTANTA",
  15: "COVASNA",
  16: "DAMBOVITA",
  17: "DOLJ",
  18: "GALATI",
  19: "GIURGIU",
  20: "GORJ",
  21: "HARGHITA",
  22: "HUNEDOARA",
  23: "IALOMITA",
  24: "IASI",
  25: "ILFOV",
  26: "MARAMURES",
  27: "MEHEDINTI",
  28: "MURES",
  29: "NEAMT",
  30: "OLT",
  31: "PRAHOVA",
  32: "SALAJ",
  33: "SATU MARE",
  34: "SIBIU",
  35: "SUCEAVA",
  36: "TELEORMAN",
  37: "TIMIS",
  38: "TULCEA",
  39: "VALCEA",
  40: "VASLUI",
  41: "VRANCEA",
};

/** Reverse: ePay county name → index */
const EPAY_NAME_TO_INDEX = new Map<string, number>();
for (const [idx, name] of Object.entries(EPAY_COUNTIES)) {
  EPAY_NAME_TO_INDEX.set(name, Number(idx));
}

/**
 * eTerra county name (with diacritics) → ePay county index.
 * Maps from WORKSPACE_TO_COUNTY values to ePay indices.
 */
const ETERRA_TO_EPAY: Record<string, number> = {
  Alba: 0,
  Arad: 1,
  Argeș: 2,
  Bacău: 3,
  Bihor: 4,
  "Bistrița-Năsăud": 5,
  Botoșani: 6,
  Brașov: 8,
  Brăila: 7,
  Buzău: 10,
  "Caraș-Severin": 12,
  Cluj: 13,
  Constanța: 14,
  Covasna: 15,
  Dâmbovița: 16,
  Dolj: 17,
  Galați: 18,
  Gorj: 20,
  Harghita: 21,
  Hunedoara: 22,
  Ialomița: 23,
  Iași: 24,
  Ilfov: 25,
  Maramureș: 26,
  Mehedinți: 27,
  Mureș: 28,
  Neamț: 29,
  Olt: 30,
  Prahova: 31,
  "Satu Mare": 33,
  Sălaj: 32,
  Sibiu: 34,
  Suceava: 35,
  Teleorman: 36,
  Timiș: 37,
  Tulcea: 38,
  Vaslui: 40,
  Vâlcea: 39,
  Vrancea: 41,
  București: 9,
  Călărași: 11,
  Giurgiu: 19,
};

/** Strip diacritics and uppercase for fuzzy matching */
function normalize(s: string): string {
  return s
    .normalize("NFD")
    .replace(/[\u0300-\u036f]/g, "")
    .toUpperCase()
    .replace(/[^A-Z\s]/g, "")
    .trim();
}

/**
 * Resolve ePay county index from a county name (with or without diacritics).
 * Tries exact match first, then normalized match.
 */
export function resolveEpayCountyIndex(county: string): number | null {
  // Direct match (eTerra county name with diacritics)
  const direct = ETERRA_TO_EPAY[county];
  if (direct !== undefined) return direct;

  // Normalized match against ePay names
  const norm = normalize(county);
  const fromEpay = EPAY_NAME_TO_INDEX.get(norm);
  if (fromEpay !== undefined) return fromEpay;

  // Fuzzy: try without hyphens/spaces
  const compact = norm.replace(/\s+/g, "");
  for (const [name, idx] of EPAY_NAME_TO_INDEX) {
    if (name.replace(/\s+/g, "") === compact) return idx;
  }

  return null;
}

/**
 * Get ePay county display name from index.
 */
export function getEpayCountyName(index: number): string | null {
  return EPAY_COUNTIES[index] ?? null;
}
