import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';

export interface GetTotalRacesInput {}

export type GetTotalRacesErrorCode = 'REPOSITORY_ERROR';

export interface GetTotalRacesResult {
  totalRaces: number;
}

export class GetTotalRacesUseCase {
  constructor(private readonly raceRepository: RaceRepository) {}

  async execute(
    _input: GetTotalRacesInput,
  ): Promise<Result<GetTotalRacesResult, ApplicationErrorCode<GetTotalRacesErrorCode, { message: string }>>> {
    void _input;
    try {
      const races = await this.raceRepository.findAll();
      const totalRaces = races.length;

      return Result.ok({ totalRaces });
    } catch (error: unknown) {
      const message = error instanceof Error ? error.message : 'Failed to get total races';

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message },
      });
    }
  }
}