/**
 * Version utilities.
 * Reads package.json at runtime so CLI + API stay in sync.
 */

import { readFileSync } from 'fs';
import { join } from 'path';
import { getPackageRoot } from '../paths';

let cachedVersion: string | null = null;

export function getVersion(): string {
  if (cachedVersion) return cachedVersion;
  try {
    const pkgPath = join(getPackageRoot(), 'package.json');
    const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) as { version?: string };
    cachedVersion = pkg.version || '0.0.0';
  } catch {
    cachedVersion = '0.0.0';
  }
  return cachedVersion;
}

export const VERSION = getVersion();
