import type {
    LeagueScoringPresetDTO,
    LeagueScoringPresetProvider,
} from '@core/racing/application/ports/LeagueScoringPresetProvider';
import {
    getLeagueScoringPresetById,
    listLeagueScoringPresets,
} from '../../../bootstrap/LeagueScoringPresets';

/**
 * Infrastructure adapter exposing the in-memory scoring preset registry
 * through the LeagueScoringPresetProvider application port.
 */
export class InMemoryLeagueScoringPresetProvider
  implements LeagueScoringPresetProvider
{
  listPresets(): LeagueScoringPresetDTO[] {
    return listLeagueScoringPresets().map((preset) => ({
      id: preset.id,
      name: preset.name,
      description: preset.description,
      primaryChampionshipType: preset.primaryChampionshipType,
      sessionSummary: preset.sessionSummary,
      bonusSummary: preset.bonusSummary,
      dropPolicySummary: preset.dropPolicySummary,
    }));
  }

  getPresetById(id: string): LeagueScoringPresetDTO | undefined {
    const preset = getLeagueScoringPresetById(id);
    if (!preset) {
      return undefined;
    }

    return {
      id: preset.id,
      name: preset.name,
      description: preset.description,
      primaryChampionshipType: preset.primaryChampionshipType,
      sessionSummary: preset.sessionSummary,
      bonusSummary: preset.bonusSummary,
      dropPolicySummary: preset.dropPolicySummary,
    };
  }

  createScoringConfigFromPreset(presetId: string, seasonId: string) {
    const preset = getLeagueScoringPresetById(presetId);
    if (!preset) {
      throw new Error(`Scoring preset with id ${presetId} not found`);
    }
    return preset.createConfig({ seasonId });
  }
}