import AppError from "../../errors/AppError";
import { set } from "../../helpers/RedisClient";
import StatusChatEnd from "../../models/StatusChatEnd";
import AssociateQuickAnswerQueue from "../QueueService/AssociateQuickAnswerQueue";

interface Request {
  name: string;
  farewellMessage: string;
  isDefault: boolean;
}

const CreateStatusChatEndService = async ({
  name,
  farewellMessage,
  isDefault = false
}: Request): Promise<StatusChatEnd> => {
  const nameExists = await StatusChatEnd.findOne({
    where: { name }
  });

  if (nameExists) {
    throw new AppError("ERR_STATUS_CHAT_END_DUPLICATED");
  }

  const statusChatEnd = await StatusChatEnd.create({ name, farewellMessage, isDefault });

  const { id } = statusChatEnd;
  await set(`statusChatEnd:${id}`, { id, name, farewellMessage, isDefault });

  return statusChatEnd;
};

export default CreateStatusChatEndService;
