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

describe('GetLeagueAdminUseCase', () => {
  let mockLeagueRepo: ILeagueRepository;
  let mockFindById: Mock;

  beforeEach(() => {
    mockFindById = vi.fn();
    mockLeagueRepo = {
      findById: mockFindById,
      findAll: vi.fn(),
      create: vi.fn(),
      update: vi.fn(),
      delete: vi.fn(),
      exists: vi.fn(),
      findByOwnerId: vi.fn(),
      searchByName: vi.fn(),
    } as ILeagueRepository;
  });

  const createUseCase = () => new GetLeagueAdminUseCase(
    mockLeagueRepo,
  );

  const params = {
    leagueId: 'league1',
  };

  it('should return error when league not found', async () => {
    mockFindById.mockResolvedValue(null);

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('LEAGUE_NOT_FOUND');
    expect(result.unwrapErr().details.message).toBe('League not found');
  });

  it('should return league data when league found', async () => {
    const league = { id: 'league1', ownerId: 'owner1' };
    mockFindById.mockResolvedValue(league);

    const useCase = createUseCase();
    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      league: {
        id: 'league1',
        ownerId: 'owner1',
      },
    });
  });
});