// data.jsx — Mundial 2026: grupos, equipos y calendario completo de fase de grupos.
// Fuente de calendario usada para esta versión: FIFA/medios actualizados al 13-06-2026.
// Exportado a window.

const TEAMS = {
  // Grupo A
  MEX: { code: "MEX", name: "México", flag: "🇲🇽" },
  RSA: { code: "RSA", name: "Sudáfrica", flag: "🇿🇦" },
  KOR: { code: "KOR", name: "Corea del Sur", flag: "🇰🇷" },
  CZE: { code: "CZE", name: "Chequia", flag: "🇨🇿" },
  // Grupo B
  CAN: { code: "CAN", name: "Canadá", flag: "🇨🇦" },
  BIH: { code: "BIH", name: "Bosnia y Herzegovina", flag: "🇧🇦" },
  QAT: { code: "QAT", name: "Catar", flag: "🇶🇦" },
  SUI: { code: "SUI", name: "Suiza", flag: "🇨🇭" },
  // Grupo C
  BRA: { code: "BRA", name: "Brasil", flag: "🇧🇷" },
  MAR: { code: "MAR", name: "Marruecos", flag: "🇲🇦" },
  HAI: { code: "HAI", name: "Haití", flag: "🇭🇹" },
  SCO: { code: "SCO", name: "Escocia", flag: "🏴󠁧󠁢󠁳󠁣󠁴󠁿" },
  // Grupo D
  USA: { code: "USA", name: "Estados Unidos", flag: "🇺🇸" },
  PAR: { code: "PAR", name: "Paraguay", flag: "🇵🇾" },
  AUS: { code: "AUS", name: "Australia", flag: "🇦🇺" },
  TUR: { code: "TUR", name: "Turquía", flag: "🇹🇷" },
  // Grupo E
  GER: { code: "GER", name: "Alemania", flag: "🇩🇪" },
  CUW: { code: "CUW", name: "Curazao", flag: "🇨🇼" },
  CIV: { code: "CIV", name: "Costa de Marfil", flag: "🇨🇮" },
  ECU: { code: "ECU", name: "Ecuador", flag: "🇪🇨" },
  // Grupo F
  NED: { code: "NED", name: "Países Bajos", flag: "🇳🇱" },
  JPN: { code: "JPN", name: "Japón", flag: "🇯🇵" },
  SWE: { code: "SWE", name: "Suecia", flag: "🇸🇪" },
  TUN: { code: "TUN", name: "Túnez", flag: "🇹🇳" },
  // Grupo G
  BEL: { code: "BEL", name: "Bélgica", flag: "🇧🇪" },
  EGY: { code: "EGY", name: "Egipto", flag: "🇪🇬" },
  IRN: { code: "IRN", name: "Irán", flag: "🇮🇷" },
  NZL: { code: "NZL", name: "Nueva Zelanda", flag: "🇳🇿" },
  // Grupo H
  ESP: { code: "ESP", name: "España", flag: "🇪🇸" },
  CPV: { code: "CPV", name: "Cabo Verde", flag: "🇨🇻" },
  KSA: { code: "KSA", name: "Arabia Saudí", flag: "🇸🇦" },
  URU: { code: "URU", name: "Uruguay", flag: "🇺🇾" },
  // Grupo I
  FRA: { code: "FRA", name: "Francia", flag: "🇫🇷" },
  SEN: { code: "SEN", name: "Senegal", flag: "🇸🇳" },
  IRQ: { code: "IRQ", name: "Irak", flag: "🇮🇶" },
  NOR: { code: "NOR", name: "Noruega", flag: "🇳🇴" },
  // Grupo J
  ARG: { code: "ARG", name: "Argentina", flag: "🇦🇷" },
  ALG: { code: "ALG", name: "Argelia", flag: "🇩🇿" },
  AUT: { code: "AUT", name: "Austria", flag: "🇦🇹" },
  JOR: { code: "JOR", name: "Jordania", flag: "🇯🇴" },
  // Grupo K
  POR: { code: "POR", name: "Portugal", flag: "🇵🇹" },
  DRC: { code: "DRC", name: "RD Congo", flag: "🇨🇩" },
  UZB: { code: "UZB", name: "Uzbekistán", flag: "🇺🇿" },
  COL: { code: "COL", name: "Colombia", flag: "🇨🇴" },
  // Grupo L
  ENG: { code: "ENG", name: "Inglaterra", flag: "🏴󠁧󠁢󠁥󠁮󠁧󠁿" },
  CRO: { code: "CRO", name: "Croacia", flag: "🇭🇷" },
  GHA: { code: "GHA", name: "Ghana", flag: "🇬🇭" },
  PAN: { code: "PAN", name: "Panamá", flag: "🇵🇦" },
};

const GROUP_TEAMS = {
  A: ["MEX", "RSA", "KOR", "CZE"],
  B: ["CAN", "BIH", "QAT", "SUI"],
  C: ["BRA", "MAR", "HAI", "SCO"],
  D: ["USA", "PAR", "AUS", "TUR"],
  E: ["GER", "CUW", "CIV", "ECU"],
  F: ["NED", "JPN", "SWE", "TUN"],
  G: ["BEL", "EGY", "IRN", "NZL"],
  H: ["ESP", "CPV", "KSA", "URU"],
  I: ["FRA", "SEN", "IRQ", "NOR"],
  J: ["ARG", "ALG", "AUT", "JOR"],
  K: ["POR", "DRC", "UZB", "COL"],
  L: ["ENG", "CRO", "GHA", "PAN"],
};
const GROUPS = GROUP_TEAMS;

const VENEZUELA_TZ = "America/Caracas";

function dateYMDInVenezuela(date = new Date()) {
  return new Intl.DateTimeFormat("en-CA", {
    timeZone: VENEZUELA_TZ,
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  }).format(date);
}

function toVenezuelaKickoff(day, kickoff) {
  const m = String(kickoff).trim().match(/^(\d{1,2})(?::(\d{2}))?\s*(am|pm)\s*([A-Z]+)$/i);
  if (!m) return { day, time: `${kickoff} · VET` };

  let hour = Number(m[1]);
  const minute = Number(m[2] || 0);
  const meridian = m[3].toLowerCase();
  const zone = m[4].toUpperCase();
  if (meridian === "pm" && hour !== 12) hour += 12;
  if (meridian === "am" && hour === 12) hour = 0;

  // Offsets reales usados para junio 2026 según la zona local indicada en el calendario.
  // Venezuela / America-Caracas = UTC-04:00.
  const offsets = { ET: -4, EDT: -4, PT: -7, PDT: -7, CDT: -5, CT: -5, CST: -6, MT: -6, MDT: -6 };
  const sourceOffset = offsets[zone] ?? -4;
  const venezuelaOffset = -4;
  const [y, mo, d] = day.split("-").map(Number);
  const utcMs = Date.UTC(y, mo - 1, d, hour - sourceOffset, minute);
  const vz = new Date(utcMs + venezuelaOffset * 60 * 60 * 1000);

  const yyyy = vz.getUTCFullYear();
  const mm = String(vz.getUTCMonth() + 1).padStart(2, "0");
  const dd = String(vz.getUTCDate()).padStart(2, "0");
  let h12 = vz.getUTCHours();
  const min = String(vz.getUTCMinutes()).padStart(2, "0");
  const suffix = h12 >= 12 ? "p. m." : "a. m.";
  h12 = h12 % 12 || 12;
  return { day: `${yyyy}-${mm}-${dd}`, time: `${h12}:${min} ${suffix} VET`, local: kickoff };
}

const TODAY = dateYMDInVenezuela();
const ARCHIVED_MATCH_IDS = new Set(["A1a", "A1b", "B1a", "D1a"]);

// id = grupo + jornada + slot. num = número cronológico de partido de fase de grupos.
const RAW_MATCHES = [
  ["A1a", "A", 1, "MEX", "RSA", "2026-06-11", "1pm CST", "Mexico City Stadium"],
  ["A1b", "A", 1, "KOR", "CZE", "2026-06-11", "8pm CST", "Estadio Guadalajara"],
  ["B1a", "B", 1, "CAN", "BIH", "2026-06-12", "3pm ET", "Toronto Stadium"],
  ["D1a", "D", 1, "USA", "PAR", "2026-06-12", "6pm PT", "Los Angeles Stadium"],
  ["B1b", "B", 1, "QAT", "SUI", "2026-06-13", "12pm PT", "San Francisco Bay Area Stadium"],
  ["C1a", "C", 1, "BRA", "MAR", "2026-06-13", "6pm ET", "New York New Jersey Stadium"],
  ["C1b", "C", 1, "HAI", "SCO", "2026-06-13", "9pm ET", "Boston Stadium"],
  ["D1b", "D", 1, "AUS", "TUR", "2026-06-13", "6pm PT", "BC Place"],

  ["E1a", "E", 1, "GER", "CUW", "2026-06-14", "12pm CDT", "Houston Stadium"],
  ["F1a", "F", 1, "NED", "JPN", "2026-06-14", "3pm CDT", "Dallas Stadium"],
  ["E1b", "E", 1, "CIV", "ECU", "2026-06-14", "7pm ET", "Philadelphia Stadium"],
  ["F1b", "F", 1, "SWE", "TUN", "2026-06-14", "8pm CST", "Estadio Monterrey"],

  ["H1a", "H", 1, "ESP", "CPV", "2026-06-15", "12pm ET", "Atlanta Stadium"],
  ["G1a", "G", 1, "BEL", "EGY", "2026-06-15", "12pm PT", "BC Place"],
  ["H1b", "H", 1, "KSA", "URU", "2026-06-15", "6pm ET", "Miami Stadium"],
  ["G1b", "G", 1, "IRN", "NZL", "2026-06-15", "6pm PT", "Los Angeles Stadium"],

  ["I1a", "I", 1, "FRA", "SEN", "2026-06-16", "3pm ET", "New York New Jersey Stadium"],
  ["I1b", "I", 1, "IRQ", "NOR", "2026-06-16", "6pm ET", "Boston Stadium"],
  ["J1a", "J", 1, "ARG", "ALG", "2026-06-16", "8pm CDT", "Kansas City Stadium"],
  ["J1b", "J", 1, "AUT", "JOR", "2026-06-16", "9pm PT", "San Francisco Bay Area Stadium"],

  ["K1a", "K", 1, "POR", "DRC", "2026-06-17", "12pm CDT", "Houston Stadium"],
  ["L1a", "L", 1, "ENG", "CRO", "2026-06-17", "3pm CDT", "Dallas Stadium"],
  ["L1b", "L", 1, "GHA", "PAN", "2026-06-17", "7pm ET", "Toronto Stadium"],
  ["K1b", "K", 1, "UZB", "COL", "2026-06-17", "8pm CST", "Mexico City Stadium"],

  ["A2a", "A", 2, "CZE", "RSA", "2026-06-18", "12pm ET", "Atlanta Stadium"],
  ["B2a", "B", 2, "SUI", "BIH", "2026-06-18", "12pm PT", "Los Angeles Stadium"],
  ["B2b", "B", 2, "CAN", "QAT", "2026-06-18", "3pm PT", "BC Place"],
  ["A2b", "A", 2, "MEX", "KOR", "2026-06-18", "7pm CST", "Estadio Guadalajara"],

  ["C2b", "C", 2, "SCO", "MAR", "2026-06-19", "6pm ET", "Boston Stadium"],
  ["D2a", "D", 2, "USA", "AUS", "2026-06-19", "12pm PT", "Seattle Stadium"],
  ["C2a", "C", 2, "BRA", "HAI", "2026-06-19", "8:30pm ET", "Philadelphia Stadium"],
  ["D2b", "D", 2, "TUR", "PAR", "2026-06-19", "9pm PT", "San Francisco Bay Area Stadium"],

  ["F2a", "F", 2, "NED", "SWE", "2026-06-20", "12pm CDT", "Houston Stadium"],
  ["E2a", "E", 2, "GER", "CIV", "2026-06-20", "4pm ET", "Toronto Stadium"],
  ["E2b", "E", 2, "ECU", "CUW", "2026-06-20", "7pm CDT", "Kansas City Stadium"],
  ["F2b", "F", 2, "TUN", "JPN", "2026-06-20", "10pm CST", "Estadio Monterrey"],

  ["H2a", "H", 2, "ESP", "KSA", "2026-06-21", "12pm ET", "Atlanta Stadium"],
  ["G2a", "G", 2, "BEL", "IRN", "2026-06-21", "12pm PT", "Los Angeles Stadium"],
  ["H2b", "H", 2, "URU", "CPV", "2026-06-21", "6pm ET", "Miami Stadium"],
  ["G2b", "G", 2, "NZL", "EGY", "2026-06-21", "6pm PT", "BC Place"],

  ["J2a", "J", 2, "ARG", "AUT", "2026-06-22", "12pm CDT", "Dallas Stadium"],
  ["I2a", "I", 2, "FRA", "IRQ", "2026-06-22", "5pm ET", "Philadelphia Stadium"],
  ["I2b", "I", 2, "NOR", "SEN", "2026-06-22", "8pm ET", "New York New Jersey Stadium"],
  ["J2b", "J", 2, "JOR", "ALG", "2026-06-22", "8pm PT", "San Francisco Bay Area Stadium"],

  ["K2a", "K", 2, "POR", "UZB", "2026-06-23", "12pm CDT", "Houston Stadium"],
  ["L2a", "L", 2, "ENG", "GHA", "2026-06-23", "4pm ET", "Boston Stadium"],
  ["L2b", "L", 2, "PAN", "CRO", "2026-06-23", "7pm ET", "Toronto Stadium"],
  ["K2b", "K", 2, "COL", "DRC", "2026-06-23", "8pm CST", "Estadio Guadalajara"],

  ["B3a", "B", 3, "SUI", "CAN", "2026-06-24", "12pm PT", "BC Place"],
  ["B3b", "B", 3, "BIH", "QAT", "2026-06-24", "12pm PT", "Seattle Stadium"],
  ["C3a", "C", 3, "SCO", "BRA", "2026-06-24", "6pm ET", "Miami Stadium"],
  ["C3b", "C", 3, "MAR", "HAI", "2026-06-24", "6pm ET", "Atlanta Stadium"],
  ["A3a", "A", 3, "CZE", "MEX", "2026-06-24", "7pm CST", "Mexico City Stadium"],
  ["A3b", "A", 3, "RSA", "KOR", "2026-06-24", "7pm CST", "Estadio Monterrey"],

  ["E3a", "E", 3, "ECU", "GER", "2026-06-25", "4pm ET", "New York New Jersey Stadium"],
  ["E3b", "E", 3, "CUW", "CIV", "2026-06-25", "4pm ET", "Philadelphia Stadium"],
  ["F3a", "F", 3, "JPN", "SWE", "2026-06-25", "6pm CDT", "Dallas Stadium"],
  ["F3b", "F", 3, "TUN", "NED", "2026-06-25", "6pm CDT", "Kansas City Stadium"],
  ["D3a", "D", 3, "TUR", "USA", "2026-06-25", "7pm PT", "Los Angeles Stadium"],
  ["D3b", "D", 3, "PAR", "AUS", "2026-06-25", "7pm PT", "San Francisco Bay Area Stadium"],

  ["I3a", "I", 3, "NOR", "FRA", "2026-06-26", "3pm ET", "Boston Stadium"],
  ["I3b", "I", 3, "SEN", "IRQ", "2026-06-26", "3pm ET", "Toronto Stadium"],
  ["H3a", "H", 3, "CPV", "KSA", "2026-06-26", "7pm CDT", "Houston Stadium"],
  ["H3b", "H", 3, "URU", "ESP", "2026-06-26", "6pm CST", "Estadio Guadalajara"],
  ["G3a", "G", 3, "EGY", "IRN", "2026-06-26", "8pm PT", "Seattle Stadium"],
  ["G3b", "G", 3, "NZL", "BEL", "2026-06-26", "8pm PT", "BC Place"],

  ["L3a", "L", 3, "PAN", "ENG", "2026-06-27", "5pm ET", "New York New Jersey Stadium"],
  ["L3b", "L", 3, "CRO", "GHA", "2026-06-27", "5pm ET", "Philadelphia Stadium"],
  ["K3a", "K", 3, "COL", "POR", "2026-06-27", "7:30pm ET", "Miami Stadium"],
  ["K3b", "K", 3, "DRC", "UZB", "2026-06-27", "7:30pm ET", "Atlanta Stadium"],
  ["J3a", "J", 3, "ALG", "AUT", "2026-06-27", "9pm CDT", "Kansas City Stadium"],
  ["J3b", "J", 3, "JOR", "ARG", "2026-06-27", "9pm CDT", "Dallas Stadium"],
];

const MATCHES = RAW_MATCHES.map(([id, group, md, home, away, localDay, localKickoff, venue], i) => {
  const vet = toVenezuelaKickoff(localDay, localKickoff);
  return {
    id, group, md, home, away,
    day: vet.day,
    kickoff: vet.time,
    localDay,
    localKickoff: vet.local || localKickoff,
    venue,
    num: i + 1,
    archived: ARCHIVED_MATCH_IDS.has(id),
  };
});

// Resultados reales archivados al momento de construir esta versión. El acumulado histórico de jugadores sigue en PLAYERS.base.
const SEED_RESULTS = {
  A1a: { h: 2, a: 0, final: true }, // México 2-0 Sudáfrica
  A1b: { h: 2, a: 1, final: true }, // Corea del Sur 2-1 Chequia
  B1a: { h: 1, a: 1, final: true }, // Canadá 1-1 Bosnia y Herzegovina
  D1a: { h: 4, a: 1, final: true }, // Estados Unidos 4-1 Paraguay
};

// Pronósticos del 13/6 tomados del chat. Partidos: B1b QAT-SUI, C1a BRA-MAR, C1b HAI-SCO, D1b AUS-TUR
const _P13 = {
  jcpadre: [[0, 2], [1, 1], [0, 0], [1, 0]],
  luchito: [[2, 1], [2, 0], [0, 2], [0, 3]],
  yesenia: [[1, 1], [3, 2], [1, 2], [0, 1]],
  jcnene: [[0, 2], [2, 1], [0, 0], [1, 1]],
  nayi: [[0, 1], [3, 1], [0, 1], [1, 2]],
  carlos: [[0, 3], [2, 1], [0, 2], [1, 3]],
  martha: [[1, 2], [1, 1], [0, 2], [0, 1]],
  sofia: [[0, 2], [2, 1], [0, 2], [0, 2]],
  daniel: [[1, 1], [2, 0], [1, 1], [2, 1]],
  stefani: [[0, 1], [2, 1], [0, 1], [1, 1]],
  yani: [[1, 0], [1, 2], [0, 0], [0, 2]],
  angie: [[0, 2], [3, 1], [1, 1], [2, 0]],
};
const _DAY13 = ["B1b", "C1a", "C1b", "D1b"];
const SEED_PREDS = {};
_DAY13.forEach((mid, i) => {
  SEED_PREDS[mid] = {};
  Object.keys(_P13).forEach((pid) => {
    SEED_PREDS[mid][pid] = { h: _P13[pid][i][0], a: _P13[pid][i][1] };
  });
});

// Roster — base = puntos antes de empezar a calcular dentro de esta app.
const PLAYERS = [
  { id: "sofia", name: "Sofía", full: "Sofía Guillén", base: 11 },
  { id: "stefani", name: "Stefany", full: "Stefany Quintero", base: 9 },
  { id: "nayi", name: "Nayi", full: "Nayeli Molero", base: 9 },
  { id: "daniel", name: "Daniel", full: "Daniel Sulbarán", base: 6 },
  { id: "jcpadre", name: "JC Padre", full: "Juan Carlos Flores", base: 6 },
  { id: "martha", name: "Martha", full: "Martha Flores", base: 6 },
  { id: "carlos", name: "Carlos", full: "Carlos Salas", base: 6 },
  { id: "yani", name: "Yani", full: "Yani", base: 3 },
  { id: "yesenia", name: "Yesenia", full: "Yesenia Altuve", base: 3 },
  { id: "angie", name: "Angie", full: "Angie", base: 3 },
  { id: "jcnene", name: "JC el Nene", full: "Juan Carlos (hijo)", base: 3 },
  { id: "luchito", name: "Luchito", full: "Luis Flores", base: 0 },
];

const _WD = ["Dom", "Lun", "Mar", "Mié", "Jue", "Vie", "Sáb"];
const _MO = ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"];
function dayLabel(d) {
  const [y, m, dd] = d.split("-").map(Number);
  const wd = new Date(Date.UTC(y, m - 1, dd)).getUTCDay();
  return `${_WD[wd]} ${dd} ${_MO[m - 1]}`;
}

Object.assign(window, {
  TEAMS, GROUP_TEAMS, GROUPS, MATCHES, SEED_RESULTS, SEED_PREDS, PLAYERS, dayLabel, TODAY, VENEZUELA_TZ,
});
