/**
 * Parse vendor status code from various possible locations in the error object.
 * @param err
 * @returns {number|null}
 */
const parseVendorStatusCode = (err) => {
  // Prefer explicit numeric props when available.
  const codeProp = err?.code ?? err?.statusCode ?? err?.meta?.status?.StatusCode ?? err?.status?.StatusCode;
  const num = Number(codeProp);
  if (!Number.isNaN(num) && num > 0) return num;

  // Fallback: parse from message text (e.g., "... 507 CANNOT EXCEED ...")
  const m = String(err?.message || "").match(/\b(\d{3})\b/);
  return m ? Number(m[1]) : null;
};

/**
 * Classify RR vendor errors into a small set of stable codes/messages for the FE.
 * @param {any} err
 * @returns {{vendorStatusCode:number|null, errorCode:string, title:string, friendlyMessage:string, severity:'info'|'warning'|'error', canRetry:boolean}}
 */
const classifyRRVendorError = (err) => {
  const code = parseVendorStatusCode(err);
  const rawMsg = String(err?.meta?.status?.Message || err?.status?.Message || err?.message || "").toUpperCase();

  // Open RO cap exceeded
  if (code === 507 || /MAXIMUM NUMBER OF ROS/.test(rawMsg)) {
    return {
      vendorStatusCode: 507,
      errorCode: "RR_MAX_OPEN_ROS",
      title: "Customer Open RO limit reached",
      friendlyMessage:
        "Reynolds has reached the maximum number of open Repair Orders for this Customer. Close or finalize an RO in Reynolds, then try again.",
      severity: "warning",
      canRetry: true
    };
  }

  // Default fallback
  return {
    vendorStatusCode: code ?? null,
    errorCode: "RR_VENDOR_ERROR",
    title: "Reynolds request failed",
    friendlyMessage:
      "We couldn’t complete the request with Reynolds. Please try again or contact support if this persists.",
    severity: "error",
    canRetry: true
  };
};

module.exports = { classifyRRVendorError };
