import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetLeagueSeasonsUseCase } from './GetLeagueSeasonsUseCase';
import { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import { Season } from '../../domain/entities/Season';

describe('GetLeagueSeasonsUseCase', () => {
  let useCase: GetLeagueSeasonsUseCase;
  let seasonRepository: {
    findByLeagueId: Mock;
  };

  beforeEach(() => {
    seasonRepository = {
      findByLeagueId: vi.fn(),
    };
    useCase = new GetLeagueSeasonsUseCase(
      seasonRepository as unknown as ISeasonRepository,
    );
  });

  it('should return seasons mapped to view model', async () => {
    const leagueId = 'league-1';
    const seasons = [
      Season.create({
        id: 'season-1',
        leagueId,
        gameId: 'game-1',
        name: 'Season 1',
        status: 'active',
        startDate: new Date('2023-01-01'),
        endDate: new Date('2023-12-31'),
      }),
      Season.create({
        id: 'season-2',
        leagueId,
        gameId: 'game-1',
        name: 'Season 2',
        status: 'planned',
      }),
    ];

    seasonRepository.findByLeagueId.mockResolvedValue(seasons);

    const result = await useCase.execute({ leagueId });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      seasons: [
        {
          seasonId: 'season-1',
          name: 'Season 1',
          status: 'active',
          startDate: new Date('2023-01-01'),
          endDate: new Date('2023-12-31'),
          isPrimary: false,
          isParallelActive: false, // only one active
        },
        {
          seasonId: 'season-2',
          name: 'Season 2',
          status: 'planned',
          startDate: undefined,
          endDate: undefined,
          isPrimary: false,
          isParallelActive: false,
        },
      ],
    });
  });

  it('should set isParallelActive true for active seasons when multiple active', async () => {
    const leagueId = 'league-1';
    const seasons = [
      Season.create({
        id: 'season-1',
        leagueId,
        gameId: 'game-1',
        name: 'Season 1',
        status: 'active',
      }),
      Season.create({
        id: 'season-2',
        leagueId,
        gameId: 'game-1',
        name: 'Season 2',
        status: 'active',
      }),
    ];

    seasonRepository.findByLeagueId.mockResolvedValue(seasons);

    const result = await useCase.execute({ leagueId });

    expect(result.isOk()).toBe(true);
    const viewModel = result.unwrap();
    expect(viewModel.seasons).toHaveLength(2);
    expect(viewModel.seasons[0]!.isParallelActive).toBe(true);
    expect(viewModel.seasons[1]!.isParallelActive).toBe(true);
  });

  it('should return error when repository fails', async () => {
    const leagueId = 'league-1';
    seasonRepository.findByLeagueId.mockRejectedValue(new Error('DB error'));

    const result = await useCase.execute({ leagueId });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      message: 'Failed to fetch seasons',
    });
  });
});