// utils/httpClient.js
const axios = require("axios");

const DEFAULT_TIMEOUT = Number(process.env.HTTP_TIMEOUT_MS) || 30000;

async function post(url, data, opts = {}) {
  const timeout = opts.timeout || DEFAULT_TIMEOUT;
  const headers = opts.headers || {};
  const validateStatus = opts.validateStatus || (() => true);

  try {
    const resp = await axios.post(url, data, { headers, timeout, validateStatus });
    return {
      ok: resp.status >= 200 && resp.status < 300,
      status: resp.status,
      data: resp.data,
    };
  } catch (err) {
    return {
      ok: false,
      status: err.response ? err.response.status : 0,
      error: err.message,
      data: err.response?.data,
    };
  }
}

module.exports = { post };
