import { describe, expect, it } from 'vitest';
import { refitTrainingSet, type DcRow } from './refit';
import type { Fixture } from '../../src/lib/types';

const slot = (team: string | null) => ({ team, placeholder: null, label: team ?? '?' });
const fx = (over: Partial<Fixture>): Fixture => ({
  num: 1, stage: 'group', group: 'A', round: 'Matchday 1', groupRound: 1,
  kickoff: '2026-06-14T18:00:00Z', venue: 'Boston (Foxborough)',
  home: slot('Spain'), away: slot('Japan'),
  status: 'finished', homeScore: 2, awayScore: 0, minute: null,
  ...over,
} as Fixture);

const base = {
  asOf: '2026-06-10',
  rows: [
    ['2026-06-01', 'Spain', 'France', 1, 1, 1],
    ['2010-01-01', 'Spain', 'France', 3, 0, 0], // outside a 15y window from 2026
  ] as DcRow[],
};

describe('refitTrainingSet', () => {
  it('appends finished fixtures and advances asOf to the newest match', () => {
    const { train, asOf } = refitTrainingSet(base, [fx({})], 70);
    expect(asOf).toBe('2026-06-14');
    const wc = train.find((m) => m.daysAgo === 0);
    expect(wc).toMatchObject({ home: 'Spain', away: 'Japan', homeGoals: 2, awayGoals: 0, neutral: true });
  });

  it('recomputes daysAgo relative to the new asOf', () => {
    const { train } = refitTrainingSet(base, [fx({})], 70);
    const old = train.find((m) => m.home === 'Spain' && m.away === 'France');
    expect(old?.daysAgo).toBe(13); // 2026-06-01 → 2026-06-14
  });

  it('drops rows that fall outside the window', () => {
    const { train } = refitTrainingSet(base, [fx({})], 70);
    expect(train.some((m) => m.homeGoals === 3)).toBe(false);
  });

  it('treats a host playing at home as non-neutral', () => {
    const { train } = refitTrainingSet(base, [fx({ home: slot('USA'), venue: 'Los Angeles (Inglewood)' })], 70);
    const wc = train.find((m) => m.daysAgo === 0);
    expect(wc).toMatchObject({ home: 'USA', neutral: false });
  });

  it('flips orientation when the away side is the host at home', () => {
    const { train } = refitTrainingSet(base, [fx({ away: slot('Mexico'), venue: 'Mexico City' })], 70);
    const wc = train.find((m) => m.daysAgo === 0);
    expect(wc).toMatchObject({ home: 'Mexico', away: 'Spain', homeGoals: 0, awayGoals: 2, neutral: false });
  });

  it('skips fixtures without teams or scores', () => {
    const { train } = refitTrainingSet(base, [fx({ homeScore: null })], 70);
    expect(train.every((m) => m.daysAgo > 0)).toBe(true);
  });
});
