import type { GetAllRacesPageDataResult } from '@core/racing/application/use-cases/GetAllRacesPageDataUseCase';
import { AllRacesPageDataPresenter } from './AllRacesPageDataPresenter';

describe('AllRacesPageDataPresenter', () => {
  let presenter: AllRacesPageDataPresenter;

  beforeEach(() => {
    presenter = new AllRacesPageDataPresenter();
  });

  describe('present', () => {
    it('should map result to response model', () => {
      const result: GetAllRacesPageDataResult = {
        races: [
          {
            id: 'race-1',
            track: 'Track A',
            car: 'Car A',
            scheduledAt: '2024-01-01T10:00:00Z',
            status: 'scheduled',
            leagueId: 'league-1',
            leagueName: 'League A',
            strengthOfField: 1500,
          },
          {
            id: 'race-2',
            track: 'Track B',
            car: 'Car B',
            scheduledAt: '2024-01-02T10:00:00Z',
            status: 'completed',
            leagueId: 'league-2',
            leagueName: 'League B',
            strengthOfField: null,
          },
        ],
        filters: {
          statuses: [
            { value: 'all', label: 'All Statuses' },
            { value: 'scheduled', label: 'Scheduled' },
          ],
          leagues: [
            { id: 'league-1', name: 'League A' },
            { id: 'league-2', name: 'League B' },
          ],
        },
      };

      presenter.present(result);

      expect(presenter.responseModel).toEqual({
        races: [
          {
            id: 'race-1',
            track: 'Track A',
            car: 'Car A',
            scheduledAt: '2024-01-01T10:00:00Z',
            status: 'scheduled',
            leagueId: 'league-1',
            leagueName: 'League A',
            strengthOfField: 1500,
          },
          {
            id: 'race-2',
            track: 'Track B',
            car: 'Car B',
            scheduledAt: '2024-01-02T10:00:00Z',
            status: 'completed',
            leagueId: 'league-2',
            leagueName: 'League B',
            strengthOfField: null,
          },
        ],
        filters: {
          statuses: [
            { value: 'all', label: 'All Statuses' },
            { value: 'scheduled', label: 'Scheduled' },
          ],
          leagues: [
            { id: 'league-1', name: 'League A' },
            { id: 'league-2', name: 'League B' },
          ],
        },
      });
    });

    it('should handle empty races', () => {
      const result: GetAllRacesPageDataResult = {
        races: [],
        filters: {
          statuses: [{ value: 'all', label: 'All Statuses' }],
          leagues: [],
        },
      };

      presenter.present(result);

      expect(presenter.responseModel.races).toEqual([]);
      expect(presenter.responseModel.filters.leagues).toEqual([]);
    });
  });

  describe('reset', () => {
    it('should clear the model', () => {
      const result: GetAllRacesPageDataResult = {
        races: [{ id: 'race-1', track: 'Track A', car: 'Car A', scheduledAt: '2024-01-01', status: 'scheduled', leagueId: 'league-1', leagueName: 'League A', strengthOfField: null }],
        filters: { statuses: [], leagues: [] },
      };
      presenter.present(result);
      expect(presenter.responseModel).toBeDefined();

      presenter.reset();
      expect(() => presenter.responseModel).toThrow('Presenter not presented');
    });
  });

  describe('responseModel', () => {
    it('should throw error when accessed before present()', () => {
      expect(() => presenter.responseModel).toThrow('Presenter not presented');
    });
  });

  describe('viewModel', () => {
    it('should return same as responseModel', () => {
      const result: GetAllRacesPageDataResult = {
        races: [{ id: 'race-1', track: 'Track A', car: 'Car A', scheduledAt: '2024-01-01', status: 'scheduled', leagueId: 'league-1', leagueName: 'League A', strengthOfField: null }],
        filters: { statuses: [], leagues: [] },
      };

      presenter.present(result);

      expect(presenter.viewModel).toEqual(presenter.responseModel);
    });
  });
});