import { create } from "zustand";

export interface MetricsState {
  cost: number | null;
  max_budget_per_task: number | null;
  usage: {
    prompt_tokens: number;
    completion_tokens: number;
    cache_read_tokens: number;
    cache_write_tokens: number;
    context_window: number;
    per_turn_token: number;
  } | null;
}

export interface MetricsStore extends MetricsState {
  setMetrics: (metrics: MetricsState) => void;
  resetMetrics: () => void;
}

const EMPTY_METRICS: MetricsState = {
  cost: null,
  max_budget_per_task: null,
  usage: null,
};

const useMetricsStore = create<MetricsStore>((set) => ({
  ...EMPTY_METRICS,
  setMetrics: (metrics) => set(metrics),
  resetMetrics: () => set(EMPTY_METRICS),
}));

// Dev-only console handle for inspecting the live WS metrics while
// debugging the usage meter (window.__OH_METRICS_STORE__.getState()).
// Gated on import.meta.env.DEV so it never reaches production builds.
if (typeof window !== "undefined" && import.meta.env.DEV) {
  (
    window as unknown as { __OH_METRICS_STORE__?: typeof useMetricsStore }
  ).__OH_METRICS_STORE__ = useMetricsStore;
}

export default useMetricsStore;
