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

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

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

  it('should return total number of races', async () => {
    const races = [
      { id: 'race-1', name: 'Race 1' },
      { id: 'race-2', name: 'Race 2' },
    ];

    raceRepository.findAll.mockResolvedValue(races);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      totalRaces: 2,
    });
  });

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

    raceRepository.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 races' },
    });
  });
});