const WINDOWS_STATUS = {
  0xC0000005: ["STATUS_ACCESS_VIOLATION", "runtime_crash", ["Reinstall the runtime.", "Install or update Microsoft Visual C++ Redistributable x64.", "Unblock downloaded runtime files.", "Try a local disk path or alternate runtime build."]],
  0xC000001D: ["STATUS_ILLEGAL_INSTRUCTION", "cpu_incompatible", ["Use a baseline or more compatible CPU runtime build.", "Verify CPU AVX/AVX2 support."]],
  0xC0000135: ["STATUS_DLL_NOT_FOUND", "missing_dependency", ["Reinstall the runtime.", "Verify sibling DLL files.", "Install or update Microsoft Visual C++ Redistributable x64."]],
  0xC0000139: ["STATUS_ENTRYPOINT_NOT_FOUND", "dependency_mismatch", ["Delete and reinstall the complete runtime folder.", "Do not mix DLLs from different releases."]],
  0xC000007B: ["STATUS_INVALID_IMAGE_FORMAT", "architecture_mismatch", ["Download the runtime matching the detected OS and architecture.", "Reinstall and verify the archive hash."]],
  0xC0000142: ["STATUS_DLL_INIT_FAILED", "dependency_initialization_failed", ["Unblock runtime files.", "Install or update Microsoft Visual C++ Redistributable x64.", "Review security software restrictions."]],
  0xC0000409: ["STATUS_STACK_BUFFER_OVERRUN", "runtime_crash", ["Reinstall the runtime.", "Try an alternate runtime build.", "Inspect the captured runtime logs."]],
  0xC0000374: ["STATUS_HEAP_CORRUPTION", "runtime_crash", ["Reinstall the runtime.", "Try an alternate build.", "Reduce context size if failure occurs after model load."]]
};
const WINDOWS_LAUNCH = {
  2: ["ERROR_FILE_NOT_FOUND", "executable_missing"],
  3: ["ERROR_PATH_NOT_FOUND", "path_missing"],
  5: ["ERROR_ACCESS_DENIED", "permission_denied"],
  126: ["ERROR_MOD_NOT_FOUND", "missing_dependency"],
  127: ["ERROR_PROC_NOT_FOUND", "dependency_mismatch"],
  193: ["ERROR_BAD_EXE_FORMAT", "architecture_mismatch"],
  206: ["ERROR_FILENAME_EXCED_RANGE", "path_too_long"],
  740: ["ERROR_ELEVATION_REQUIRED", "permission_denied"],
  1114: ["ERROR_DLL_INIT_FAILED", "dependency_initialization_failed"],
  1455: ["ERROR_COMMITMENT_LIMIT", "insufficient_memory"]
};
const POSIX = {
  11: ["SIGSEGV", "runtime_crash"],
  4: ["SIGILL", "cpu_incompatible"],
  6: ["SIGABRT", "runtime_abort"],
  9: ["SIGKILL", "killed_or_oom"],
  15: ["SIGTERM", "terminated"]
};

function normalizeExitCode(code, signal, platform = process.platform) {
  if (platform === "win32" && Number.isInteger(code)) {
    const unsigned = code >>> 0;
    const signed = unsigned | 0;
    const known = WINDOWS_STATUS[unsigned];
    return {
      raw_exit_code: code,
      signed_exit_code: signed,
      unsigned_exit_code: unsigned,
      hex_exit_code: `0x${unsigned.toString(16).toUpperCase().padStart(8, "0")}`,
      code: known?.[0] || "WINDOWS_PROCESS_EXIT",
      category: known?.[1] || "runtime_exit",
      remediation_steps: known?.[2] || ["Inspect runtime stdout and stderr.", "Reinstall or try an alternate runtime build."]
    };
  }
  const signalNumber = typeof signal === "string" ? require("os").constants.signals[signal] : null;
  const number = signalNumber || (Number.isInteger(code) && code >= 128 ? code - 128 : Number.isInteger(code) && code < 0 ? -code : null);
  const known = number ? POSIX[number] : null;
  return {
    raw_exit_code: code,
    signed_exit_code: code,
    unsigned_exit_code: Number.isInteger(code) ? code >>> 0 : null,
    hex_exit_code: Number.isInteger(code) ? `0x${(code >>> 0).toString(16).toUpperCase().padStart(8, "0")}` : null,
    code: known?.[0] || signal || "PROCESS_EXIT",
    category: known?.[1] || "runtime_exit",
    remediation_steps: known ? ["Inspect runtime logs.", "Verify runtime compatibility and model settings."] : ["Inspect runtime stdout and stderr."]
  };
}

function classifyLaunchError(error, platform = process.platform) {
  const numeric = Number(error?.errno);
  const known = platform === "win32" ? WINDOWS_LAUNCH[numeric] : null;
  return {
    raw_exit_code: numeric || null,
    signed_exit_code: numeric || null,
    unsigned_exit_code: Number.isInteger(numeric) ? numeric >>> 0 : null,
    hex_exit_code: Number.isInteger(numeric) ? `0x${(numeric >>> 0).toString(16).toUpperCase().padStart(8, "0")}` : null,
    code: known?.[0] || error?.code || "PROCESS_LAUNCH_FAILED",
    category: known?.[1] || (/EACCES|EPERM/.test(error?.code) ? "permission_denied" : /ENOENT/.test(error?.code) ? "executable_missing" : "launch_failed"),
    remediation_steps: ["Verify the executable and working directory.", "Reinstall the runtime.", "Check file permissions and security software."]
  };
}

module.exports = { normalizeExitCode, classifyLaunchError };
