import type { Logger } from '@core/shared/domain/Logger';
import { Inject, Injectable } from '@nestjs/common';

// Use cases
import { ReviewProtestUseCase } from '@core/racing/application/use-cases/ReviewProtestUseCase';

// Presenter
import { ReviewProtestPresenter, type ReviewProtestResponseDTO } from './presenters/ReviewProtestPresenter';

// Tokens
import { LOGGER_TOKEN } from './ProtestsProviders';


@Injectable()
export class ProtestsService {
  constructor(
    private readonly reviewProtestUseCase: ReviewProtestUseCase,
    private readonly reviewProtestPresenter: ReviewProtestPresenter,
    @Inject(LOGGER_TOKEN) private readonly logger: Logger,
  ) {}

  async reviewProtest(command: {
    protestId: string;
    stewardId: string;
    decision: 'uphold' | 'dismiss';
    decisionNotes: string;
  }): Promise<ReviewProtestResponseDTO> {
    this.logger.debug('[ProtestsService] Reviewing protest:', command);

    const result = await this.reviewProtestUseCase.execute(command);

    if (result.isErr()) {
      const err = result.unwrapErr();
      this.reviewProtestPresenter.presentError(err);
      return this.reviewProtestPresenter.responseModel;
    }

    // Present the result with the additional context
    this.reviewProtestPresenter.present(result.unwrap(), {
      stewardId: command.stewardId,
      decision: command.decision,
    });

    return this.reviewProtestPresenter.responseModel;
  }
}