import AppError from "../../errors/AppError";
import QuickAnswer from "../../models/QuickAnswer";
import AssociateQuickAnswerQueue from "../QueueService/AssociateQuickAnswerQueue";

interface Request {
  shortcut: string;
  message: string;
  queueIds?: number[];
}

const CreateQuickAnswerService = async ({
  shortcut,
  message,
  queueIds = []
}: Request): Promise<QuickAnswer> => {
  const nameExists = await QuickAnswer.findOne({
    where: { shortcut }
  });

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

  const quickAnswer = await QuickAnswer.create({ shortcut, message });

  await AssociateQuickAnswerQueue(quickAnswer, queueIds);

  return quickAnswer;
};

export default CreateQuickAnswerService;
