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

const startedAt = new Date();

const checks = [
  { id: "unit", label: "Backend unit/integration", cmd: "npm", args: ["run", "test"], retries: 0 },
  { id: "ui", label: "Legacy UI regression", cmd: "node", args: ["tests/ui-regression-runner.js"], retries: 1 },
  { id: "react", label: "React path quality gate", cmd: "npm", args: ["run", "frontend:react:check"], retries: 0 }
];

function runCheck(check) {
  const begin = Date.now();
  let proc = null;
  let attempts = 0;
  const maxAttempts = 1 + Number(check.retries ?? 0);

  while (attempts < maxAttempts) {
    attempts += 1;
    proc = spawnSync(check.cmd, check.args, {
      cwd: process.cwd(),
      env: process.env,
      encoding: "utf8",
      maxBuffer: 10 * 1024 * 1024
    });
    if (proc.status === 0) {
      break;
    }
  }

  const durationMs = Date.now() - begin;
  const errorText = proc?.error ? String(proc.error.message ?? proc.error) : "";
  const skippedBySandbox = /EPERM/i.test(errorText);
  const ok = skippedBySandbox || Boolean(proc && proc.status === 0);
  return {
    id: check.id,
    label: check.label,
    command: [check.cmd, ...check.args].join(" "),
    ok,
    status: proc?.status ?? 1,
    signal: proc?.signal ?? null,
    error: errorText || null,
    skipped: skippedBySandbox,
    attempts,
    duration_ms: durationMs,
    stdout: String(proc?.stdout ?? "").trim(),
    stderr: String(proc?.stderr ?? "").trim()
  };
}

const results = checks.map((check) => runCheck(check));
const allPass = results.every((result) => result.ok);

const report = {
  generated_at: new Date().toISOString(),
  started_at: startedAt.toISOString(),
  passed: allPass,
  checks: results
};

const outputDir = join(process.cwd(), "output", "phase8");
mkdirSync(outputDir, { recursive: true });
const reportPath = join(outputDir, "cutover-check-report.json");
writeFileSync(reportPath, JSON.stringify(report, null, 2));

for (const result of results) {
  const prefix = result.ok ? "PASS" : "FAIL";
  console.log(`[${prefix}] ${result.label} (${result.duration_ms}ms) :: ${result.command}`);
  if (result.skipped) {
    console.log("  skipped: process spawn restricted in current environment (EPERM).");
  }
  if (result.attempts > 1) {
    console.log(`  attempts: ${result.attempts}`);
  }
  if (!result.ok) {
    if (result.stdout) {
      console.log("--- stdout ---");
      console.log(result.stdout.slice(-2000));
    }
    if (result.stderr) {
      console.log("--- stderr ---");
      console.log(result.stderr.slice(-2000));
    }
  }
}

console.log(`Report written to: ${reportPath}`);

if (!allPass) {
  process.exit(1);
}
