import { IUseCaseError, UseCaseError } from "@/contexts/common/application";
import { IServerError } from "@/contexts/common/domain/errors";
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
import { ExpressController } from "@/contexts/common/infrastructure/express";
import { DuplicateQuoteUseCase } from "@/contexts/sales/application";
import { Quote } from "@/contexts/sales/domain/entities";
import { ensureIdIsValid, IDuplicateQuote_Response_DTO } from "@shared/contexts";
import { ISalesContext } from "../../../../Sales.context";
import { IDuplicateQuotePresenter } from "./presenter";

export class DuplicateQuoteController extends ExpressController {
  private useCase: DuplicateQuoteUseCase;
  private presenter: IDuplicateQuotePresenter;
  private context: ISalesContext;

  constructor(
    props: {
      useCase: DuplicateQuoteUseCase;
      presenter: IDuplicateQuotePresenter;
    },
    context: ISalesContext
  ) {
    super();

    const { useCase, presenter } = props;
    this.useCase = useCase;
    this.presenter = presenter;
    this.context = context;
  }

  async executeImpl() {
    try {
      const { quoteId } = this.req.params;

      // Validar ID
      const quoteIdOrError = ensureIdIsValid(quoteId);
      if (quoteIdOrError.isFailure) {
        const errorMessage = "Quote ID is not valid";
        const infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          errorMessage,
          quoteIdOrError.error
        );
        return this.invalidInputError(errorMessage, infraError);
      }

      // Llamar al caso de uso
      const result = await this.useCase.execute({
        sourceId: quoteIdOrError.object,
      });

      if (result.isFailure) {
        return this._handleExecuteError(result.error);
      }

      const quote = <Quote>result.object;

      return this.created<IDuplicateQuote_Response_DTO>(this.presenter.map(quote, this.context));
    } catch (e: unknown) {
      return this.fail(e as IServerError);
    }
  }

  private _handleExecuteError(error: IUseCaseError) {
    let errorMessage: string;
    let infraError: IInfrastructureError;

    switch (error.code) {
      case UseCaseError.INVALID_INPUT_DATA:
        errorMessage = "Quote data not valid";
        infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          errorMessage,
          error
        );
        return this.invalidInputError(errorMessage, infraError);
        break;

      case UseCaseError.RESOURCE_ALREADY_EXITS:
        errorMessage = "Quote already exists";

        infraError = InfrastructureError.create(
          InfrastructureError.RESOURCE_ALREADY_REGISTERED,
          errorMessage,
          error
        );
        return this.conflictError(errorMessage, infraError);
        break;

      case UseCaseError.REPOSITORY_ERROR:
        errorMessage = "Error saving quote";
        infraError = InfrastructureError.create(
          InfrastructureError.UNEXCEPTED_ERROR,
          errorMessage,
          error
        );
        return this.conflictError(errorMessage, infraError);
        break;

      case UseCaseError.UNEXCEPTED_ERROR:
        errorMessage = error.message;

        infraError = InfrastructureError.create(
          InfrastructureError.UNEXCEPTED_ERROR,
          errorMessage,
          error
        );
        return this.internalServerError(errorMessage, infraError);
        break;

      default:
        errorMessage = error.message;
        return this.clientError(errorMessage);
    }
  }
}
