import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
import type { LeagueScoringPresetProvider } from '../ports/LeagueScoringPresetProvider';
import type { LeagueScoringConfigData } from '../presenters/ILeagueScoringConfigPresenter';
import type { AsyncUseCase } from '@core/shared/application/AsyncUseCase';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

type GetLeagueScoringConfigErrorCode =
  | 'LEAGUE_NOT_FOUND'
  | 'NO_SEASONS'
  | 'NO_ACTIVE_SEASON'
  | 'NO_SCORING_CONFIG'
  | 'GAME_NOT_FOUND';

/**
 * Use Case for retrieving a league's scoring configuration for its active season.
 */
export class GetLeagueScoringConfigUseCase
  implements AsyncUseCase<{ leagueId: string }, LeagueScoringConfigData, GetLeagueScoringConfigErrorCode>
{
  constructor(
    private readonly leagueRepository: ILeagueRepository,
    private readonly seasonRepository: ISeasonRepository,
    private readonly leagueScoringConfigRepository: ILeagueScoringConfigRepository,
    private readonly gameRepository: IGameRepository,
    private readonly presetProvider: LeagueScoringPresetProvider,
  ) {}

  async execute(params: { leagueId: string }): Promise<Result<LeagueScoringConfigData, ApplicationErrorCode<GetLeagueScoringConfigErrorCode>>> {
    const { leagueId } = params;

    const league = await this.leagueRepository.findById(leagueId);
    if (!league) {
      return Result.err({ code: 'LEAGUE_NOT_FOUND' });
    }

    const seasons = await this.seasonRepository.findByLeagueId(leagueId);
    if (!seasons || seasons.length === 0) {
      return Result.err({ code: 'NO_SEASONS' });
    }

    const activeSeason =
      seasons.find((s) => s.status === 'active') ?? seasons[0];

    if (!activeSeason) {
      return Result.err({ code: 'NO_ACTIVE_SEASON' });
    }

    const scoringConfig =
      await this.leagueScoringConfigRepository.findBySeasonId(activeSeason.id);
    if (!scoringConfig) {
      return Result.err({ code: 'NO_SCORING_CONFIG' });
    }

    const game = await this.gameRepository.findById(activeSeason.gameId);
    if (!game) {
      return Result.err({ code: 'GAME_NOT_FOUND' });
    }

    const presetId = scoringConfig.scoringPresetId;
    const preset = presetId ? this.presetProvider.getPresetById(presetId) : undefined;

    const data: LeagueScoringConfigData = {
      leagueId: league.id,
      seasonId: activeSeason.id,
      gameId: game.id,
      gameName: game.name,
      ...(presetId !== undefined ? { scoringPresetId: presetId } : {}),
      ...(preset !== undefined ? { preset } : {}),
      championships: scoringConfig.championships,
    };

    return Result.ok(data);
  }
}