import type { SessionType } from '@core/racing/domain/types/SessionType';
import { PointsTable } from '@core/racing/domain/value-objects/PointsTable';
import type { BonusRule } from '@core/racing/domain/types/BonusRule';
import type { ChampionshipConfig } from '@core/racing/domain/types/ChampionshipConfig';
import { makePointsTable } from './PointsTableFactory';

export const makeChampionshipConfig = (params: {
  id: string;
  name: string;
  sessionTypes: SessionType[];
  mainPoints: number[];
  sprintPoints?: number[];
  mainBonusRules?: BonusRule[];
}): ChampionshipConfig => {
  const { id, name, sessionTypes, mainPoints, sprintPoints, mainBonusRules } = params;

  const pointsTableBySessionType: Record<SessionType, PointsTable> = {} as Record<SessionType, PointsTable>;

  sessionTypes.forEach((sessionType) => {
    if (sessionType === 'main') {
      pointsTableBySessionType[sessionType] = makePointsTable(mainPoints);
    } else if (sessionType === 'sprint' && sprintPoints) {
      pointsTableBySessionType[sessionType] = makePointsTable(sprintPoints);
    } else {
      pointsTableBySessionType[sessionType] = new PointsTable({});
    }
  });

  const bonusRulesBySessionType: Record<SessionType, BonusRule[]> = {} as Record<SessionType, BonusRule[]>;
  sessionTypes.forEach((sessionType) => {
    if (sessionType === 'main' && mainBonusRules) {
      bonusRulesBySessionType[sessionType] = mainBonusRules;
    } else {
      bonusRulesBySessionType[sessionType] = [];
    }
  });

  return {
    id,
    name,
    type: 'driver',
    sessionTypes,
    pointsTableBySessionType,
    bonusRulesBySessionType,
    dropScorePolicy: {
      strategy: 'none',
    },
  };
};