import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { GameRepository } from '../../domain/repositories/GameRepository';
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import type { LeagueRepository } from '../../domain/repositories/LeagueRepository';
import type { LeagueScoringConfigRepository } from '../../domain/repositories/LeagueScoringConfigRepository';
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';
import {
    GetAllLeaguesWithCapacityAndScoringUseCase,
    type GetAllLeaguesWithCapacityAndScoringInput,
} from './GetAllLeaguesWithCapacityAndScoringUseCase';

describe('GetAllLeaguesWithCapacityAndScoringUseCase', () => {
  let mockLeagueRepo: { findAll: Mock };
  let mockMembershipRepo: { getLeagueMembers: Mock };
  let mockSeasonRepo: { findByLeagueId: Mock };
  let mockScoringConfigRepo: { findBySeasonId: Mock };
  let mockGameRepo: { findById: Mock };

  beforeEach(() => {
    mockLeagueRepo = { findAll: vi.fn() };
    mockMembershipRepo = { getLeagueMembers: vi.fn() };
    mockSeasonRepo = { findByLeagueId: vi.fn() };
    mockScoringConfigRepo = { findBySeasonId: vi.fn() };
    mockGameRepo = { findById: vi.fn() };
  });

  it('should return enriched leagues with capacity and scoring', async () => {
    const useCase = new GetAllLeaguesWithCapacityAndScoringUseCase(
      mockLeagueRepo as unknown as LeagueRepository,
      mockMembershipRepo as unknown as LeagueMembershipRepository,
      mockSeasonRepo as unknown as SeasonRepository,
      mockScoringConfigRepo as unknown as LeagueScoringConfigRepository,
      mockGameRepo as unknown as GameRepository,
      { getPresetById: vi.fn().mockReturnValue({ id: 'preset1', name: 'Default' }) }
    );

    const league = { id: 'league1', name: 'Test League', settings: { maxDrivers: 30 } };
    const members = [
      { status: { toString: () => 'active' }, role: { toString: () => 'member' } },
      { status: { toString: () => 'active' }, role: { toString: () => 'owner' } },
    ];
    const season = { id: 'season1', status: { isActive: () => true }, gameId: 'game1' };
    const scoringConfig = { scoringPresetId: 'preset1' };
    const game = { id: 'game1', name: 'iRacing' };

    mockLeagueRepo.findAll.mockResolvedValue([league]);
    mockMembershipRepo.getLeagueMembers.mockResolvedValue(members);
    mockSeasonRepo.findByLeagueId.mockResolvedValue([season]);
    mockScoringConfigRepo.findBySeasonId.mockResolvedValue(scoringConfig);
    mockGameRepo.findById.mockResolvedValue(game);

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

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();

    expect(presented.leagues).toHaveLength(1);

    const summary = presented.leagues[0];

    expect(summary.league).toEqual(league);
    expect(summary.currentDrivers).toBe(2);
    expect(summary.maxDrivers).toBe(30);
    expect(summary.season).toEqual(season);
    expect(summary.scoringConfig).toEqual(scoringConfig);
    expect(summary.game).toEqual(game);
    expect(summary.preset).toEqual({ id: 'preset1', name: 'Default' });
  });
});
