import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { LeagueStatsViewModel } from '../presenters/ILeagueStatsPresenter';
import type { DriverRatingProvider } from '../ports/DriverRatingProvider';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export interface GetLeagueStatsUseCaseParams {
  leagueId: string;
}

export class GetLeagueStatsUseCase {
  constructor(
    private readonly leagueMembershipRepository: ILeagueMembershipRepository,
    private readonly raceRepository: IRaceRepository,
    private readonly driverRatingProvider: DriverRatingProvider,
  ) {}

  async execute(params: GetLeagueStatsUseCaseParams): Promise<Result<LeagueStatsViewModel, ApplicationErrorCode<'REPOSITORY_ERROR'>>> {
    try {
      const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
      const races = await this.raceRepository.findByLeagueId(params.leagueId);
      const driverIds = memberships.map(m => m.driverId);
      const ratings = this.driverRatingProvider.getRatings(driverIds);
      const validRatings = Array.from(ratings.values()).filter(r => r !== null) as number[];
      const averageRating = validRatings.length > 0 ? Math.round(validRatings.reduce((sum, r) => sum + r, 0) / validRatings.length) : 0;
      const viewModel: LeagueStatsViewModel = {
        totalMembers: memberships.length,
        totalRaces: races.length,
        averageRating,
      };
      return Result.ok(viewModel);
    } catch {
      return Result.err({ code: 'REPOSITORY_ERROR', message: 'Failed to fetch league stats' });
    }
  }
}