import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { DriverRepository } from '../../domain/repositories/DriverRepository';
import {
    GetTotalDriversErrorCode,
    GetTotalDriversInput,
    GetTotalDriversUseCase,
} from './GetTotalDriversUseCase';
describe('GetTotalDriversUseCase', () => {
  let useCase: GetTotalDriversUseCase;
  let driverRepository: {
    findAll: Mock;
  };
  beforeEach(() => {
    driverRepository = {
      findAll: vi.fn(),
    };

    useCase = new GetTotalDriversUseCase(driverRepository as unknown as DriverRepository);
  });

  it('should return total number of drivers', async () => {
    const drivers = [{ id: '1' }, { id: '2' }];

    driverRepository.findAll.mockResolvedValue(drivers);

    const input: GetTotalDriversInput = {};

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    });

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

    driverRepository.findAll.mockRejectedValue(error);

    const input: GetTotalDriversInput = {};

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);

    const unwrappedError = result.unwrapErr() as ApplicationErrorCode<
      GetTotalDriversErrorCode,
      { message: string }
    >;

    expect(unwrappedError.code).toBe('REPOSITORY_ERROR');
    expect(unwrappedError.details.message).toBe(error.message);
  });
});