import type { ChampionshipConfig } from '../../../racing/domain/types/ChampionshipConfig';
import type { ChampionshipType } from '../../../racing/domain/types/ChampionshipType';
import type { SessionType } from '../../../racing/domain/types/SessionType';
import type { BonusRule } from '../../../racing/domain/types/BonusRule';
import type { DropScorePolicy, DropScoreStrategy } from '../../../racing/domain/types/DropScorePolicy';
import { PointsTable } from '../../../racing/domain/value-objects/PointsTable';

interface ChampionshipConfigInput {
  id: string;
  name: string;
  sessionTypes: SessionType[];
  mainPoints?: number[];
  mainBonusRules?: BonusRule[];
  type?: ChampionshipType;
  dropScorePolicy?: DropScorePolicy;
  strategy?: DropScoreStrategy;
}

export function makeChampionshipConfig(input: ChampionshipConfigInput): ChampionshipConfig {
  const {
    id,
    name,
    sessionTypes,
    mainPoints = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1],
    mainBonusRules = [],
    type = 'driver',
    dropScorePolicy = { strategy: 'none' },
  } = input;

  const pointsTableBySessionType: Record<SessionType, PointsTable> = {} as Record<SessionType, PointsTable>;
  
  // Convert array format to PointsTable for each session type
  sessionTypes.forEach(sessionType => {
    const pointsArray = mainPoints;
    const pointsMap: Record<number, number> = {};
    pointsArray.forEach((points, index) => {
      pointsMap[index + 1] = points;
    });
    pointsTableBySessionType[sessionType] = new PointsTable(pointsMap);
  });

  const bonusRulesBySessionType: Record<SessionType, BonusRule[]> = {} as Record<SessionType, BonusRule[]>;
  
  // Add bonus rules for each session type
  sessionTypes.forEach(sessionType => {
    bonusRulesBySessionType[sessionType] = mainBonusRules;
  });

  return {
    id,
    name,
    type,
    sessionTypes,
    pointsTableBySessionType,
    bonusRulesBySessionType,
    dropScorePolicy,
  };
}