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

describe('GetSeasonDetailsUseCase', () => {
  let useCase: GetSeasonDetailsUseCase;
  let leagueRepository: {
    findById: Mock;
  };
  let seasonRepository: {
    findById: Mock;
  };

  beforeEach(() => {
    leagueRepository = {
      findById: vi.fn(),
    };
    seasonRepository = {
      findById: vi.fn(),
    };
    useCase = new GetSeasonDetailsUseCase(
      leagueRepository as unknown as ILeagueRepository,
      seasonRepository as unknown as ISeasonRepository,
    );
  });

  it('returns full details for a season belonging to the league', async () => {
    const league = { id: 'league-1' };
    const season = Season.create({
      id: 'season-1',
      leagueId: 'league-1',
      gameId: 'iracing',
      name: 'Detailed Season',
      status: 'planned',
    }).withMaxDrivers(24);

    leagueRepository.findById.mockResolvedValue(league);
    seasonRepository.findById.mockResolvedValue(season);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
    });

    expect(result.isOk()).toBe(true);
    const dto = result.unwrap();
    expect(dto.seasonId).toBe('season-1');
    expect(dto.leagueId).toBe('league-1');
    expect(dto.gameId).toBe('iracing');
    expect(dto.name).toBe('Detailed Season');
    expect(dto.status).toBe('planned');
    expect(dto.maxDrivers).toBe(24);
  });

  it('returns error when league not found', async () => {
    leagueRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'LEAGUE_NOT_FOUND',
      details: { message: 'League not found: league-1' },
    });
  });

  it('returns error when season not found', async () => {
    const league = { id: 'league-1' };
    leagueRepository.findById.mockResolvedValue(league);
    seasonRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'SEASON_NOT_FOUND',
      details: { message: 'Season season-1 does not belong to league league-1' },
    });
  });

  it('returns error when season belongs to different league', async () => {
    const league = { id: 'league-1' };
    const season = Season.create({
      id: 'season-1',
      leagueId: 'league-2',
      gameId: 'iracing',
      name: 'Season',
      status: 'active',
    });

    leagueRepository.findById.mockResolvedValue(league);
    seasonRepository.findById.mockResolvedValue(season);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'SEASON_NOT_FOUND',
      details: { message: 'Season season-1 does not belong to league league-1' },
    });
  });
});