import { BatchTransport } from "./transport.js";
import { _setTransport, _getTransport } from "./_registry.js";

export interface InitOptions {
  apiKey: string;
  endpoint?: string;
  maxBatchSize?: number;
  flushInterval?: number;
}

export function init(options: InitOptions): void {
  const existing = _getTransport();
  if (existing) {
    void existing.shutdown();
  }
  _setTransport(
    new BatchTransport({
      apiKey: options.apiKey,
      endpoint: options.endpoint ?? "https://agentlens.vectry.tech",
      maxBatchSize: options.maxBatchSize,
      flushInterval: options.flushInterval,
    }),
  );
}

export async function shutdown(): Promise<void> {
  const transport = _getTransport();
  if (transport) {
    await transport.shutdown();
    _setTransport(null);
  }
}

export function getClient(): BatchTransport | null {
  return _getTransport();
}

export async function flush(): Promise<void> {
  const transport = _getTransport();
  if (transport) {
    await transport.flush();
  }
}

export {
  TraceStatus,
  DecisionType,
  SpanType,
  SpanStatus,
  EventType,
  generateId,
  nowISO,
} from "./models.js";

export type {
  JsonValue,
  DecisionPointPayload,
  SpanPayload,
  EventPayload,
  TracePayload,
} from "./models.js";

export { BatchTransport } from "./transport.js";
export type { BatchTransportOptions } from "./transport.js";

export { TraceBuilder } from "./trace.js";
export type {
  TraceBuilderOptions,
  AddSpanInput,
  AddDecisionInput,
  AddEventInput,
  EndOptions,
} from "./trace.js";

export { createDecision } from "./decision.js";
export type { CreateDecisionInput } from "./decision.js";
