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 { CreateQuoteUseCase } from "@/contexts/sales/application";
import { Quote } from "@/contexts/sales/domain/entities";
import {
  ICreateQuote_Request_DTO,
  ICreateQuote_Response_DTO,
  ensureCreateQuote_Request_DTOIsValid,
} from "@shared/contexts";
import { ISalesContext } from "../../../../Sales.context";
import { ICreateQuotePresenter } from "./presenter";

export class CreateQuoteController extends ExpressController {
  private useCase: CreateQuoteUseCase;
  private presenter: ICreateQuotePresenter;
  private context: ISalesContext;

  constructor(
    props: {
      useCase: CreateQuoteUseCase;
      presenter: ICreateQuotePresenter;
    },
    context: ISalesContext
  ) {
    super();

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

  async executeImpl() {
    try {
      const quoteDTO: ICreateQuote_Request_DTO = this.req.body;
      const dealer = this.context.dealer;

      if (!dealer) {
        const errorMessage = "Unexpected missing dealer data";
        const infraError = InfrastructureError.create(
          InfrastructureError.UNEXCEPTED_ERROR,
          errorMessage
        );
        return this.internalServerError(errorMessage, infraError);
      }

      // Validaciones de DTO
      const quoteDTOOrError = ensureCreateQuote_Request_DTOIsValid(quoteDTO);

      if (quoteDTOOrError.isFailure) {
        const errorMessage = "Quote data not valid";
        const infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          errorMessage,
          quoteDTOOrError.error
        );
        return this.invalidInputError(errorMessage, infraError);
      }

      // Llamar al caso de uso
      const result = await this.useCase.execute(quoteDTO);

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

      const quote = <Quote>result.object;

      return this.created<ICreateQuote_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);
    }
  }
}
