import log from "../config/log";
import moment from "moment";
import getDueDescription from "./getDueDescription";
import crypto from "crypto";
import axios from "axios";
import CONSTANTS from "../config/constants";
import getClientEaseBuzzCredentials from "./getClientEaseBuzzCredentials";

const easeBuzzPaymentLink = async ({
  C,
  F,
  landlordId,
  landLordName,
  landLordMobile,
  landlordMID,
  propName,
  propGID,
  propId,
  type,
  clientId,
  clientName,
  clientMobile,
  email,
  amount,
  remark,
  expenseId

}: any) => {
  const U = "easeBuzzPaymentLink";
  try {
    //Type Rent or security
    let key = process.env.EASEBUZZ_KEY;
    let salt = process.env.EASEBUZZ_SALT;
    const easebuzzCreds = await getClientEaseBuzzCredentials(
      Number(clientId),
      0,
    );
    if (easebuzzCreds) {
      key = easebuzzCreds.key;
      salt = easebuzzCreds.salt;
    }
    const udf1 = `${landLordName}_${propName}`;
    const udf2 = landLordMobile;
    const udf3 = `${propGID}_${landlordId}`;
    const udf4 = remark;
    const udf5 = `${'LANDLORDPAYMENT'}_${type}_${expenseId}`;
    const txnId = Date.now() + clientId;
    const firstName = clientName;
    const phone = clientMobile;
    let productInfo = `Payment for `;
    if(Number(type) == CONSTANTS.DUES_TYPES.RENT) {
      productInfo += "rent";
    } else {
      productInfo += "security deposit";      
    }

    const hashString = `${key}|${txnId}|${firstName}|${email}|${phone}|${amount}|${udf1}|${udf2}|${udf3}|${udf4}|${udf5}|${productInfo}|${salt}`;
    
    const hash = crypto.createHash("sha512").update(hashString).digest("hex");

    log.info(
      `[${C}], [${F}], [${U}], Client Id [${clientId}], Prop Id [${propId}], Landlord Id [${landlordId}], Email [${email}], Amount [${amount}], Hash String [${hashString}], UDF1(LandLord Name) [${udf1}], UDF2(Landlord Mobile) [${udf2}], UDF3(propertyGid_PayerId(clientId)) [${udf3}], UDF4(PROP ID) [${udf4}], UDF5(TRANSDESC_TYPE_EXPENSEID) [${udf5}], Transaction Id [${txnId}],  Client Name [${firstName}], Client Mobile [${phone}], Amount [${amount}], Product Info [${productInfo}], Remark [${remark}], Client Requesting....`
    );

    //show_payment_mode
    const options = {
      method: 'POST',
      url: process.env.EASEBUZZ_WEB_CHECKOUT,
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json'
      },
      data: {
        merchant_txn: txnId,
        key: key,
        email: email,
        name: firstName,
        amount: amount,
        phone: phone,
        udf1: udf1,
        udf2: udf2,
        udf3: udf3,
        udf4: udf4,
        udf5: udf5,
        message: productInfo,
        expiry_date: moment().add(2, 'days').format('DD-MM-YYYY'),
        operation: [
          { type: 'sms', template: 'Default sms template' },
        ],
        sub_merchant_id: landlordMID,
        hash: hash
      }
    };
    const { data } = await axios.request(options);
    log.info(`[${C}], [${F}], [${U}], Status [${data?.status}], Payment Url [${data?.data?.payment_url}], Payment url shared`);
    return data;
  } catch (error: any) {
    log.info(`[${C}], [${F}], [${U}], Error while adding Transaction ${error?.message || error}`);

    return false;
  }
};



export default easeBuzzPaymentLink;
