import type { GetAllRacesResult } from '@core/racing/application/use-cases/GetAllRacesUseCase';
import { GetAllRacesPresenter } from './GetAllRacesPresenter';

describe('GetAllRacesPresenter', () => {
  it('should map races and distinct leagues into the DTO', async () => {
    const presenter = new GetAllRacesPresenter();

    const output: GetAllRacesResult = {
      races: [
        {
          id: 'race-1',
          leagueId: 'league-1',
          track: 'Track A',
          car: 'Car A',
          status: 'scheduled',
          scheduledAt: new Date('2025-01-01T10:00:00.000Z'),
          strengthOfField: 1500,
        },
        {
          id: 'race-2',
          leagueId: 'league-1',
          track: 'Track B',
          car: 'Car B',
          status: 'completed',
          scheduledAt: new Date('2025-01-02T10:00:00.000Z'),
          strengthOfField: undefined,
        },
        {
          id: 'race-3',
          leagueId: 'league-2',
          track: 'Track C',
          car: 'Car C',
          status: 'running',
          scheduledAt: new Date('2025-01-03T10:00:00.000Z'),
          strengthOfField: 1800,
        },
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      ] as never,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      leagues: [
        { id: 'league-1', name: 'League One' },
        { id: 'league-2', name: 'League Two' },
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      ] as never,
      totalCount: 3,
    };

    await presenter.present(output);
    const viewModel = presenter.getResponseModel();

    expect(viewModel).not.toBeNull();
    expect(viewModel!.races).toHaveLength(3);

    // Leagues should be distinct and match league ids/names from races
    expect(viewModel!.filters.leagues).toEqual(
      expect.arrayContaining([
        { id: 'league-1', name: 'League One' },
        { id: 'league-2', name: 'League Two' },
      ]),
    );
    expect(viewModel!.filters.leagues).toHaveLength(2);
  });

  it('should handle empty races by returning empty leagues', async () => {
    const presenter = new GetAllRacesPresenter();

    const output: GetAllRacesResult = {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      races: [] as never,
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      leagues: [] as never,
      totalCount: 0,
    };

    await presenter.present(output);
    const viewModel = presenter.getResponseModel();

    expect(viewModel).not.toBeNull();
    expect(viewModel!.races).toHaveLength(0);
    expect(viewModel!.filters.leagues).toHaveLength(0);
  });
})
