#!/usr/bin/env node
import { installManagedNatsServer, readNatsServerVersion } from './lib/nats-server-tool.mjs';

const json = process.argv.includes('--json');

try {
  const result = await installManagedNatsServer();
  const version = readNatsServerVersion(result.path);
  const proof = {
    ok: true,
    tool: 'nats-server',
    installed: result.installed,
    source: result.source,
    path: result.path,
    version,
    url: result.url,
  };
  if (json) {
    console.log(JSON.stringify(proof, null, 2));
  } else {
    console.log(`nats-server ready: ${version}`);
    console.log(`path: ${result.path}`);
    console.log(`source: ${result.source}${result.installed ? ' (installed now)' : ''}`);
  }
} catch (error) {
  const message = error instanceof Error ? error.message : String(error);
  if (json) {
    console.log(JSON.stringify({ ok: false, tool: 'nats-server', error: message }, null, 2));
  } else {
    console.error(`nats-server setup failed: ${message}`);
  }
  process.exit(1);
}
