import axios from "axios";
import log from "../config/log";
import moment from "moment";
import clientConfigDB from "../models/clientConfig.model";
import CONSTANTS from "../config/constants";
import getClientWhatsappCredentials from "./getClientWhatsappCredentials";

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

  try {
    
    let formattedMobile = mobile.trim().replace(" ", "");
    let credentials = await getClientWhatsappCredentials(clientId, propId);

    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: credentials?.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: credentials?.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;
  }
};

const addUserToWhatsapp = async (
  mobile: string,
  tenantName: string,
  key: any,
) => {
  const F = "addUserToWhatsapp";
  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: key,
        },
      },
    );
    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], User Added successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to add user, Response : ${JSON.stringify(
          response?.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,
  clientId : number = 0,
  propId : number = 0,

) => {
  const F = "sendWhatsappTenantWelcome";
  const templateName = template;

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

    let credentials = await getClientWhatsappCredentials(clientId, propId);


    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantWelcomeWithLink";
  const templateName = template;

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappStaffWelcome";
  const templateName = "staffwelcomenew";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "WhatsApp Dues Reminder";
  const templateName = "tenantrentdues_withoutllink_live";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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",
        languageCode: "en",
        bodyValues: [name, rentAmount, dueDate, footer],
      },
    };

    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappNewCompTenant";
  const templateName = "new_complaint_tenant";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappNewCompOwner";
  let templateName = "notifyowneronnewcomplaint";
  if(clientId === 519) {
    templateName = "new_complaint_owner";
  }
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappAssignedTenant";
  let templateName="complaint_assigned_tenant_6o";
  if(519 == clientId) {
    templateName="complaint_assigned_tenant";
  }
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappAssignedStaff";
  const templateName="complaint_assigned_staff";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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 sendWhatsappAssignedStaffNew = async (
  mobile: string,
  name: string,
  propName: string,
  roomNo: string,
  tenantName: string,
  tenantMobile: string,
  complaint: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappAssignedStaffNew";
  const templateName="complaintassignedtostaffnew";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    footer = footer.replace("Team", "").trim();

    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

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

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappResolvedTenant";
  const templateName="complaint_resolved_tenant";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappResolvedOwner";
  const templateName="complaint_resolved_owner";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId : number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantSingleDue";
  let  templateName = "combinedtenantdues_reminder";
  if(clientId === 519) {
    templateName = "combinedtenantdues_reminder_co";
  }

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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="",
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappOtp";
  const templateName = process.env.WHATSAPP_OTP_TEMPLATE;

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);

    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappPaymentConfirmationClient";
  const templateName = "paymentconfirmationownernew";
  tenantName = tenantName.trim();
  propName = propName.trim();
  try {

    let credentials = await getClientWhatsappCredentials(clientId, propId);

    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappPaymentConfirmationTenant";
  //const templateName = "paymentconfirmationtenantnew";
  const templateName = "paymentconfirmationtenantwithattachment";
  footer = footer.replace("Team", "").trim();
  const url = `${receipt}?v=${moment().format("YYYYMMDDHHmmss")}`;
  propName = propName.trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: [amount.toString(), propName, roomNum, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappParcel";
  const templateName = "tenant_courier_notification";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappParcelReturned";
  const templateName = "tenant_courier_returned";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappParcelCollection";
  const templateName = "tenant_courier_collected";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantKycReminder";
  const templateName = "tenantkycreminder";
  footer = footer.replace("Team", "").trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials.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: credentials?.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 sendWhatsappTenantKycReminderWithAppLink = async (
  mobile: string,
  tenantName: string,
  footer: string,
  android: string = CONSTANTS.DEFAULT_APP_LINK.ANDROID,
  ios: string = CONSTANTS.DEFAULT_APP_LINK.IOS,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantKycReminderWithAppLink";
  const templateName = "tenantkycremindewithlink";
  footer = footer.replace("Team", "").trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials.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, android, ios, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappStaffLeadAssigned";
  const templateName = "leadassignment2staff";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);

    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappStaffKycDone";
  const templateName = "staffkyccompleted";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantRentAgreementRenew";
  const templateName = "agreementrenewalnoticetotenant";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantMoveOutInitiated";
  const templateName = "scheduledmoveoutnotification";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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 sendWhatsappTenantBookingAmountWithLink = async (
  mobile: string,
  name: string,
  propName: string,
  roomNum: string,
  moveInDate: string,
  bookingAmount: number,
  paymentLink: string,
  footer: string,
  clientId : number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantBookingAmountWithLink";
  let  templateName = "booking_msg";

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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, moveInDate, bookingAmount, paymentLink, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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 sendWhatsappTenantSettlement = async (
  mobile: string,
  tenantName: string,
  url: string | null,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantSettlement";
  const templateName = "tenantfinalsettlement";
  footer = footer.replace("Team", "").trim();
  tenantName = tenantName.trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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, footer],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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 sendWhatsappNewRequest = async (
  mobile: string,
  name : string,
  tenantName: string,
  requestType: string,
  propName: string,
  roomNum: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappNewRequest";
  const templateName = "notifyowneronrequestinitiation";
  footer = footer.replace("Team", "").trim();
  tenantName = tenantName.trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappRequestUpdates = async (
  mobile: string,
  name: string,
  requestType: string,
  occupancyName: string,
  requestStatus: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappRequestUpdates";
  const templateName = "notifyonrequestupdate";
  footer = footer.replace("Team", "").trim();
  name = name.trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappRequestRejectionWithReason = async (
  mobile: string,
  tenantName: string,
  requestType: string,
  propName: string,
  reason: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappRequestRejectionWithReason";
  const templateName = "notifyonrequestrejection";
  footer = footer.replace("Team", "").trim();
  tenantName = tenantName.trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, tenantName, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappTenantSeparateSecurity = async (
  mobile: string,
  name: string,
  propName: string,
  roomNum: string,
  rentAmount: string,
  paymentLink: string,
  footer: string,
  clientId : number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappTenantSingleDue";
  let  templateName = "separatesecurityduealert";
  if(clientId === 519) {
    templateName = "separatesecuritydueco";
  }

  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    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: credentials?.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: credentials?.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 sendWhatsappPayoutReceived = async (
  mobile: string,
  name: string,
  amount: number,
  receivingAcc: string,
  clientName: string,
  utrNo: string,
  paymentDate: string,
  paymentMode: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappPayoutReceived";
  const templateName = "payout_credited";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");
    let credentials = await getClientWhatsappCredentials(clientId, propId);

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

    const maskedReceivingAcc = receivingAcc 
      ? "X".repeat(Math.max(0, receivingAcc.length - 4)) + receivingAcc.slice(-4)
      : "";

    const capitalizedMode = paymentMode?.toUpperCase();

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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, maskedReceivingAcc, paymentDate, capitalizedMode, clientName, utrNo],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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 sendWhatsappPayoutDebit = async (
  mobile: string,
  name: string,
  amount: number,
  receivingAcc: string,
  utrNo: string,
  paymentDate: string,
  paymentMode: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappPayoutDebit";
  const templateName = "payoutnotifytoclient";

  try {
    let formattedMobile = mobile.trim().replace(" ", "");
    let credentials = await getClientWhatsappCredentials(clientId, propId);

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

    const maskedReceivingAcc = receivingAcc 
      ? "X".repeat(Math.max(0, receivingAcc.length - 4)) + receivingAcc.slice(-4)
      : "";

    const capitalizedMode = paymentMode?.toUpperCase();

    const response: any = await axios.post(
      `${process.env.WHATSAPP_ADD_USER_URI}`,
      body,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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, maskedReceivingAcc, paymentDate, capitalizedMode, utrNo],
      },
    };
    const response2: any = await axios.post(
      `${process.env.WHATSAPP_SEND_MESSAGE_URI}`,
      messageBody,
      {
        timeout: 8000,
        headers: {
          "Content-Type": "application/json",
          Authorization: credentials?.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 sendWhatsappEvictionNotify = async (
  mobile: string,
  name : string,
  evictionDate: string,
  propName: string,
  roomNum: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappEvictionNotify";
  const templateName = "initiateeviction";
  footer = footer.replace("Team", "").trim();
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappNewLandlord = async (
  mobile: string,
  name : string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappNewRequest";
  const templateName = "landlord_welcome";
  try {
    if(clientId !=519) {
      return false;
    }
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappToParentOnIN = async (
  mobile: string,
  parentName: string,
  tenantName : string,
  propName : string,
  date : string,
  time : string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappToParentOnIN";
  const templateName = "tenant_attendance_in_new";
  try {
    if(clientId !=846) {
      return false;
    }
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    footer = footer.replace("Team", "").trim();
    parentName = parentName.trim();
    tenantName = tenantName.trim();
    propName = propName.trim();
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, parentName, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappToParentOnOut = async (
  mobile: string,
  parentName: string,
  tenantName : string,
  propName : string,
  date : string,
  time : string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappToParentOnOut";
  const templateName = "tenant_attendance_out";
  try {
    if(clientId !=846) {
      return false;
    }
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    footer = footer.replace("Team", "").trim();
    parentName = parentName.trim();
    tenantName = tenantName.trim();
    propName = propName.trim();
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, parentName, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappToParentOnAttendance = async (
  mobile: string,
  parentName: string,
  tenantName : string,
  attendanceType : string,
  propName : string,
  date : string,
  time : string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappToParentOnAttendance";
  const templateName = "tenant_attendance_record";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    footer = footer.replace("Team", "").trim();
    parentName = parentName.trim();
    tenantName = tenantName.trim();
    propName = propName.trim();
    
    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, parentName, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

    if (response2?.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 sendWhatsappAssignedNewPropComplaintStaff = async (
  mobile: string,
  name: string,
  propName: string,
  complaint: string,
  footer: string,
  clientId: number = 0,
  propId: number = 0,
) => {
  const F = "sendWhatsappAssignedNewPropComplaintStaff";
  const templateName="complaintassignedtostaffforprop";
  try {
    let credentials = await getClientWhatsappCredentials(clientId, propId);
    let formattedMobile = mobile.trim().replace(" ", "");
    footer = footer.replace("Team", "").trim();

    /*Adding User To interakt*/
    let userAdded = await addUserToWhatsapp(mobile, name, credentials?.key);
    if(!userAdded) {
      log.info(
        `[${F}], Client Id [${clientId}], Mobile [${mobile}], Failed to add user
        )}`
      );
      return false;
    }

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

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

    if (response?.data?.result) {
      log.info(
        `[${F}], Mobile [${mobile}], Message sent successfully, Response : ${JSON.stringify(
          response?.data
        )}`
      );
      return true;
    } else {
      log.info(
        `[${F}], Mobile [${mobile}], Unable to send message, Response : ${JSON.stringify(
          response?.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;
