const os = require("os");
const crypto = require("crypto");
const { machineIdSync } = require("node-machine-id");
const logger = require("../handlers/logger");

function generateSystemFingerprint() {
  try {
    const machineId = machineIdSync({ original: true }); // Stronger binding to hardware
    const hostname = os.hostname();
    const cpus = os
      .cpus()
      .map((cpu) => cpu.model)
      .join("");
    const totalMemory = os.totalmem();

    const rawData = `${machineId}-${hostname}-${cpus}-${totalMemory}`;
    const fingerprint = crypto
      .createHash("sha256")
      .update(rawData)
      .digest("hex");

    return fingerprint;
  } catch (error) {
    logger.error("Failed to generate system fingerprint:", error);
    return "unable to generate system fingerprint";
  }
}

module.exports = { generateSystemFingerprint };
