import type { UseCaseOutputPort } from '@core/shared/application';
import type { ReviewProtestResult, ReviewProtestApplicationError } from '@core/racing/application/use-cases/ReviewProtestUseCase';

export interface ReviewProtestResponseDTO {
  success: boolean;
  errorCode?: string;
  message?: string;
  protestId?: string;
  stewardId?: string;
  decision?: 'uphold' | 'dismiss';
}

export class ReviewProtestPresenter implements UseCaseOutputPort<ReviewProtestResult> {
  private model: ReviewProtestResponseDTO | null = null;
  private command: { stewardId: string; decision: 'uphold' | 'dismiss' } | null = null;

  reset(): void {
    this.model = null;
    this.command = null;
  }

  setCommand(command: { stewardId: string; decision: 'uphold' | 'dismiss' }): void {
    this.command = command;
  }

  present(result: ReviewProtestResult): void {
    if (!this.command) {
      throw new Error('Command must be set before presenting result');
    }
    
    this.model = {
      success: true,
      protestId: result.protestId,
      stewardId: this.command.stewardId,
      decision: this.command.decision,
    };
  }

  presentError(error: ReviewProtestApplicationError): void {
    this.model = {
      success: false,
      errorCode: error.code,
      message: error.details.message,
    };
  }

  getResponseModel(): ReviewProtestResponseDTO | null {
    return this.model;
  }

  get responseModel(): ReviewProtestResponseDTO {
    if (!this.model) {
      throw new Error('Presenter not presented');
    }

    return this.model;
  }
}
