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 { CreateDealerUseCase } from "@/contexts/sales/application";
import { Dealer } from "@/contexts/sales/domain/entities";
import {
  ICreateDealer_Request_DTO,
  ICreateDealer_Response_DTO,
  ensureCreateDealer_Request_DTOIsValid,
} from "@shared/contexts";
import { ISalesContext } from "../../../../Sales.context";
import { ICreateDealerPresenter } from "./presenter";

export class CreateDealerController extends ExpressController {
  private useCase: CreateDealerUseCase;
  private presenter: ICreateDealerPresenter;
  private context: ISalesContext;

  constructor(
    props: {
      useCase: CreateDealerUseCase;
      presenter: ICreateDealerPresenter;
    },
    context: ISalesContext
  ) {
    super();

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

  async executeImpl() {
    try {
      const dealerDTO: ICreateDealer_Request_DTO = this.req.body;

      // Validaciones de DTO
      const dealerDTOOrError = ensureCreateDealer_Request_DTOIsValid(dealerDTO);

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

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

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

      const dealer = <Dealer>result.object;

      return this.created<ICreateDealer_Response_DTO>(this.presenter.map(dealer, 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 = "Dealer data not valid";
        infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          errorMessage,
          error
        );
        return this.invalidInputError(errorMessage, infraError);
        break;

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

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

      case UseCaseError.REPOSITORY_ERROR:
        errorMessage = "Error saving dealer";
        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);
    }
  }
}
