import type { Metrics, MetricTags } from "@/metrics/types.js";

export type MetricCall = {
  kind: "increment" | "count" | "gauge" | "histogram" | "timing";
  name: string;
  value?: number;
  tags?: MetricTags;
};

export class FakeMetrics implements Metrics {
  readonly calls: MetricCall[] = [];

  increment(name: string, tags?: MetricTags): void {
    this.calls.push({ kind: "increment", name, tags });
  }
  count(name: string, value: number, tags?: MetricTags): void {
    this.calls.push({ kind: "count", name, value, tags });
  }
  gauge(name: string, value: number, tags?: MetricTags): void {
    this.calls.push({ kind: "gauge", name, value, tags });
  }
  histogram(name: string, value: number, tags?: MetricTags): void {
    this.calls.push({ kind: "histogram", name, value, tags });
  }
  timing(name: string, ms: number, tags?: MetricTags): void {
    this.calls.push({ kind: "timing", name, value: ms, tags });
  }

  countOf(name: string): number {
    return this.calls.filter((c) => c.name === name).length;
  }
  last(name: string): MetricCall | undefined {
    return [...this.calls].reverse().find((c) => c.name === name);
  }
  tagsOf(name: string): Array<MetricTags | undefined> {
    return this.calls.filter((c) => c.name === name).map((c) => c.tags);
  }
}
