/**
 * Application Use Case: GetPrizesUseCase
 *
 * Retrieves prizes for a league or season.
 */

import type { UseCase } from '@core/shared/application/UseCase';
import { Result } from '@core/shared/domain/Result';
import type { Prize } from '../../domain/entities/Prize';
import type { PrizeRepository } from '../../domain/repositories/PrizeRepository';

export interface GetPrizesInput {
  leagueId: string;
  seasonId?: string;
}

export interface GetPrizesResult {
  prizes: Prize[];
}

export class GetPrizesUseCase
  implements UseCase<GetPrizesInput, GetPrizesResult, never>
{
  constructor(
    private readonly prizeRepository: PrizeRepository,
  ) {}

  async execute(input: GetPrizesInput): Promise<Result<GetPrizesResult, never>> {
    const { leagueId, seasonId } = input;

    let prizes;
    if (seasonId) {
      prizes = await this.prizeRepository.findByLeagueIdAndSeasonId(leagueId, seasonId);
    } else {
      prizes = await this.prizeRepository.findByLeagueId(leagueId);
    }

    prizes.sort((a, b) => a.position - b.position);

    return Result.ok({ prizes });
  }
}