import Contact from "../../models/Contact";
import AppError from "../../errors/AppError";
import { getSettingValue } from "../../helpers/WhaticketSettings";
import Queue from "../../models/Queue";

const ShowContactService = async (id: string | number): Promise<Contact> => {
  let includeQueue: any[] = [];

  if (getSettingValue("contactByqueues")?.value == "enabled") {
    includeQueue = [
      { model: Queue, as: "queues", attributes: ["id", "name", "color"] }
    ];
  }

  const contact = await Contact.findByPk(id, {
    include: ["extraInfo", ...includeQueue]
  });

  if (!contact) {
    throw new AppError("ERR_NO_CONTACT_FOUND", 404);
  }

  return contact;
};

export default ShowContactService;
