import { AwardPrizePresenter } from './AwardPrizePresenter';
import { AwardPrizeResultDTO } from '../dtos/AwardPrizeDTO';
import { PrizeType } from '../dtos/PaymentsDto';

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

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

  describe('present', () => {
    it('should store the result', () => {
      const result: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      presenter.present(result);

      expect(presenter.getResponseModel()).toEqual(result);
    });

    it('should overwrite previous result', () => {
      const firstResult: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      const secondResult: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-456',
          leagueId: 'league-456',
          seasonId: 'season-456',
          position: 2,
          name: 'Another Prize',
          amount: 200,
          type: PrizeType.MERCHANDISE,
          description: 'Another Description',
          awarded: false,
          createdAt: new Date('2024-01-02'),
        },
      };

      presenter.present(firstResult);
      presenter.present(secondResult);

      expect(presenter.getResponseModel()).toEqual(secondResult);
    });
  });

  describe('getResponseModel', () => {
    it('should return null when not presented', () => {
      expect(presenter.getResponseModel()).toBeNull();
    });

    it('should return the result after present()', () => {
      const result: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      presenter.present(result);

      expect(presenter.getResponseModel()).toEqual(result);
    });
  });

  describe('reset', () => {
    it('should clear the result', () => {
      const result: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      presenter.present(result);
      presenter.reset();

      expect(presenter.getResponseModel()).toBeNull();
    });

    it('should allow presenting again after reset', () => {
      const firstResult: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      const secondResult: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-456',
          leagueId: 'league-456',
          seasonId: 'season-456',
          position: 2,
          name: 'Another Prize',
          amount: 200,
          type: PrizeType.MERCHANDISE,
          description: 'Another Description',
          awarded: false,
          createdAt: new Date('2024-01-02'),
        },
      };

      presenter.present(firstResult);
      presenter.reset();
      presenter.present(secondResult);

      expect(presenter.getResponseModel()).toEqual(secondResult);
    });
  });

  describe('viewModel', () => {
    it('should return the result', () => {
      const result: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      presenter.present(result);

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

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

    it('should throw error after reset', () => {
      const result: AwardPrizeResultDTO = {
        prize: {
          id: 'prize-123',
          leagueId: 'league-123',
          seasonId: 'season-123',
          position: 1,
          name: 'Test Prize',
          amount: 100,
          type: PrizeType.CASH,
          description: 'Test Description',
          awarded: false,
          createdAt: new Date('2024-01-01'),
        },
      };

      presenter.present(result);
      presenter.reset();

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