import StatusChatEnd from "../../models/StatusChatEnd";
import AppError from "../../errors/AppError";
import { Op } from "sequelize";

interface Request {
  id?: string | number;
  name?: string;
  isDefault?: boolean;
}

const ShowStatusChatEndService = async ({
  id,
  name,
  isDefault
}: Request): Promise<StatusChatEnd> => {
  let statusChatEnd: any;

  if (id) {
    statusChatEnd = await StatusChatEnd.findByPk(id, {
      attributes: ["id", "name", "farewellMessage", "isDefault"]
    });
  } else if (name) {
    statusChatEnd = await StatusChatEnd.findOne({
      where: { name },
      attributes: ["id", "name", "farewellMessage", "isDefault"]
    });
  } else if (isDefault) {
    statusChatEnd = await StatusChatEnd.findOne({
      where: { isDefault },
      attributes: ["id", "name", "farewellMessage", "isDefault"]
    });

    if (!statusChatEnd) {
      statusChatEnd = await StatusChatEnd.findOne();
    }
  }

  if (!statusChatEnd) {
    throw new AppError("ERR_NO_STATUS_FOUND", 404);
  }

  return statusChatEnd;
};

export default ShowStatusChatEndService;
