import type { Logger } from "pino";
import { DatadogMetrics } from "./datadog-recorder.js";
import { NoopMetrics } from "./noop-recorder.js";
import type { Metrics } from "./types.js";

export type { Metrics, MetricTags } from "./types.js";
export { METRIC } from "./names.js";

export interface MetricsConfig {
  metricsEnabled: boolean;
  ddAgentHost: string;
  ddDogstatsdPort: number;
  ddEnv: string;
  /** Active agent provider (cursor | claude); emitted as a global tag on every metric. */
  provider: string;
}

export function createMetrics(config: MetricsConfig, logger: Logger): Metrics {
  if (!config.metricsEnabled) return new NoopMetrics();
  try {
    return new DatadogMetrics(
      config.ddAgentHost,
      config.ddDogstatsdPort,
      config.ddEnv,
      config.provider,
      logger,
    );
  } catch (err) {
    logger.warn(
      { error: err instanceof Error ? err.message : String(err) },
      "metrics_init_failed_using_noop",
    );
    return new NoopMetrics();
  }
}
