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

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

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

  it('should return total number of drivers', async () => {
    const drivers = [
      Driver.create({ id: '1', iracingId: '123', name: 'Driver 1', country: 'US' }),
      Driver.create({ id: '2', iracingId: '456', name: 'Driver 2', country: 'UK' }),
    ];

    driverRepository.findAll.mockResolvedValue(drivers);

    const result = await useCase.execute();

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

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

    driverRepository.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 drivers' },
    });
  });
});