import { IUseCaseError, UseCaseError } from "@/contexts/common/application/useCases";
import { IServerError } from "@/contexts/common/domain/errors";
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
import { ExpressController } from "@/contexts/common/infrastructure/express";
import { SetStatusQuoteUseCase } from "@/contexts/sales/application";
import {
  ensureIdIsValid,
  ensureSetStatusQuote_Request_DTOIsValid,
  ISetStatusQuote_Request_DTO,
} from "@shared/contexts";
import { ISalesContext } from "../../../../Sales.context";

export class SetStatusQuoteController extends ExpressController {
  private useCase: SetStatusQuoteUseCase;
  private context: ISalesContext;

  constructor(props: { useCase: SetStatusQuoteUseCase }, context: ISalesContext) {
    super();

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

  async executeImpl(): Promise<any> {
    try {
      const { quoteId } = this.req.params;
      const newStatusDTO: ISetStatusQuote_Request_DTO = this.req.body;

      // 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);
      }

      // Validar DTO de datos
      const newStatusDTOOrError = ensureSetStatusQuote_Request_DTOIsValid(newStatusDTO);

      if (newStatusDTOOrError.isFailure) {
        const errorMessage = "New quote status is not valid";
        const infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          errorMessage,
          newStatusDTOOrError.error
        );
        return this.invalidInputError(errorMessage, infraError);
      }

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

      if (result.isFailure) {
        return this._handleExecuteError(result.error);
      }
      return this.noContent();
    } catch (e: unknown) {
      return this.fail(e as IServerError);
    }
  }

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

    switch (error.code) {
      case UseCaseError.NOT_FOUND_ERROR:
        errorMessage = "Quote not found";

        infraError = InfrastructureError.create(
          InfrastructureError.RESOURCE_NOT_FOUND_ERROR,
          errorMessage,
          error
        );

        return this.notFoundError(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);
    }
  }
}
