/**
 * Mutable on-disk state root for a coordinator process.
 * Allows two processes on one host (e.g. :9334 + :9335) without sharing block_store.
 *
 * Env: SARMAD_DATA_DIR (default = this package directory / __dirname of caller via defaultDir)
 */
const fs = require("fs");
const path = require("path");

function resolveDataDir(defaultDir) {
  const raw = String(process.env.SARMAD_DATA_DIR || "").trim();
  const base = raw ? path.resolve(raw) : path.resolve(defaultDir || __dirname);
  try {
    fs.mkdirSync(base, { recursive: true });
  } catch (_) {}
  return base;
}

/** Default for modules that live next to server.js */
const DATA_DIR = resolveDataDir(__dirname);

function dataPath(...parts) {
  return path.join(DATA_DIR, ...parts);
}

module.exports = {
  DATA_DIR,
  resolveDataDir,
  dataPath,
};
