// Canonical team identities: ISO code, flag, and alias normalization so the
// three data sources (openfootball, martj42 results CSV, football-data.org)
// resolve to ONE name. Names follow openfootball's spelling as canonical.

interface TeamMeta {
  /** ISO 3166-1 alpha-2 (drives the flag emoji), or a special key. */
  iso2: string;
  /** Override flag emoji for non-ISO entities (home nations). */
  flag?: string;
}

/** Canonical name → metadata. The 48 World Cup 2026 qualifiers. */
const TEAMS: Record<string, TeamMeta> = {
  'Czech Republic': { iso2: 'CZ' },
  Mexico: { iso2: 'MX' },
  'South Africa': { iso2: 'ZA' },
  'South Korea': { iso2: 'KR' },
  'Bosnia & Herzegovina': { iso2: 'BA' },
  Canada: { iso2: 'CA' },
  Qatar: { iso2: 'QA' },
  Switzerland: { iso2: 'CH' },
  Brazil: { iso2: 'BR' },
  Haiti: { iso2: 'HT' },
  Morocco: { iso2: 'MA' },
  Scotland: { iso2: 'GB', flag: '🏴\u{E0067}\u{E0062}\u{E0073}\u{E0063}\u{E0074}\u{E007F}' },
  Australia: { iso2: 'AU' },
  Paraguay: { iso2: 'PY' },
  Turkey: { iso2: 'TR' },
  USA: { iso2: 'US' },
  'Curaçao': { iso2: 'CW' },
  Ecuador: { iso2: 'EC' },
  Germany: { iso2: 'DE' },
  'Ivory Coast': { iso2: 'CI' },
  Japan: { iso2: 'JP' },
  Netherlands: { iso2: 'NL' },
  Sweden: { iso2: 'SE' },
  Tunisia: { iso2: 'TN' },
  Belgium: { iso2: 'BE' },
  Egypt: { iso2: 'EG' },
  Iran: { iso2: 'IR' },
  'New Zealand': { iso2: 'NZ' },
  'Cape Verde': { iso2: 'CV' },
  'Saudi Arabia': { iso2: 'SA' },
  Spain: { iso2: 'ES' },
  Uruguay: { iso2: 'UY' },
  France: { iso2: 'FR' },
  Iraq: { iso2: 'IQ' },
  Norway: { iso2: 'NO' },
  Senegal: { iso2: 'SN' },
  Algeria: { iso2: 'DZ' },
  Argentina: { iso2: 'AR' },
  Austria: { iso2: 'AT' },
  Jordan: { iso2: 'JO' },
  Colombia: { iso2: 'CO' },
  'DR Congo': { iso2: 'CD' },
  Portugal: { iso2: 'PT' },
  Uzbekistan: { iso2: 'UZ' },
  Croatia: { iso2: 'HR' },
  England: { iso2: 'GB', flag: '🏴\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}' },
  Ghana: { iso2: 'GH' },
  Panama: { iso2: 'PA' },
};

/** Variant spelling → canonical name (lowercased keys for case-insensitivity). */
const ALIASES: Record<string, string> = {
  'bosnia and herzegovina': 'Bosnia & Herzegovina',
  'bosnia-herzegovina': 'Bosnia & Herzegovina',
  'korea republic': 'South Korea',
  'republic of korea': 'South Korea',
  korea: 'South Korea',
  'united states': 'USA',
  'united states of america': 'USA',
  'usmnt': 'USA',
  'türkiye': 'Turkey',
  turkiye: 'Turkey',
  "côte d'ivoire": 'Ivory Coast',
  'cote d ivoire': 'Ivory Coast',
  'ivory coast': 'Ivory Coast',
  'cape verde islands': 'Cape Verde',
  'cabo verde': 'Cape Verde',
  'dr congo': 'DR Congo',
  'congo dr': 'DR Congo',
  'democratic republic of the congo': 'DR Congo',
  'czechia': 'Czech Republic',
  curacao: 'Curaçao',
  'iran': 'Iran',
  'ir iran': 'Iran',
};

function isoToFlag(iso2: string): string {
  if (iso2.length !== 2) return '🏳️';
  const A = 0x1f1e6;
  const cc = iso2.toUpperCase();
  return String.fromCodePoint(A + (cc.charCodeAt(0) - 65), A + (cc.charCodeAt(1) - 65));
}

/** Resolve any source spelling to the canonical team name (or the input trimmed). */
export function canonicalTeam(name: string): string {
  const trimmed = name.trim();
  if (TEAMS[trimmed]) return trimmed;
  const alias = ALIASES[trimmed.toLowerCase()];
  return alias ?? trimmed;
}

/** Flag emoji for a team name, '🏳️' if unknown. */
export function teamFlag(name: string): string {
  const meta = TEAMS[canonicalTeam(name)];
  if (!meta) return '🏳️';
  return meta.flag ?? isoToFlag(meta.iso2);
}

/** True when the name is one of the known 2026 qualifiers. */
export function isKnownTeam(name: string): boolean {
  return Boolean(TEAMS[canonicalTeam(name)]);
}

export const ALL_TEAMS = Object.keys(TEAMS);
