import { afterEach, describe, expect, it, vi } from 'vitest';

import { determineAction } from '../forecast-scheduler';

describe('forecast scheduler policy', () => {
  afterEach(() => {
    vi.useRealTimers();
  });

  it('prebuilds missing EU forecasts across the lookahead window', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-03-31T16:30:00.000Z'));

    const decision = determineAction(
      'champions_league',
      '2026-04-14T19:00:00.000Z',
      false,
      new Date('2026-03-31T12:30:00.000-04:00'),
    );

    expect(decision).toEqual({ action: 'generate', runType: 'eu_lookahead' });
  });

  it('retries missing EU forecasts again inside the nearer generate window', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-04-10T16:30:00.000Z'));

    const decision = determineAction(
      'epl',
      '2026-04-12T19:00:00.000Z',
      false,
      new Date('2026-04-10T12:30:00.000-04:00'),
    );

    expect(decision).toEqual({ action: 'generate', runType: 'eu_t96h' });
  });

  it('refreshes EU forecasts in the T-1h window when a forecast already exists', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-04-08T17:05:00.000Z'));

    const decision = determineAction(
      'champions_league',
      '2026-04-08T18:00:00.000Z',
      true,
      new Date('2026-04-08T13:05:00.000-04:00'),
    );

    expect(decision).toEqual({ action: 'refresh', runType: 't_minus_1h' });
  });
});
