import { ApplyPenaltyCommandDTO } from '../../types/generated/ApplyPenaltyCommandDTO';

export interface ProtestDecisionData {
  decision: 'uphold' | 'dismiss' | null;
  penaltyType: string;
  penaltyValue?: number;
  stewardNotes: string;
}

export class ProtestDecisionCommandModel {
  decision: 'uphold' | 'dismiss' | null = null;
  penaltyType: string = 'time_penalty';
  penaltyValue: number = 5;
  stewardNotes: string = '';

  constructor(initial: Partial<ProtestDecisionData> = {}) {
    this.decision = initial.decision ?? null;
    this.penaltyType = initial.penaltyType ?? 'time_penalty';
    this.penaltyValue = initial.penaltyValue ?? 5;
    this.stewardNotes = initial.stewardNotes ?? '';
  }

  get isValid(): boolean {
    return this.decision !== null && this.stewardNotes.trim().length > 0;
  }

  get canSubmit(): boolean {
    return this.isValid;
  }

  reset(): void {
    this.decision = null;
    this.penaltyType = 'time_penalty';
    this.penaltyValue = 5;
    this.stewardNotes = '';
  }

  toApplyPenaltyCommand(
    raceId: string,
    driverId: string,
    stewardId: string,
    protestId: string,
    options?: {
      requiresValue?: boolean;
      defaultUpheldReason?: string;
      defaultDismissedReason?: string;
    },
  ): ApplyPenaltyCommandDTO {
    const reason =
      this.decision === 'uphold'
        ? (options?.defaultUpheldReason ?? 'Protest upheld')
        : (options?.defaultDismissedReason ?? 'Protest dismissed');

    const base: ApplyPenaltyCommandDTO = {
      raceId,
      driverId,
      stewardId,
      type: this.penaltyType,
      reason,
      protestId,
      notes: this.stewardNotes,
    };

    if (options?.requiresValue) {
      return { ...base, value: this.penaltyValue };
    }

    return base;
  }
}