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

const root = resolve(new URL("..", import.meta.url).pathname);

function run(label, command, args) {
  const result = spawnSync(command, args, {
    cwd: root,
    encoding: "utf8",
  });

  process.stdout.write(`\n## ${label}\n`);
  if (result.stdout) process.stdout.write(result.stdout);
  if (result.stderr) process.stdout.write(result.stderr);

  if (result.status !== 0) {
    process.stderr.write(`${label} failed with status ${result.status}\n`);
    process.exit(result.status ?? 1);
  }
}

run("CLI dry-run", process.execPath, ["bin/lazyclaude-ai.js", "--dry-run", "doctor"]);
run("Plugin validation", process.execPath, ["scripts/validate-plugin.mjs"]);
run("LSP doctor", process.execPath, ["plugins/lazyclaude/bin/lazyclaude-lsp-doctor.js"]);

process.stdout.write("\nDOCTOR_PASS\n");
