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

export interface GetTotalLeaguesInput {}

export type GetTotalLeaguesErrorCode = 'REPOSITORY_ERROR';

export interface GetTotalLeaguesResult {
  totalLeagues: number;
}

export class GetTotalLeaguesUseCase {
  constructor(private readonly leagueRepository: LeagueRepository) {}

  async execute(
    _input: GetTotalLeaguesInput,
  ): Promise<Result<GetTotalLeaguesResult, ApplicationErrorCode<GetTotalLeaguesErrorCode, { message: string }>>> {
    void _input;
    try {
      const leagues = await this.leagueRepository.findAll();
      const totalLeagues = leagues.length;

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

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