import Whatsapp from "../../models/Whatsapp";

const ListWhatsAppsNumber = async (
  whatsappId: string | number,
  status?: string
): Promise<Whatsapp[] | any> => {
  const whatsapp = await Whatsapp.findByPk(whatsappId, { raw: true });

  let whatsapps: any = [];

  if (whatsapp) {
    if (status) { 
      whatsapps = await Whatsapp.findAll({
        raw: true,
        where: { number: whatsapp.number, status: status, },
        attributes: ["id", "number", "status", "isDefault", "url", 'isOfficial']
      });  
    } else {
      whatsapps = await Whatsapp.findAll({
        raw: true,
        where: { number: whatsapp.number },
        attributes: ["id", "number", "status", "isDefault", "url", 'isOfficial']
      });
    }

    return { whatsapps, whatsapp };
  }

  return { whatsapps: [], whatsapp: null };
};

export default ListWhatsAppsNumber;
