import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import {
  GetAllLeaguesWithCapacityAndScoringUseCase,
  type GetAllLeaguesWithCapacityAndScoringInput,
  type GetAllLeaguesWithCapacityAndScoringResult,
} from './GetAllLeaguesWithCapacityAndScoringUseCase';

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

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

  it('should return enriched leagues with capacity and scoring', async () => {
    const useCase = new GetAllLeaguesWithCapacityAndScoringUseCase(
      mockLeagueRepo as unknown as ILeagueRepository,
      mockMembershipRepo as unknown as ILeagueMembershipRepository,
      mockSeasonRepo as unknown as ISeasonRepository,
      mockScoringConfigRepo as unknown as ILeagueScoringConfigRepository,
      mockGameRepo as unknown as IGameRepository,
      { getPresetById: vi.fn().mockReturnValue({ id: 'preset1', name: 'Default' }) },
      output,
    );

    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);
    expect(result.unwrap()).toBeUndefined();

    expect(output.present).toHaveBeenCalledTimes(1);

    const presented =
      output.present.mock.calls[0]?.[0] as GetAllLeaguesWithCapacityAndScoringResult;

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

    const [summary] = presented?.leagues ?? [];

    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' });
  });
});
