#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";

const reportDir = path.resolve(process.cwd(), process.env.LOAD_REPORT_DIR || "./reports");
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const reportFile = path.join(reportDir, `load-staging-${timestamp}.json`);

fs.mkdirSync(reportDir, { recursive: true });

const env = {
  ...process.env,
  LOAD_CALLBACKS: process.env.LOAD_CALLBACKS || "500",
  LOAD_HEARTBEATS: process.env.LOAD_HEARTBEATS || "1000",
  LOAD_DYNAMIC_QR: process.env.LOAD_DYNAMIC_QR || "500",
  LOAD_READS: process.env.LOAD_READS || "200",
  LOAD_EXPORTS: process.env.LOAD_EXPORTS || "10",
  LOAD_CONCURRENCY: process.env.LOAD_CONCURRENCY || "30",
  LOAD_REPORT_FILE: reportFile
};

const result = spawnSync("node", ["scripts/load-test.mjs"], {
  stdio: "inherit",
  env
});

if (result.status !== 0) {
  throw new Error(`staging load report failed with status ${result.status}`);
}

console.log(JSON.stringify({ ok: true, report_file: reportFile }, null, 2));
