// services/bbpsService.js
const axios = require("axios");

const callBBPSValidateAPI = async (payload) => {
  try {
    const response = await axios.post(process.env.BBPS_VALIDATE_URL, payload, {
      headers: { "Content-Type": "application/json" },
      timeout: 15000,
    });

    return {
      success: true,
      statusCode: response.status,
      data: response.data,
    };
  } catch (error) {
    if (error.response) {
      const { status, data } = error.response;

      const detail = data?.detail || {};
      return {
        success: false,
        statusCode: status,
        error: "BBPS API call failed",
        detail: {
          error_type: detail.error_type || "unknown_error",
          error_code: detail.error_code || "NA",
          message:
            detail.message ||
            data.message ||
            "Unknown error occurred during BBPS validation",
        },
      };
    } else {
      return {
        success: false,
        statusCode: 500,
        error: "Network or unexpected error",
        detail: {
          message: error.message,
        },
      };
    }
  }
};

module.exports = { callBBPSValidateAPI };
