"""ScoringWeights — validated immutable weight set for the scoring engine.

Enforces that all five weights are non-negative and sum to exactly 100.
"""

# Enable future language features for compatibility
from __future__ import annotations

# Import dataclass from dataclasses
from dataclasses import dataclass


# Apply the @dataclass decorator
@dataclass(frozen=True, slots=True)
# Define class ScoringWeights
class ScoringWeights:
    """Five scoring dimension weights that must sum to 100."""

    # tests: float
    tests: float
    # detection_rules: float
    detection_rules: float
    # d3fend: float
    d3fend: float
    # recency: float
    recency: float
    # severity: float
    severity: float

    # Define function __post_init__
    def __post_init__(self) -> None:
        """Validate that all weights are non-negative and sum to exactly 100.

        Returns:
            None
        """
        # Assign fields = [
        fields = [
            self.tests,
            self.detection_rules,
            self.d3fend,
            self.recency,
            self.severity,
        ]
        # Iterate over fields
        for f in fields:
            # Check: f < 0
            if f < 0:
                # Raise ValueError
                raise ValueError("Scoring weights must be non-negative")

        # Assign total = sum(fields)
        total = sum(fields)
        # Check: abs(total - 100) > 0.01
        if abs(total - 100) > 0.01:
            # Raise ValueError
            raise ValueError(
                f"Scoring weights must sum to 100, got {total}"
            )

    # Apply the @classmethod decorator
    @classmethod
    # Define function default
    def default(cls) -> ScoringWeights:
        """Return the default weight distribution.

        Returns:
            ScoringWeights: A weight set with tests=40, detection_rules=25,
                d3fend=15, recency=10, severity=10.
        """
        # Return cls(
        return cls(
            # Keyword argument: tests
            tests=40.0,
            # Keyword argument: detection_rules
            detection_rules=25.0,
            # Keyword argument: d3fend
            d3fend=15.0,
            # Keyword argument: recency
            recency=10.0,
            # Keyword argument: severity
            severity=10.0,
        )

    # Backward-compatible aliases for older API payloads
    @property
    # Define function freshness
    def freshness(self) -> float:
        """Return the recency weight (backward-compatible alias).

        Returns:
            float: The value of the ``recency`` weight.
        """
        # Return self.recency
        return self.recency

    # Apply the @property decorator
    @property
    # Define function platform_diversity
    def platform_diversity(self) -> float:
        """Return the severity weight (backward-compatible alias).

        Returns:
            float: The value of the ``severity`` weight.
        """
        # Return self.severity
        return self.severity
