import { ILeagueFullConfigPresenter, LeagueFullConfigData, LeagueConfigFormViewModel } from '@core/racing/application/presenters/ILeagueFullConfigPresenter';
import { LeagueConfigFormModelDTO } from '../dtos/LeagueConfigFormModelDTO';

export class LeagueConfigPresenter implements ILeagueFullConfigPresenter {
  private result: LeagueConfigFormViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: LeagueFullConfigData) {
    // Map from LeagueFullConfigData to LeagueConfigFormViewModel
    const league = dto.league;
    const settings = league.settings;
    const stewarding = settings.stewarding;

    this.result = {
      leagueId: league.id,
      basics: {
        name: league.name,
        description: league.description,
        visibility: 'public', // TODO: Map visibility from league
        gameId: 'iracing', // TODO: Map from game
      },
      structure: {
        mode: 'solo', // TODO: Map from league settings
        maxDrivers: settings.maxDrivers || 32,
        multiClassEnabled: false, // TODO: Map
      },
      championships: {
        enableDriverChampionship: true, // TODO: Map
        enableTeamChampionship: false,
        enableNationsChampionship: false,
        enableTrophyChampionship: false,
      },
      scoring: {
        customScoringEnabled: false, // TODO: Map
      },
      dropPolicy: {
        strategy: 'none', // TODO: Map
      },
      timings: {
        practiceMinutes: 30, // TODO: Map
        qualifyingMinutes: 15,
        mainRaceMinutes: 60,
        sessionCount: 1,
        roundsPlanned: 10, // TODO: Map
      },
      stewarding: {
        decisionMode: stewarding?.decisionMode || 'admin_only',
        requireDefense: stewarding?.requireDefense || false,
        defenseTimeLimit: stewarding?.defenseTimeLimit || 48,
        voteTimeLimit: stewarding?.voteTimeLimit || 72,
        protestDeadlineHours: stewarding?.protestDeadlineHours || 48,
        stewardingClosesHours: stewarding?.stewardingClosesHours || 168,
        notifyAccusedOnProtest: stewarding?.notifyAccusedOnProtest || true,
        notifyOnVoteRequired: stewarding?.notifyOnVoteRequired || true,
        requiredVotes: stewarding?.requiredVotes,
      },
    };
  }

  getViewModel(): LeagueConfigFormViewModel | null {
    return this.result;
  }

  // API-specific method to get the DTO
  get viewModel(): LeagueConfigFormModelDTO | null {
    if (!this.result) return null;

    // Map from LeagueConfigFormViewModel to LeagueConfigFormModelDto
    return {
      leagueId: this.result.leagueId,
      basics: {
        name: this.result.basics.name,
        description: this.result.basics.description,
        visibility: this.result.basics.visibility as 'public' | 'private',
      },
      structure: {
        mode: this.result.structure.mode as 'solo' | 'team',
      },
      championships: [], // TODO: Map championships
      scoring: {
        type: 'standard', // TODO: Map scoring type
        points: 25, // TODO: Map points
      },
      dropPolicy: {
        strategy: this.result.dropPolicy.strategy as 'none' | 'worst_n',
        n: this.result.dropPolicy.n,
      },
      timings: {
        raceDayOfWeek: 'sunday', // TODO: Map from timings
        raceTimeHour: 20,
        raceTimeMinute: 0,
      },
      stewarding: {
        decisionMode: this.result.stewarding.decisionMode === 'steward_vote' ? 'committee_vote' : 'single_steward',
        requireDefense: this.result.stewarding.requireDefense,
        defenseTimeLimit: this.result.stewarding.defenseTimeLimit,
        voteTimeLimit: this.result.stewarding.voteTimeLimit,
        protestDeadlineHours: this.result.stewarding.protestDeadlineHours,
        stewardingClosesHours: this.result.stewarding.stewardingClosesHours,
        notifyAccusedOnProtest: this.result.stewarding.notifyAccusedOnProtest,
        notifyOnVoteRequired: this.result.stewarding.notifyOnVoteRequired,
        requiredVotes: this.result.stewarding.requiredVotes,
      },
    };
  }
}