const fs = require("fs");

function loadEnv() {
  const envPath = "/opt/peercortex-app/.env";
  try {
    const envContent = fs.readFileSync(envPath, "utf8");
    envContent.split("\n").forEach((line) => {
      const trimmed = line.trim();
      if (!trimmed || trimmed.startsWith("#")) return;
      const eqIdx = trimmed.indexOf("=");
      if (eqIdx > 0) {
        const key = trimmed.substring(0, eqIdx).trim();
        const val = trimmed.substring(eqIdx + 1).trim();
        if (!process.env[key]) process.env[key] = val;
      }
    });
    console.log('[Config] Environment variables loaded');
  } catch (_e) {
    console.warn("Warning: Could not read .env file at", envPath);
  }
}

// Automatically load environment variables when module is required
loadEnv();

module.exports = {
  loadEnv
};
