import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetAllLeaguesWithCapacityUseCase } from './GetAllLeaguesWithCapacityUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';

describe('GetAllLeaguesWithCapacityUseCase', () => {
  let mockLeagueRepo: { findAll: Mock };
  let mockMembershipRepo: { getLeagueMembers: Mock };

  beforeEach(() => {
    mockLeagueRepo = { findAll: vi.fn() };
    mockMembershipRepo = { getLeagueMembers: vi.fn() };
  });

  it('should return leagues with capacity information', async () => {
    const useCase = new GetAllLeaguesWithCapacityUseCase(
      mockLeagueRepo as unknown as ILeagueRepository,
      mockMembershipRepo as unknown as ILeagueMembershipRepository,
    );

    const league1 = { id: 'league1', name: 'Test League 1' };
    const league2 = { id: 'league2', name: 'Test League 2' };
    const members1 = [
      { status: 'active', role: 'member' },
      { status: 'active', role: 'owner' },
      { status: 'inactive', role: 'member' },
    ];
    const members2 = [
      { status: 'active', role: 'admin' },
    ];

    mockLeagueRepo.findAll.mockResolvedValue([league1, league2]);
    mockMembershipRepo.getLeagueMembers
      .mockResolvedValueOnce(members1)
      .mockResolvedValueOnce(members2);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      leagues: [league1, league2],
      memberCounts: new Map([
        ['league1', 2],
        ['league2', 1],
      ]),
    });
  });

  it('should return empty result when no leagues', async () => {
    const useCase = new GetAllLeaguesWithCapacityUseCase(
      mockLeagueRepo as unknown as ILeagueRepository,
      mockMembershipRepo as unknown as ILeagueMembershipRepository,
    );

    mockLeagueRepo.findAll.mockResolvedValue([]);
    mockMembershipRepo.getLeagueMembers.mockResolvedValue([]);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      leagues: [],
      memberCounts: new Map(),
    });
  });
});