import { RulebookViewData } from '@/lib/view-data/leagues/RulebookViewData';
import { RulebookApiDto } from '@/lib/types/tbd/RulebookApiDto';

export class RulebookViewDataBuilder {
  static build(apiDto: RulebookApiDto): RulebookViewData {
    const primaryChampionship = apiDto.scoringConfig.championships.find(c => c.type === 'driver') ?? apiDto.scoringConfig.championships[0];
    const positionPoints: { position: number; points: number }[] = primaryChampionship?.pointsPreview
      .filter((p): p is { sessionType: string; position: number; points: number } => p.sessionType === primaryChampionship.sessionTypes[0])
      .map(p => ({ position: p.position, points: p.points }))
      .sort((a, b) => a.position - b.position) || [];

    return {
      leagueId: apiDto.leagueId,
      gameName: apiDto.scoringConfig.gameName,
      scoringPresetName: apiDto.scoringConfig.scoringPresetName,
      championshipsCount: apiDto.scoringConfig.championships.length,
      sessionTypes: primaryChampionship?.sessionTypes.join(', ') || 'Main',
      dropPolicySummary: apiDto.scoringConfig.dropPolicySummary,
      hasActiveDropPolicy: !apiDto.scoringConfig.dropPolicySummary.includes('All'),
      positionPoints,
      bonusPoints: primaryChampionship?.bonusSummary || [],
      hasBonusPoints: (primaryChampionship?.bonusSummary.length || 0) > 0,
    };
  }
}