// billdeskpg/utils/apirequestmodule.js
function pad2(n) { return String(n).padStart(2, "0"); }

function getTraceID() {
  const d = new Date();
  const dd = pad2(d.getDate());
  const mm = pad2(d.getMonth() + 1);
  const yy = String(d.getFullYear()).substring(2, 4);
  const rand = Math.floor(Math.random() * 10000000000) + 1;
  return yy + mm + dd + rand;
}

function generateTimestamp() {
  const d = new Date();
  const dd = pad2(d.getDate());
  const mm = pad2(d.getMonth() + 1);
  const yyyy = d.getFullYear();
  const hh = pad2(d.getHours());
  const mi = pad2(d.getMinutes());
  const ss = pad2(d.getSeconds());
  return `${yyyy}${mm}${dd}${hh}${mi}${ss}`;
}

function processRequest(payload) {
  const traceId = getTraceID();
  const timestamp = generateTimestamp();

  console.log("✔ Trace ID:", traceId);
  console.log("✔ Timestamp:", timestamp);
  const headers = {
    "Content-Type": process.env.BBPS_CONTENT_TYPE || "application/jose",
    "Accept": process.env.BBPS_ACCEPT || "application/jose",
    "BD-Traceid": traceId,
    "BD-Timestamp": timestamp,
    "BD-Clientid": process.env.BD_CLIENT_ID || process.env.BD_MERCHANT_ID
  };

  return { headers, body: payload || {} };
}

module.exports = { processRequest };
