import * as fs from 'node:fs';
import * as path from 'node:path';

export interface RuntimeInfo {
  pid: number;
  ready: boolean;
  startedAt: string;
  port: number;
  url: string;
  controlBaseUrl: string;
  controlSecret: string;
  dataDir: string;
  logDir: string;
  configDir: string;
}

export function readRuntimeInfo(filePath: string): RuntimeInfo | null {
  if (!fs.existsSync(filePath)) return null;
  return JSON.parse(fs.readFileSync(filePath, 'utf8')) as RuntimeInfo;
}

export function writeRuntimeInfo(filePath: string, info: RuntimeInfo) {
  fs.mkdirSync(path.dirname(filePath), { recursive: true });
  fs.writeFileSync(filePath, JSON.stringify(info, null, 2), 'utf8');
}

export function clearRuntimeInfo(filePath: string) {
  fs.rmSync(filePath, { force: true });
}
