import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetTotalLeaguesUseCase } from './GetTotalLeaguesUseCase';
import { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { Logger } from '@core/shared/application';

describe('GetTotalLeaguesUseCase', () => {
  let useCase: GetTotalLeaguesUseCase;
  let leagueRepository: {
    findAll: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    leagueRepository = {
      findAll: vi.fn(),
    };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new GetTotalLeaguesUseCase(
      leagueRepository as unknown as ILeagueRepository,
      logger as unknown as Logger,
    );
  });

  it('should return total number of leagues', async () => {
    const leagues = [
      { id: 'league-1', name: 'League 1' },
      { id: 'league-2', name: 'League 2' },
      { id: 'league-3', name: 'League 3' },
    ];

    leagueRepository.findAll.mockResolvedValue(leagues);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      totalLeagues: 3,
    });
  });

  it('should return error on repository failure', async () => {
    const error = new Error('Repository error');

    leagueRepository.findAll.mockRejectedValue(error);

    const result = await useCase.execute();

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'REPOSITORY_ERROR',
      details: { message: 'Failed to retrieve total leagues' },
    });
  });
});