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

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

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

export interface GetPrizesResult {
  prizes: Prize[];
}

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

  async execute(input: GetPrizesInput): Promise<Result<void, 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);

    this.output.present({ prizes });

    return Result.ok(undefined);
  }
}