import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";

const ShowWhatsAppService = async (
  id: string | number | null,
  whatsAppOfficial?: any
): Promise<Whatsapp> => {
  let whatsapp;

  if (id) {
    whatsapp = await Whatsapp.findByPk(id, {
      include: [
        {
          model: Queue,
          as: "queues",
          attributes: [
            "id",
            "name",
            "color",
            "greetingMessage", 
          ]
        }
      ],
      order: [["queues", "id", "ASC"]]
    });
  } else {
    whatsapp = await Whatsapp.findOne({
      where: { number: whatsAppOfficial?.number },
      include: [
        {
          model: Queue,
          as: "queues",
          attributes: [
            "id",
            "name",
            "color",
            "greetingMessage", 
          ]
        }
      ],
      order: [["queues", "id", "ASC"]]
    });
  }

  if (!whatsapp) {
    throw new AppError("ERR_NO_WAPP_FOUND", 404);
  }

  return whatsapp;
};

export default ShowWhatsAppService;
