import type { ImportRaceResultsApiResult } from '@core/racing/application/use-cases/ImportRaceResultsApiUseCase';
import { ImportRaceResultsApiPresenter } from './ImportRaceResultsApiPresenter';

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

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

  describe('present', () => {
    it('should map successful result to response model', () => {
      const result: ImportRaceResultsApiResult = {
        success: true,
        raceId: 'race-123',
        leagueId: 'league-456',
        driversProcessed: 10,
        resultsRecorded: 10,
        errors: [],
      };

      presenter.present(result);

      expect(presenter.responseModel).toEqual({
        success: true,
        raceId: 'race-123',
        driversProcessed: 10,
        resultsRecorded: 10,
        errors: [],
      });
    });

    it('should handle result with errors', () => {
      const result: ImportRaceResultsApiResult = {
        success: true,
        raceId: 'race-123',
        leagueId: 'league-456',
        driversProcessed: 10,
        resultsRecorded: 8,
        errors: ['Driver not found: 12345', 'Driver not found: 67890'],
      };

      presenter.present(result);

      expect(presenter.responseModel).toEqual({
        success: true,
        raceId: 'race-123',
        driversProcessed: 10,
        resultsRecorded: 8,
        errors: ['Driver not found: 12345', 'Driver not found: 67890'],
      });
    });

    it('should handle zero drivers processed', () => {
      const result: ImportRaceResultsApiResult = {
        success: true,
        raceId: 'race-123',
        leagueId: 'league-456',
        driversProcessed: 0,
        resultsRecorded: 0,
        errors: [],
      };

      presenter.present(result);

      expect(presenter.responseModel).toEqual({
        success: true,
        raceId: 'race-123',
        driversProcessed: 0,
        resultsRecorded: 0,
        errors: [],
      });
    });
  });

  describe('reset', () => {
    it('should clear the model', () => {
      presenter.present({
        success: true,
        raceId: 'race-123',
        leagueId: 'league-456',
        driversProcessed: 10,
        resultsRecorded: 10,
        errors: [],
      });
      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: ImportRaceResultsApiResult = {
        success: true,
        raceId: 'race-123',
        leagueId: 'league-456',
        driversProcessed: 10,
        resultsRecorded: 10,
        errors: [],
      };

      presenter.present(result);

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