import { describe, it, expect } from 'vitest';
import { normalizeScoreboard, normalizeSummary, type RawSummary } from './espnNormalize';

describe('normalizeScoreboard', () => {
  it('normalizes events, canonicalizes names, maps status + scores', () => {
    const out = normalizeScoreboard({
      events: [
        {
          id: '760414',
          date: '2026-06-12T02:00Z',
          name: 'Czechia at South Korea',
          status: { type: { state: 'in' }, displayClock: "67'", period: 2 },
          competitions: [
            {
              competitors: [
                { homeAway: 'home', team: { id: '1', displayName: 'South Korea' }, score: '1' },
                { homeAway: 'away', team: { id: '2', displayName: 'Czechia' }, score: '2' },
              ],
            },
          ],
        },
        {
          id: '760415',
          date: '2026-06-11T19:00Z',
          name: 'South Africa at Mexico',
          status: { type: { state: 'pre' } },
          competitions: [
            {
              competitors: [
                { homeAway: 'home', team: { id: '203', displayName: 'Mexico' } },
                { homeAway: 'away', team: { id: '467', displayName: 'South Africa' } },
              ],
            },
          ],
        },
      ],
    });
    expect(out).toHaveLength(2);
    const live = out[0]!;
    expect(live.eventId).toBe('760414');
    expect(live.home).toBe('South Korea');
    expect(live.away).toBe('Czech Republic'); // Czechia → canonical
    expect(live.status).toBe('live');
    expect(live.homeScore).toBe(1);
    expect(live.awayScore).toBe(2);
    expect(live.minute).toBe(67);

    const sched = out[1]!;
    expect(sched.status).toBe('scheduled');
    expect(sched.homeScore).toBeNull();
    expect(sched.minute).toBeNull();
  });
});

describe('normalizeSummary', () => {
  it('extracts venue, teams, H2H, form, lineups and stats', () => {
    const raw: RawSummary = {
      gameInfo: { venue: { fullName: 'Estadio Banorte' } },
      header: {
        competitions: [
          { competitors: [
            { id: '203', team: { displayName: 'Mexico' }, homeAway: 'home' },
            { id: '467', team: { displayName: 'South Africa' }, homeAway: 'away' },
          ] },
        ],
      },
      headToHeadGames: [
        { events: [
          { gameDate: '2010-06-11T14:00Z', homeTeamId: '203', awayTeamId: '467', homeTeamScore: '1', awayTeamScore: '1' },
        ] },
      ],
      boxscore: {
        form: [
          { team: { id: '203' }, events: [{ gameResult: 'W' }, { gameResult: 'd' }] },
        ],
        teams: [
          { team: { id: '203' }, statistics: [{ name: 'possessionPct', displayValue: '58%' }] },
        ],
      },
      rosters: [
        { team: { id: '203' }, roster: [{ athlete: { displayName: 'A. Player' }, position: { abbreviation: 'F' }, starter: true, jersey: '9' }] },
      ],
    };
    const s = normalizeSummary(raw);
    expect(s.venue).toBe('Estadio Banorte');
    expect(s.teams.map((t) => t.name)).toEqual(['Mexico', 'South Africa']);
    expect(s.h2h[0]).toMatchObject({ homeScore: 1, awayScore: 1, homeTeamId: '203' });
    expect(s.form['203']).toEqual(['W', 'D']); // uppercased
    expect(s.lineups['203']![0]).toMatchObject({ name: 'A. Player', position: 'F', starter: true, number: 9 });
    expect(s.stats['203']!.possessionPct).toBe('58%');
  });
});
