import type { LeagueScoringPreset } from '../../domain/types/LeagueScoringPreset';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort } from '@core/shared/application';

export type ListLeagueScoringPresetsInput = {};

export type { LeagueScoringPreset } from '../../domain/types/LeagueScoringPreset';

export interface ListLeagueScoringPresetsResult {
  presets: LeagueScoringPreset[];
}

export type ListLeagueScoringPresetsErrorCode = 'REPOSITORY_ERROR';

/**
 * Use Case for listing league scoring presets.
 * Returns preset data without business logic.
 */
export class ListLeagueScoringPresetsUseCase {
  constructor(
    private readonly presets: LeagueScoringPreset[],
    private readonly output: UseCaseOutputPort<ListLeagueScoringPresetsResult>,
  ) {}

  async execute(
    _input: ListLeagueScoringPresetsInput,
  ): Promise<
    Result<void, ApplicationErrorCode<ListLeagueScoringPresetsErrorCode, { message: string }>>
  > {
    void _input;
    try {
      const presets: LeagueScoringPreset[] = this.presets.map(p => ({
        id: p.id,
        name: p.name,
        description: p.description,
        primaryChampionshipType: p.primaryChampionshipType,
        sessionSummary: p.sessionSummary,
        bonusSummary: p.bonusSummary,
        dropPolicySummary: p.dropPolicySummary,
        defaultTimings: p.defaultTimings,
      }));

      const result: ListLeagueScoringPresetsResult = { presets };

      this.output.present(result);

      return Result.ok(undefined);
    } catch (error) {
      const message =
        error instanceof Error && error.message
          ? error.message
          : 'Failed to list league scoring presets';

      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message },
      } as ApplicationErrorCode<ListLeagueScoringPresetsErrorCode, { message: string }>);
    }
  }
}