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

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

interface Request {
  quickAnswerData: QuickAnswerData;
  quickAnswerId: string;
}

const UpdateQuickAnswerService = async ({
  quickAnswerData,
  quickAnswerId
}: Request): Promise<QuickAnswer> => {
  const { shortcut, message, queueIds } = quickAnswerData;

  const quickAnswer = await QuickAnswer.findOne({
    where: { id: quickAnswerId },
    attributes: ["id", "shortcut", "message"]
  });

  if (!quickAnswer) {
    throw new AppError("ERR_NO_QUICK_ANSWERS_FOUND", 404);
  }
  await quickAnswer.update({
    shortcut,
    message
  });

  if (queueIds) await AssociateQuickAnswerQueue(quickAnswer, queueIds);

  await quickAnswer.reload({
    attributes: ["id", "shortcut", "message"]
  });

  return quickAnswer;
};

export default UpdateQuickAnswerService;
