import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Season } from '../../domain/entities/season/Season';
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';

export interface GetSeasonDetailsInput {
  seasonId: string;
}

export type GetSeasonDetailsErrorCode = 'SEASON_NOT_FOUND' | 'REPOSITORY_ERROR';

export interface GetSeasonDetailsResult {
  season: Season;
}

export class GetSeasonDetailsUseCase {
  constructor(private readonly seasonRepository: SeasonRepository) {}

  async execute(
    input: GetSeasonDetailsInput,
  ): Promise<Result<GetSeasonDetailsResult, ApplicationErrorCode<GetSeasonDetailsErrorCode, { message: string }>>> {
    try {
      const season = await this.seasonRepository.findById(input.seasonId);

      if (!season) {
        return Result.err({
          code: 'SEASON_NOT_FOUND',
          details: { message: 'Season not found' },
        });
      }

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

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