const path = require("path");

function boolFromEnv(name, fallback) {
  const raw = process.env[name];
  if (raw === undefined) return fallback;
  return ["1", "true", "yes", "on"].includes(raw.toLowerCase());
}

function numberFromEnv(name, fallback) {
  const raw = process.env[name];
  if (!raw) return fallback;
  const value = Number(raw);
  return Number.isFinite(value) ? value : fallback;
}

function pathFromEnv(name, fallback) {
  const raw = process.env[name] || fallback;
  if (path.isAbsolute(raw)) return raw;
  return path.join(__dirname, raw);
}

function getConfig() {
  return {
    dryRun: boolFromEnv("LIVE_CHECK_DRY_RUN", true),
    intervalSeconds: numberFromEnv("LIVE_CHECK_INTERVAL_SECONDS", 60),
    healthUrl: process.env.SMARTCOIN_HEALTH_URL || "http://127.0.0.1:8000/api/config",
    healthTimeoutMs: numberFromEnv("SMARTCOIN_HEALTH_TIMEOUT_MS", 5000),
    smileBalanceSource: process.env.SMILE_BALANCE_SOURCE || "env",
    smileBalance: numberFromEnv("SMILE_BALANCE", 100000),
    smileBalanceCurrency: process.env.SMILE_BALANCE_CURRENCY || "MMK",
    smileWarningBalance: numberFromEnv("SMILE_WARNING_BALANCE", 50000),
    smileCriticalBalance: numberFromEnv("SMILE_CRITICAL_BALANCE", 10000),
    queueStatusFile: pathFromEnv("QUEUE_STATUS_FILE", "./state/queue-status.json"),
    browserHeartbeatFile: pathFromEnv("BROWSER_HEARTBEAT_FILE", "./state/browser-heartbeat.json"),
    sessionStatusFile: pathFromEnv("SESSION_STATUS_FILE", "./state/session-status.json"),
    browserHeartbeatMaxAgeSeconds: numberFromEnv("BROWSER_HEARTBEAT_MAX_AGE_SECONDS", 120),
  };
}

module.exports = {
  getConfig,
};
