#!/usr/bin/env python3

"""![status](../../../badges/status-application-validation.svg) Shared validation helpers"""

from __future__ import annotations

from collections import Counter
from collections.abc import Sequence

from evallm.application.errors import DuplicateMetricResultKeyError
from evallm.application.models import MetricSpec


def ensure_unique_metric_result_keys(metric_specs: Sequence[MetricSpec]) -> None:
    """Ensure that all metric specs resolve to unique final result keys.

    Args:
        metric_specs (Sequence[MetricSpec]): Metric specs to validate.

    Raises:
        DuplicateMetricResultKeyError: If duplicate final result keys are detected.
    """
    counts: Counter[str] = Counter(
        spec.result_key if spec.result_key is not None else spec.name
        for spec in metric_specs
    )
    if len(duplicates := [key for key, count in counts.items() if count > 1]) > 0:
        raise DuplicateMetricResultKeyError(
            duplicate_keys=duplicates,
            metric_names=[spec.name for spec in metric_specs],
        )
