// 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 `${String(yyyy).slice(2)}${mm}${dd}${rand}`;
//   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": getTraceID(),
//     "BD-Timestamp": generateTimestamp(),
//     // If your provider needs client id in header, uncomment:
//     "BD-Clientid": process.env.BBPS_CLIENT_ID,
//   };
  
//   return { headers, body: payload || {} };
//   //return { headers, body};//body: JSON.stringify(payload || {}) };
// }

// module.exports = { processRequest };

// utils/apirequestmodule.js

// utils/apirequestmodule.js
// Generates trace ID, timestamp, and headers for BBPS requests

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}`;
}

/**
 * processRequest(payload, opts)
 * payload: string | object
 * opts: { lineEndings: 'preserve' | 'lf' | 'crlf' }
 */
function processRequest(payload, opts = {}) {
  const traceId = getTraceID();
  const timestamp = generateTimestamp();

  let body = typeof payload === "string" ? payload : JSON.stringify(payload || {});
  if (opts.lineEndings === "lf") {
    body = body.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
  } else if (opts.lineEndings === "crlf") {
    body = body.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n");
  }

  const headers = {
    "Content-Type": process.env.BBPS_CONTENT_TYPE || "application/jose",
    "Accept": process.env.BBPS_ACCEPT || "application/jose",
    "BD-Traceid": traceId,
    "BD-Timestamp": timestamp,
  };

  if (process.env.BBPS_CLIENT_ID) headers["BD-Clientid"] = process.env.BBPS_CLIENT_ID;

  return { headers, body, traceId, timestamp };
}

module.exports = { processRequest };
