/**
 * World-Cup mock dataset (FEATURE 2026-06).
 *
 * Two sources later, ONE shape here:
 *   • Phase 1 (this file)            — hard-coded fixtures so the UI can
 *                                       be perfected without a backend.
 *   • Phase 2 (backend wiring)       — same shape served from
 *                                       PHP /api/sports/* OR API-Football
 *                                       proxy.
 *
 * Keeping the shape tiny + ISO-3166 alpha-2 country codes means we can
 * resolve flags entirely client-side via flagcdn.com without a runtime
 * sprite sheet.
 */

export type MatchStatus = "LIVE" | "FT" | "UPCOMING";

export type Team = {
  /** ISO-3166-1 alpha-2 lower-case (e.g. "ar", "br"). Used for flags. */
  code: string;
  /** Display name shown in the match card. */
  name: string;
};

export type Match = {
  id: string;
  /** ISO date-time, kick-off in UTC. */
  kickoffIso: string;
  status: MatchStatus;
  /** Minute / phase label rendered under the LIVE chip. */
  liveLabel?: string;
  group?: string;
  /** Knock-out round, when applicable. */
  round?: KoRound;
  venue: string;
  home: Team;
  away: Team;
  /** null when the match hasn't started. */
  homeScore: number | null;
  awayScore: number | null;
  /** Penalty shoot-out scores (knockout rounds only). */
  homePenalty?: number;
  awayPenalty?: number;
  /** Free-text broadcaster — what we fuzzy-match against the user's
   *  Xtream channel list for the Click-to-Play upsell. */
  broadcaster?: string;
};

export type KoRound =
  | "Round of 16"
  | "Quarter Finals"
  | "Semi Finals"
  | "Final";

export const TOURNAMENT = {
  name: "FIFA WORLD CUP 2026",
  startIso: "2026-06-11",
  endIso: "2026-07-19",
  teamCount: 64,
  venueCount: 16,
  finalIso: "2026-07-19",
  finalVenue: "MetLife Stadium, USA",
} as const;

/**
 * Mock fixture list — chronologically ordered. "Today" / "Tomorrow"
 * filters resolve against the user's local date so the data stays
 * useful no matter when the QA tester opens the app.
 *
 * To keep "Today" / "Tomorrow" demo-able after the real tournament
 * ends, the kick-off dates are recomputed at runtime relative to the
 * current calendar day in `useWorldCupFixtures()`.
 */
export const MOCK_MATCHES: Match[] = [
  {
    id: "wc-001",
    kickoffIso: "2026-06-12T18:00:00Z",
    status: "LIVE",
    liveLabel: "78'",
    group: "Group B",
    venue: "MetLife Stadium, USA",
    home: { code: "ma", name: "Morocco" },
    away: { code: "es", name: "Spain" },
    homeScore: 2,
    awayScore: 1,
    broadcaster: "beIN Sports 1",
  },
  {
    id: "wc-002",
    kickoffIso: "2026-06-12T20:30:00Z",
    status: "LIVE",
    liveLabel: "45+2'",
    group: "Group G",
    venue: "SoFi Stadium, USA",
    home: { code: "br", name: "Brazil" },
    away: { code: "rs", name: "Serbia" },
    homeScore: 1,
    awayScore: 0,
    broadcaster: "beIN Sports 2",
  },
  {
    id: "wc-003",
    kickoffIso: "2026-06-12T22:00:00Z",
    status: "UPCOMING",
    group: "Group D",
    venue: "AT&T Stadium, USA",
    home: { code: "fr", name: "France" },
    away: { code: "de", name: "Germany" },
    homeScore: null,
    awayScore: null,
    broadcaster: "TF1 HD",
  },
  {
    id: "wc-004",
    kickoffIso: "2026-06-13T01:00:00Z",
    status: "UPCOMING",
    group: "Group E",
    venue: "Mercedes-Benz Stadium, USA",
    home: { code: "ar", name: "Argentina" },
    away: { code: "pt", name: "Portugal" },
    homeScore: null,
    awayScore: null,
    broadcaster: "DSports HD",
  },
  {
    id: "wc-005",
    kickoffIso: "2026-06-13T04:00:00Z",
    status: "UPCOMING",
    group: "Group C",
    venue: "Hard Rock Stadium, USA",
    home: { code: "gb-eng", name: "England" },
    away: { code: "jp", name: "Japan" },
    homeScore: null,
    awayScore: null,
    broadcaster: "BBC One HD",
  },
  {
    id: "wc-006",
    kickoffIso: "2026-06-14T18:00:00Z",
    status: "UPCOMING",
    group: "Group A",
    venue: "Lumen Field, USA",
    home: { code: "us", name: "USA" },
    away: { code: "nl", name: "Netherlands" },
    homeScore: null,
    awayScore: null,
    broadcaster: "FOX Sports 1",
  },
  {
    id: "wc-007",
    kickoffIso: "2026-06-14T21:00:00Z",
    status: "UPCOMING",
    group: "Group F",
    venue: "BMO Field, Canada",
    home: { code: "be", name: "Belgium" },
    away: { code: "uy", name: "Uruguay" },
    homeScore: null,
    awayScore: null,
    broadcaster: "CBC Sports",
  },
];

/**
 * Knock-out bracket — completed Round of 16 + Quarter / Semi /
 * Final. Includes penalty results to demonstrate (3) / (4) markers.
 */
export type BracketMatch = {
  id: string;
  round: KoRound;
  home: Team;
  away: Team;
  homeScore: number | null;
  awayScore: number | null;
  homePenalty?: number;
  awayPenalty?: number;
};

export const KNOCKOUT: BracketMatch[] = [
  // Round of 16 — 8 matches
  { id: "r16-1", round: "Round of 16", home: { code: "nl", name: "Netherlands" }, away: { code: "us", name: "USA" }, homeScore: 2, awayScore: 1 },
  { id: "r16-2", round: "Round of 16", home: { code: "ar", name: "Argentina" }, away: { code: "au", name: "Australia" }, homeScore: 3, awayScore: 0 },
  { id: "r16-3", round: "Round of 16", home: { code: "fr", name: "France" }, away: { code: "pl", name: "Poland" }, homeScore: 2, awayScore: 1 },
  { id: "r16-4", round: "Round of 16", home: { code: "gb-eng", name: "England" }, away: { code: "sn", name: "Senegal" }, homeScore: 3, awayScore: 1 },
  { id: "r16-5", round: "Round of 16", home: { code: "br", name: "Brazil" }, away: { code: "kr", name: "Korea Rep." }, homeScore: 4, awayScore: 1 },
  { id: "r16-6", round: "Round of 16", home: { code: "pt", name: "Portugal" }, away: { code: "ch", name: "Switzerland" }, homeScore: 2, awayScore: 1 },
  { id: "r16-7", round: "Round of 16", home: { code: "es", name: "Spain" }, away: { code: "ma", name: "Morocco" }, homeScore: 2, awayScore: 0 },
  { id: "r16-8", round: "Round of 16", home: { code: "de", name: "Germany" }, away: { code: "jp", name: "Japan" }, homeScore: 3, awayScore: 2 },
  // Quarters
  { id: "qf-1", round: "Quarter Finals", home: { code: "nl", name: "Netherlands" }, away: { code: "ar", name: "Argentina" }, homeScore: 1, awayScore: 2 },
  { id: "qf-2", round: "Quarter Finals", home: { code: "fr", name: "France" }, away: { code: "gb-eng", name: "England" }, homeScore: 1, awayScore: 1, homePenalty: 3, awayPenalty: 4 },
  { id: "qf-3", round: "Quarter Finals", home: { code: "br", name: "Brazil" }, away: { code: "pt", name: "Portugal" }, homeScore: 2, awayScore: 0 },
  { id: "qf-4", round: "Quarter Finals", home: { code: "es", name: "Spain" }, away: { code: "de", name: "Germany" }, homeScore: 1, awayScore: 2 },
  // Semis
  { id: "sf-1", round: "Semi Finals", home: { code: "ar", name: "Argentina" }, away: { code: "gb-eng", name: "England" }, homeScore: 2, awayScore: 1 },
  { id: "sf-2", round: "Semi Finals", home: { code: "br", name: "Brazil" }, away: { code: "de", name: "Germany" }, homeScore: 1, awayScore: 2 },
  // Final
  { id: "fin", round: "Final", home: { code: "ar", name: "Argentina" }, away: { code: "de", name: "Germany" }, homeScore: 2, awayScore: 1 },
];

/** Resolve a country flag PNG via the public flagcdn.com CDN.
 *  Falls back to a tiny coloured pill if the host is offline. */
export function flagUrl(code: string, size: 20 | 40 | 80 = 40): string {
  return `https://flagcdn.com/w${size}/${code.toLowerCase()}.png`;
}
