import axios from "axios";
import log from "../config/log";
const { stringify, parse } = require("flatted");

const eqaroAPIs: any = {};

eqaroAPIs.getTokenApi = async (mobile: string, path: string, body: any) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
      },
    };

    let response = await axios.post(
      process.env.EQARO_BASE_URL + path,
      body,
      axiosHeaders
    );

    log.info(
      `[Eqaro getTokenApi], Mobile [${mobile}], Path [${path}], Response ${JSON.stringify(
        response.data
      )}`
    );

    //if (data.includes("-100")) return true;
    //else return false;
    return response.data;
  } catch (error: any) {
    if (error.response) {
      // Server responded with a status code outside 2xx
      log.info(
        `[Eqaro getTokenApi], Mobile [${mobile}], Path [${path}], Status [${error.response.status}]`
      );
      log.info(
        `[Eqaro getTokenApi], Mobile [${mobile}], Path [${path}], Error ${
          JSON.stringify(error?.response?.data) || error?.message || error
        }`
      );
    } else if (error.request) {
      // No response was received
      log.info(`[Eqaro getTokenApi], Mobile [${mobile}], Path [${path}], No Response`);
    } else {
      // Something else went wrong in setting up the request
      log.info(`[Eqaro getTokenApi], Mobile [${mobile}], Path [${path}], Issue in request`);
    }

    return false;
  }
};

eqaroAPIs.registerTenantApi = async (
  mobile: string,
  path: string,
  body: any
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${process.env.EQARO_PARTNER_TOKEN}`,
      },
    };

    let response = await axios.post(
      process.env.EQARO_BASE_URL + path,
      body,
      axiosHeaders
    );

    log.info(
      `[Eqaro registerTenantApi], Mobile [${mobile}], Path [${path}], Response ${JSON.stringify(
        response.data
      )}`
    );

    //if (data.includes("-100")) return true;
    //else return false;
    return response.data;
  } catch (error: any) {
    if (error.response) {
      // Server responded with a status code outside 2xx
      log.info(
        `[Eqaro registerTenantApi], Mobile [${mobile}], Path [${path}], Status [${error.response.status}]`
      );
      log.info(
        `[Eqaro registerTenantApi], Mobile [${mobile}], Path [${path}], Error ${
          JSON.stringify(error?.response?.data) || error?.message || error
        }`
      );
    } else if (error.request) {
      // No response was received
      log.info(`[Eqaro registerTenantApi], Mobile [${mobile}], No Response`);
    } else {
      // Something else went wrong in setting up the request
      log.info(
        `[Eqaro registerTenantApi], Mobile [${mobile}], Issue in request`
      );
    }

    return false;
  }
};

eqaroAPIs.postApi = async (
  mobile: string,
  token: string,
  path: string,
  body: any
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.post(
      process.env.EQARO_BASE_URL + path,
      body,
      axiosHeaders
    );

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro postApi], Mobile [${mobile}], Path [${path}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );

    return data;
    //if (data.includes("-100")) return true;
    //else return false;
  } catch (error: any) {
    log.info(
      `[Eqaro postApi], Mobile [${mobile}], Path [${path}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

eqaroAPIs.isUserCheckpostApi = async (
  mobile: string,
  token: string,
  path: string,
  body: any
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.post(
      process.env.EQARO_BASE_URL + path,
      body,
      axiosHeaders
    );

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro isUserCheckpostApi], Mobile [${mobile}], Path [${path}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );

    return data;
    //if (data.includes("-100")) return true;
    //else return false;
  } catch (error: any) {
    log.info(
      `[Eqaro isUserCheckpostApi], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return error?.response || false;
  }
};

eqaroAPIs.getApi = async (mobile: string, token: string, path: string) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.get(process.env.EQARO_BASE_URL + path, axiosHeaders);

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro getApi], Mobile [${mobile}], Path [${path}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );
    return { isSuccess: true, response: data };
  } catch (error: any) {
    log.info(
      `[Eqaro getApi], Mobile [${mobile}], Path [${path}], Error ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return {
      isSuccess: false,
      response: error?.response?.data || error?.message || error,
    };
  }
};

eqaroAPIs.patchApi = async (
  mobile: string,
  token: string,
  path: string,
  body: any
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.patch(
      process.env.EQARO_BASE_URL + path,
      body,
      axiosHeaders
    );

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro patchApi], Mobile [${mobile}], Path [${path}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );

    return data;
    //if (data.includes("-100")) return true;
    //else return false;
  } catch (error: any) {
    log.info(
      `[Eqaro patchApi], Mobile [${mobile}], Path [${path}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    log.info(
      `[Eqaro patchApi], Mobile [${mobile}], Path [${path}], Error Request ${JSON.stringify(
        error?.request
      )}`
    );

    return false;
  }
};

eqaroAPIs.paymentPostApi = async (
  mobile: string,
  token: string,
  path: string,
  body: any
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.post(
      process.env.EQARO_PAYMENT_BASE_URL + path,
      body,
      axiosHeaders
    );

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro paymentPostApi], Mobile [${mobile}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );

    //return data;
    return { isSuccess: true, response: data };
  } catch (error: any) {
    log.info(
      `[Eqaro paymentPostApi], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );
    return {
      isSuccess: false,
      response: error?.response?.data || error?.message || error,
    };
    //return false;
  }
};

eqaroAPIs.paymentGetApi = async (
  mobile: string,
  token: string,
  path: string
) => {
  try {
    const axiosHeaders = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.get(
      process.env.EQARO_PAYMENT_BASE_URL + path,
      axiosHeaders
    );

    const serialized = stringify(data);
    const parsedResponse = parse(serialized);

    log.info(
      `[Eqaro paymentGetApi], Mobile [${mobile}], Path [${path}], Response [${JSON.stringify(
        parsedResponse.data,
        null,
        2
      )}]`
    );
    return data;
    //if (data.includes("-100")) return true;
    //else return false;
  } catch (error: any) {
    log.info(
      `[Eqaro paymentGetApi], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

eqaroAPIs.bondGetApi = async (mobile: string, token: string, path: string) => {
  try {
    const axiosHeaders = {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    };

    let data = await axios.get(process.env.EQARO_PAYMENT_BASE_URL + path, {
      ...axiosHeaders,
      responseType: "arraybuffer",
    });

    return data;
  } catch (error: any) {
    log.info(
      `[Eqaro bondGetApi], Mobile [${mobile}], Error: ${
        JSON.stringify(error?.response?.data) || error?.message || error
      }`
    );

    return false;
  }
};

export default eqaroAPIs;
