import { Attributes, Counter } from "@opentelemetry/api";
import { getMetric } from "../metrics.js";
import { PIISource } from "./pii-detection.js";

interface RedactionMetric extends Attributes {
  /** Type of PII redacted (e.g., "email", "phone"). */
  pii_type: string;
  /** Domain part of the redacted PII (e.g., "gmail.com"). */
  pii_email_domain?: string;
  /** Source of the redaction (trace, log or metric). */
  redaction_source: PIISource;
  /** Format or structure of the redacted value. */
  pii_format: "string" | "url";
}

// Cached singleton instance of the redaction counter metric
let _redactedCounter: undefined | Counter;

/**
 * Returns a singleton OpenTelemetry counter metric used to record occurrences of PII redactions.
 *
 * @returns {Counter} The singleton OpenTelemetry counter metric for PII redactions.
 */
export function _getPIICounterRedactionMetric() {
  if (_redactedCounter) {
    return _redactedCounter;
  }

  _redactedCounter = getMetric<"counter", RedactionMetric>("counter", {
    meterName: "o11y",
    metricName: "o11y_pii_redaction",
  });
  return _redactedCounter;
}
