const path = require("path");
const fs = require("fs");

const PLUGIN_ROOT = path.resolve(__dirname, "..");
const PLUGIN_DATA = path.join(PLUGIN_ROOT, "data");
const DIRS = [
  "config", "models", "runtime", "logs", "metrics", "rag", "repo_index", "cache", "tmp",
  "diagnostics", "feedback", "corrections", "evals", "exports", "tools"
];

function ensureDataDirs() {
  for (const dir of DIRS) fs.mkdirSync(path.join(PLUGIN_DATA, dir), { recursive: true });
}

function resolveData(...parts) {
  const target = path.resolve(PLUGIN_DATA, ...parts);
  if (target !== PLUGIN_DATA && !target.startsWith(`${PLUGIN_DATA}${path.sep}`)) {
    throw new Error("Path escapes Lumi AI plugin storage.");
  }
  return target;
}

module.exports = { PLUGIN_ROOT, PLUGIN_DATA, ensureDataDirs, resolveData };
