"""Thread-safe MetricsRegistry for pipeline-level metrics."""

from __future__ import annotations

import threading

from axm_audit.auditor import AuditMetrics


class MetricsRegistry:
    """Thread-safe registry for named metrics instances.

    Allows multiple pipelines to track metrics independently.

    Example:
        >>> registry = MetricsRegistry()
        >>> m1 = registry.get("pipeline_daily")
        >>> m2 = registry.get("pipeline_hourly")
    """

    def __init__(self) -> None:
        """Initialize empty registry with lock."""
        self._lock = threading.Lock()
        self._metrics: dict[str, AuditMetrics] = {}

    def get(self, name: str) -> AuditMetrics:
        """Get or create metrics for the given name.

        Args:
            name: Unique identifier for the metrics instance.

        Returns:
            AuditMetrics instance for that name.
        """
        with self._lock:
            if name not in self._metrics:
                self._metrics[name] = AuditMetrics()
            return self._metrics[name]

    def reset(self, name: str) -> None:
        """Reset metrics for the given name.

        Args:
            name: Name of the metrics to reset.
        """
        with self._lock:
            if name in self._metrics:
                self._metrics[name] = AuditMetrics()

    def all(self) -> dict[str, AuditMetrics]:
        """Get all metrics instances.

        Returns:
            Dictionary of name -> AuditMetrics.
        """
        with self._lock:
            return dict(self._metrics)
