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

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

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

    const messageBody = {
      to: formattedMobile,
      template_id: 'dpmnvbajrv0_tenantonboardinginformation',
      sender: "918169871676",
      sample: {
        bodyvar: [name, propName, propName, roomNum, moveInDate, rent, security],
      }
    };
    

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

    if (true == response?.data?.success) {
      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 sendWhatsappGharMumbaiForStaff = async (
  mobile: string,
  name: string,
  clientName: string,
  propName: string,
) => {
  const F = "sendWhatsappGharMumbaiForStaff";
  try {
    let formattedMobile = "91"+mobile.trim().replace(" ", "");

    const messageBody = {
      to: formattedMobile,
      template_id: '2knl3jkqzws_staffregistration',
      sender: "918169871676",
      sample: {
        bodyvar: [name, propName, clientName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharNewCompTenant = async (
  mobile: string,
  name: string,
  complaintName: string,
) => {
  const F = "sendWhatsappGharNewCompTenant";

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

    const messageBody = {
      to: formattedMobile,
      template_id: 'jl5xbpq868d_newcomplainttenant',
      sender: "918169871676",
      sample: {
        bodyvar: [name, complaintName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharNewCompOwner = async (
  mobile: string,
  name: string,
  tenantName: string,
  propName: string,
  tenantMobile: string,
  complaintName: string,
) => {
  const F = "sendWhatsappGharNewCompTenant";

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

    const messageBody = {
      to: formattedMobile,
      template_id: 'brw83kk6tzv_newcomplaintowner',
      sender: "918169871676",
      sample: {
        bodyvar: [name, tenantName, propName, tenantMobile, complaintName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharAssignStaff = async (
  mobile: string,
  staffName: string,
  propName: string,
  tenantName: string,
  tenantMobile: string,
  complaintName: string,
) => {
  const F = "sendWhatsappGharAssignStaff";

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

    const messageBody = {
      to: formattedMobile,
      template_id: 'ndhvxynhcra_complaintassignedstaff',
      sender: "918169871676",
      sample: {
        bodyvar: [staffName, propName, tenantName , tenantMobile, complaintName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharAssignTenant = async (
  mobile: string,
  tenantName: string,
  complaintName: string,
  staffName: string,
) => {
  const F = "sendWhatsappGharAssignTenant";

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

    const messageBody = {
      to: formattedMobile,
      template_id: 'kz3qjlhen3m_complaintassignedtenant',
      sender: "918169871676",
      sample: {
        bodyvar: [tenantName, complaintName, staffName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharResolvedTenant = async (
  mobile: string,
  tenantName: string,
  complaintName: string,
) => {
  const F = "sendWhatsappGharResolvedTenant";

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

    const messageBody = {
      to: formattedMobile,
      template_id: '8fczmclwaf9_complaintresolvedtenant',
      sender: "918169871676",
      sample: {
        bodyvar: [tenantName, complaintName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharResolvedOwner = async (
  mobile: string,
  ownerName: string,
  complaintName: string,
  tenantName: string,
) => {
  const F = "sendWhatsappGharResolvedOwner";

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

    const messageBody = {
      to: formattedMobile,
      template_id: '323_u6c2yfs_complaintresolvedowner',
      sender: "918169871676",
      sample: {
        bodyvar: [ownerName, complaintName, tenantName],
      }
    };
    
    const response: any = await axios.post(
      `${process.env.WHATSAPP_GHAR_SEND_MESSAGE_URI}`,
      messageBody,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );

    if (true == response?.data?.success) {
      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 sendWhatsappGharMumbai;
