"""Settings helpers for ContextTierService.

Extracted from ``context_tiers.py`` to keep that module under the
500-line guideline.  Provides budget and runtime-policy helpers that
read from application settings (or fall back to defaults).

Based on ``docs/specification.md`` ACMS tier sections and issue #7547.
"""

from __future__ import annotations

from datetime import timedelta

from cleveragents.config.settings import Settings
from cleveragents.domain.models.acms.tiers import TierBudget

# ---------------------------------------------------------------------------
# Default budget when settings are not provided
# ---------------------------------------------------------------------------

DEFAULT_MAX_TOKENS_HOT = 8000
DEFAULT_MAX_DECISIONS_WARM = 500
DEFAULT_MAX_DECISIONS_COLD = 5000

# ---------------------------------------------------------------------------
# Default runtime policy values
# ---------------------------------------------------------------------------

DEFAULT_PROMOTION_THRESHOLD = 5
DEFAULT_HOT_TTL_HOURS = 24
DEFAULT_WARM_TTL_HOURS = 24


def budget_from_settings(settings: Settings | None) -> TierBudget:
    """Build a ``TierBudget`` from application settings (or defaults)."""
    if settings is None:
        return TierBudget()
    return TierBudget(
        max_tokens_hot=getattr(
            settings, "context_max_tokens_hot", DEFAULT_MAX_TOKENS_HOT
        ),
        max_decisions_warm=getattr(
            settings, "context_max_decisions_warm", DEFAULT_MAX_DECISIONS_WARM
        ),
        max_decisions_cold=getattr(
            settings, "context_max_decisions_cold", DEFAULT_MAX_DECISIONS_COLD
        ),
    )


def promotion_threshold_from_settings(settings: Settings | None) -> int:
    """Read promotion threshold from settings (or use default)."""
    if settings is None:
        return DEFAULT_PROMOTION_THRESHOLD
    return int(
        getattr(
            settings,
            "context_tier_promotion_threshold",
            DEFAULT_PROMOTION_THRESHOLD,
        ),
    )


def hot_ttl_from_settings(settings: Settings | None) -> timedelta:
    """Read hot-tier TTL from settings (or use default)."""
    if settings is None:
        return timedelta(hours=DEFAULT_HOT_TTL_HOURS)
    hours = int(
        getattr(
            settings,
            "context_tier_hot_ttl_hours",
            DEFAULT_HOT_TTL_HOURS,
        ),
    )
    return timedelta(hours=hours)


def warm_ttl_from_settings(settings: Settings | None) -> timedelta:
    """Read warm-tier TTL from settings (or use default)."""
    if settings is None:
        return timedelta(hours=DEFAULT_WARM_TTL_HOURS)
    hours = int(
        getattr(
            settings,
            "context_tier_warm_ttl_hours",
            DEFAULT_WARM_TTL_HOURS,
        ),
    )
    return timedelta(hours=hours)


__all__ = [
    "DEFAULT_HOT_TTL_HOURS",
    "DEFAULT_MAX_DECISIONS_COLD",
    "DEFAULT_MAX_DECISIONS_WARM",
    "DEFAULT_MAX_TOKENS_HOT",
    "DEFAULT_PROMOTION_THRESHOLD",
    "DEFAULT_WARM_TTL_HOURS",
    "budget_from_settings",
    "hot_ttl_from_settings",
    "promotion_threshold_from_settings",
    "warm_ttl_from_settings",
]
