import type { LeagueConfigFormModel } from '@/lib/types/LeagueConfigFormModel';
import type { LeagueScoringPresetViewModel } from '@/lib/view-models/LeagueScoringPresetViewModel';
import type { CustomPointsConfig } from '@/lib/view-models/ScoringConfigurationViewModel';

/**
 * LeagueScoringSectionViewModel
 *
 * View model for the league scoring section UI state and operations
 */
export class LeagueScoringSectionViewModel {
  readonly form: LeagueConfigFormModel;
  readonly presets: LeagueScoringPresetViewModel[];
  readonly readOnly: boolean;
  readonly patternOnly: boolean;
  readonly championshipsOnly: boolean;
  readonly disabled: boolean;
  readonly currentPreset: LeagueScoringPresetViewModel | null;
  readonly isCustom: boolean;

  constructor(
    form: LeagueConfigFormModel,
    presets: LeagueScoringPresetViewModel[],
    options: {
      readOnly?: boolean;
      patternOnly?: boolean;
      championshipsOnly?: boolean;
    } = {}
  ) {
    this.form = form;
    this.presets = presets;
    this.readOnly = options.readOnly || false;
    this.patternOnly = options.patternOnly || false;
    this.championshipsOnly = options.championshipsOnly || false;
    this.disabled = this.readOnly;
    this.currentPreset = form.scoring.patternId 
      ? presets.find(p => p.id === form.scoring.patternId) || null
      : null;
    this.isCustom = form.scoring.customScoringEnabled || false;
  }

  /**
   * Get default custom points configuration
   */
  static getDefaultCustomPoints(): CustomPointsConfig {
    return {
      racePoints: [25, 18, 15, 12, 10, 8, 6, 4, 2, 1],
      poleBonusPoints: 1,
      fastestLapPoints: 1,
      leaderLapPoints: 0,
    };
  }

  /**
   * Check if form can be modified
   */
  canModify(): boolean {
    return !this.readOnly;
  }

  /**
   * Get available presets for display
   */
  getAvailablePresets(): LeagueScoringPresetViewModel[] {
    return this.presets;
  }

  /**
   * Get championships configuration for display
   */
  getChampionshipsConfig() {
    const isTeamsMode = this.form.structure.mode === 'fixedTeams';
    
    return [
      {
        key: 'enableDriverChampionship' as const,
        label: 'Driver Standings',
        description: 'Track individual driver points',
        enabled: this.form.championships.enableDriverChampionship,
        available: true,
      },
      {
        key: 'enableTeamChampionship' as const,
        label: 'Team Standings',
        description: 'Combined team points',
        enabled: this.form.championships.enableTeamChampionship,
        available: isTeamsMode,
        unavailableHint: 'Teams mode only',
      },
      {
        key: 'enableNationsChampionship' as const,
        label: 'Nations Cup',
        description: 'By nationality',
        enabled: this.form.championships.enableNationsChampionship,
        available: true,
      },
      {
        key: 'enableTrophyChampionship' as const,
        label: 'Trophy Cup',
        description: 'Special category',
        enabled: this.form.championships.enableTrophyChampionship,
        available: true,
      },
    ];
  }

  /**
   * Get panel visibility based on flags
   */
  shouldShowPatternPanel(): boolean {
    return !this.championshipsOnly;
  }

  shouldShowChampionshipsPanel(): boolean {
    return !this.patternOnly;
  }

  /**
   * Get the active custom points configuration
   */
  getActiveCustomPoints(): CustomPointsConfig {
    // This would be stored separately in the form model
    // For now, return defaults
    return LeagueScoringSectionViewModel.getDefaultCustomPoints();
  }
}