import { describe, expect, it } from 'vitest';
import { upcomingByDay } from './fixtures';
import type { Fixture } from './types';

const fx = (num: number, kickoff: string, status: Fixture['status'] = 'scheduled'): Fixture => ({
  num, stage: 'group', group: 'A', round: 'Matchday 1', groupRound: 1,
  kickoff, venue: 'Somewhere',
  home: { team: 'A', placeholder: null, label: 'A' },
  away: { team: 'B', placeholder: null, label: 'B' },
  status, homeScore: null, awayScore: null, minute: null,
} as Fixture);

// fixed "now": midday UTC so local-day edges don't flip the assertions
const NOW = new Date('2026-06-14T12:00:00Z');

describe('upcomingByDay', () => {
  it('groups scheduled fixtures after today into at most maxDays days', () => {
    const groups = upcomingByDay([
      fx(1, '2026-06-15T18:00:00Z'),
      fx(2, '2026-06-15T21:00:00Z'),
      fx(3, '2026-06-16T18:00:00Z'),
      fx(4, '2026-06-17T18:00:00Z'),
      fx(5, '2026-06-18T18:00:00Z'),
    ], 3, NOW);
    expect(groups).toHaveLength(3);
    expect(groups[0]!.fixtures.map((f) => f.num)).toEqual([1, 2]);
    expect(groups[2]!.fixtures.map((f) => f.num)).toEqual([4]);
  });

  it('excludes today, live and finished fixtures', () => {
    const groups = upcomingByDay([
      fx(1, '2026-06-14T20:00:00Z'),            // today → excluded
      fx(2, '2026-06-15T18:00:00Z', 'live'),     // live → excluded
      fx(3, '2026-06-15T21:00:00Z', 'finished'), // finished → excluded
      fx(4, '2026-06-15T15:00:00Z'),
    ], 3, NOW);
    expect(groups).toHaveLength(1);
    expect(groups[0]!.fixtures.map((f) => f.num)).toEqual([4]);
  });

  it('sorts within a day by kickoff and keeps day order', () => {
    const groups = upcomingByDay([
      fx(2, '2026-06-15T21:00:00Z'),
      fx(1, '2026-06-15T15:00:00Z'),
    ], 2, NOW);
    expect(groups[0]!.fixtures.map((f) => f.num)).toEqual([1, 2]);
    expect(groups[0]!.firstKickoff).toBe('2026-06-15T15:00:00Z');
  });

  it('returns fewer groups when fewer days remain', () => {
    expect(upcomingByDay([fx(1, '2026-06-15T18:00:00Z')], 3, NOW)).toHaveLength(1);
  });

  it('handles the tournament-over case', () => {
    expect(upcomingByDay([fx(1, '2026-06-10T18:00:00Z', 'finished')], 3, NOW)).toEqual([]);
  });
});
