import path from "node:path";

export const TEMP_REPORT_ROOT = process.env.HWLAB_TEMP_REPORT_ROOT || "/tmp/hwlab-dev-gate";
const forbiddenRepoReportDir = "reports";

export function tempReportPath(...segments) {
  return path.join(TEMP_REPORT_ROOT, ...segments);
}

export function resolveOutputPath(root, outputPath) {
  return path.isAbsolute(outputPath) ? outputPath : path.resolve(root, outputPath);
}

export function ensureNotRepoReportsPath(root, outputPath, label = "report path") {
  const absoluteRoot = path.resolve(root);
  const absoluteOutput = resolveOutputPath(absoluteRoot, outputPath);
  const relative = path.relative(absoluteRoot, absoluteOutput).split(path.sep).join("/");
  if (relative === forbiddenRepoReportDir || relative.startsWith(`${forbiddenRepoReportDir}/`)) {
    throw new Error(`${label} must not write under the forbidden repository report directory; use ${TEMP_REPORT_ROOT} or .state instead`);
  }
  return absoluteOutput;
}
