"""SLA threshold checking for Trust Tax metrics."""

from __future__ import annotations

from dataclasses import dataclass

from axm.logging import get_logger
from axm_audit.auditor import AuditMetrics

logger = get_logger("axm-audit")


@dataclass
class SLAConfig:
    """Configuration for SLA thresholds.

    Attributes:
        max_trust_tax_ratio: Maximum allowed validation overhead (0.0-1.0).
    """

    max_trust_tax_ratio: float = 0.01  # 1% default


@dataclass
class SLAResult:
    """Result of an SLA check.

    Attributes:
        passed: Whether the SLA was met.
        actual_ratio: The actual trust tax ratio.
        threshold: The configured threshold.
    """

    passed: bool
    actual_ratio: float
    threshold: float


def check_sla(
    metrics: AuditMetrics,
    execution_ns: int,
    config: SLAConfig,
) -> SLAResult:
    """Check if metrics meet SLA thresholds.

    Args:
        metrics: The audit metrics to check.
        execution_ns: Total execution time in nanoseconds.
        config: SLA configuration with thresholds.

    Returns:
        SLAResult with pass/fail status and actual ratio.
    """
    if execution_ns <= 0:
        return SLAResult(
            passed=True, actual_ratio=0.0, threshold=config.max_trust_tax_ratio
        )

    actual_ratio = metrics.total_duration_ns / execution_ns
    passed = actual_ratio <= config.max_trust_tax_ratio

    if not passed:
        logger.warning(
            f"SLA violated: Trust Tax {actual_ratio:.2%} "
            f"> {config.max_trust_tax_ratio:.2%}"
        )

    return SLAResult(
        passed=passed,
        actual_ratio=actual_ratio,
        threshold=config.max_trust_tax_ratio,
    )
