import axios from "axios";
import log from "../config/log";

export const sendWhatsapp = async (
  mobile: string,
  name: string,
  propName: string,
  template: string
) => {
  const F = "sendWhatsapp";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: template,
        languageCode: "en",
        bodyValues: [name, propName],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappOwnerWelcome = async (
  mobile: string,
  name: string,
) => {
  const F = "sendWhatsappOwnerWelcome";
  const templateName = "owneronboarding";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantWelcome = async (
  mobile: string,
  name: string,
  propName: string,
  rent: string,
  security: string,
  roomNum: string,
  moveInDate: string,
  footer: string,
  template: string
) => {
  const F = "sendWhatsappTenantWelcome";
  const templateName = template;

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, propName, propName, roomNum, moveInDate, rent, security, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantWelcomeWithLink = async (
  mobile: string,
  name: string,
  propName: string,
  rent: string,
  security: string,
  roomNum: string,
  moveInDate: string,
  footer: string,
  template: string,
  androidLink: string,
  iosLink: string,
) => {
  const F = "sendWhatsappTenantWelcomeWithLink";
  const templateName = template;

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, propName, propName, roomNum, moveInDate, rent, security, androidLink, iosLink, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappStaffWelcome = async (
  mobile: string,
  name: string,
  ownerName: string,
  propName: string,
  footer: string,
) => {
  const F = "sendWhatsappStaffWelcome";
  const templateName = "staffwelcomenew";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, propName, ownerName, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantDues = async (
  mobile: string,
  name: string,
  rentAmount: string,
  dueDate: string,
  footer: string,
) => {
  const F = "WhatsApp Dues Reminder";
  const templateName = "tenantrentdues_withoutllink_live";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en_US",
        bodyValues: [name, rentAmount, dueDate, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappNewCompTenant = async (
  mobile: string,
  name: string,
  complaint: string,
  propName: string,
  footerText: string
) => {
  const F = "sendWhatsappNewCompTenant";
  const templateName = "new_complaint_tenant";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, complaint, footerText],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappNewCompOwner = async (
  mobile: string,
  name: string,
  tenantName: string,
  propName: string,
  tenantMobile: string,
  complaint: string,
) => {
  const F = "sendWhatsappNewCompOwner";
  const templateName = "new_complaint_owner";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");
    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, tenantName, propName, tenantMobile, complaint],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappAssignedTenant = async (
  mobile: string,
  name: string,
  complaint: string,
  staffName: string,
  footer: string,
) => {
  const F = "sendWhatsappAssignedTenant";
  const templateName="complaint_assigned_tenant";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, complaint, staffName, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappAssignedStaff = async (
  mobile: string,
  name: string,
  propName: string,
  tenantName: string,
  tenantMobile: string,
  complaint: string,
  footer: string
) => {
  const F = "sendWhatsappAssignedStaff";
  const templateName="complaint_assigned_staff";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, propName, tenantName, tenantMobile, complaint, footer]
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappResolvedTenant = async (
  mobile: string,
  name: string,
  complaint: string,
) => {
  const F = "sendWhatsappResolvedTenant";
  const templateName="complaint_resolved_tenant";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, complaint],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappResolvedOwner = async (
  mobile: string,
  name: string,
  complaint: string,
  tenantName: string,
) => {
  const F = "sendWhatsappResolvedOwner";
  const templateName="complaint_resolved_owner";
  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, complaint, tenantName],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantSingleDue = async (
  mobile: string,
  name: string,
  propName: string,
  roomNum: string,
  rentAmount: string,
  paymentLink: string,
  footer: string,
) => {
  const F = "sendWhatsappTenantSingleDue";
  const templateName = "combinedtenantdues_reminder";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [name, propName, roomNum, rentAmount, paymentLink, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappOtp = async (
  mobile: string,
  otp: string,
  name: string="",
) => {
  const F = "sendWhatsappOtp";
  const templateName = process.env.WHATSAPP_OTP_TEMPLATE;

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [otp],
        buttonValues: {0:[otp]},
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappPaymentConfirmationClient = async (
  mobile: string,
  clientName: string,
  tenantName: string,
  amount: number,
  propName: string,
  roomNum: string,
  dueName: string,
  collectionDate: string,
  mode: string,
  recordedBy: string,
) => {
  const F = "sendWhatsappPaymentConfirmationClient";
  const templateName = "paymentconfirmationownernew";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: clientName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [tenantName, amount.toString(), propName, roomNum, dueName, collectionDate, mode, recordedBy],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappPaymentConfirmationTenant = async (
  mobile: string,
  tenantName: string,
  amount: number,
  propName: string,
  roomNum: string,
  receipt: string,
  footer: string,
) => {
  const F = "sendWhatsappPaymentConfirmationTenant";
  const templateName = "paymentconfirmationtenantnew";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [amount.toString(), propName, roomNum, receipt, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappParcel = async (
  mobile: string,
  tenantName: string,
  deliveryPartner: string,
  url: string | null,
  footer: string,
) => {
  const F = "sendWhatsappParcel";
  const templateName = "tenant_courier_notification";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        headerValues: [url],
        bodyValues: [tenantName, deliveryPartner, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};
export const sendWhatsappParcelReturned = async (
  mobile: string,
  tenantName: string,
  deliveryPartner: string,
  footer: string,
) => {
  const F = "sendWhatsappParcelReturned";
  const templateName = "tenant_courier_returned";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [tenantName, deliveryPartner, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappParcelCollection = async (
  mobile: string,
  tenantName: string,
  deliveryPartner: string,
  footer: string,
) => {
  const F = "sendWhatsappParcelCollection";
  const templateName = "tenant_courier_collected";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [tenantName, deliveryPartner, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantKycReminder = async (
  mobile: string,
  tenantName: string,
  footer: string,
) => {
  const F = "sendWhatsappTenantKycReminder";
  const templateName = "tenantkycreminder";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [tenantName, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappStaffLeadAssigned = async (
  mobile: string,
  leadName: string,
  leadMobile: string,
  footer: string,
) => {
  const F = "sendWhatsappStaffLeadAssigned";
  const templateName = "leadassignment2staff";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: leadName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [leadName, leadMobile, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappStaffKycDone = async (
  mobile: string,
  staffName: string,
  fatherName: string,
  address: string,
  aadhaar: string,
) => {
  const F = "sendWhatsappStaffKycDone";
  const templateName = "staffkyccompleted";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: staffName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [staffName, staffName, fatherName, aadhaar, address],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantRentAgreementRenew = async (
  mobile: string,
  tenantName: string,
  occupancyName: string,
  rent: string,
  startDate: string,
  endDate: string,
  footer: string,
) => {
  const F = "sendWhatsappTenantRentAgreementRenew";
  const templateName = "agreementrenewalnoticetotenant";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [tenantName, occupancyName, rent, startDate, endDate, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export const sendWhatsappTenantMoveOutInitiated = async (
  mobile: string,
  tenantName: string,
  propName: string,
  moveOutDate: string,
  totalDues: string,
  footer: string,
) => {
  const F = "sendWhatsappTenantMoveOutInitiated";
  const templateName = "scheduledmoveoutnotification";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");

    const body = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      traits: {
        name: tenantName,
        email: "",
      },
      tags: ["sample-tag-1", "sample-tag-2"],
    };

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return false;
    }

    const messageBody = {
      countryCode: "+91",
      userId: formattedMobile,
      phoneNumber: formattedMobile,
      callbackData: "some text here",
      type: "Template",
      template: {
        name: templateName,
        languageCode: "en",
        bodyValues: [propName, moveOutDate, totalDues, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: process.env.WHATSAPP_API_KEY,
        },
      }
    );

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response2?.data
        )}`
      );
      return false;
    }
  } catch (error: any) {
    log.info(
      `[${F}], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export default sendWhatsapp;
