"""RLM Neo-Cortex -- Per-tier surprise threshold configuration.

Provides tier-aware threshold lookup so that surprise scoring
sensitivity can be adjusted per customer subscription tier without
requiring code changes.

Tier design:
    - Starter ($497/mo):      Most selective -- only stores clearly surprising content
    - Professional ($997/mo): Default thresholds -- balanced capture
    - Enterprise ($1,497/mo): More sensitive -- captures more memories for deeper recall
    - Queen ($20K+/mo):       Captures nearly everything for maximum intelligence

Each tier has three thresholds:
    - discard: below this score, memory is discarded entirely
    - working: above discard but below this, stored as short-term working memory
    - episodic: above working but below this, stored as medium-term episodic memory
    - (anything >= episodic is stored as long-term semantic memory / axiom candidate)
"""
from __future__ import annotations

from typing import Dict


# ---------------------------------------------------------------------------
# Tier threshold configuration
# ---------------------------------------------------------------------------

TIER_THRESHOLDS: Dict[str, Dict[str, float]] = {
    "starter": {"discard": 0.35, "working": 0.50, "episodic": 0.80},
    "professional": {"discard": 0.30, "working": 0.50, "episodic": 0.80},
    "enterprise": {"discard": 0.20, "working": 0.45, "episodic": 0.75},
    "queen": {"discard": 0.10, "working": 0.30, "episodic": 0.60},
}

# Default tier used for unknown tier names
DEFAULT_TIER = "professional"


def get_thresholds(tier: str) -> Dict[str, float]:
    """Get surprise thresholds for a given tier.

    Args:
        tier: Customer tier name (starter, professional, enterprise, queen).
              Case-insensitive. Falls back to professional defaults if unknown.

    Returns:
        Dict with keys: discard, working, episodic -- all float values in (0, 1).
    """
    return TIER_THRESHOLDS.get(tier.lower(), TIER_THRESHOLDS[DEFAULT_TIER])


def validate_thresholds() -> bool:
    """Validate that all tier thresholds are sane.

    Checks:
        1. All values between 0.0 and 1.0
        2. For each tier: discard < working < episodic (monotonically increasing)

    Returns:
        True if all validations pass.

    Raises:
        ValueError: If any threshold is invalid.
    """
    for tier_name, thresholds in TIER_THRESHOLDS.items():
        for key, value in thresholds.items():
            if not 0.0 <= value <= 1.0:
                raise ValueError(
                    f"Tier '{tier_name}' threshold '{key}' = {value} is outside [0, 1]"
                )
        if not (thresholds["discard"] < thresholds["working"] < thresholds["episodic"]):
            raise ValueError(
                f"Tier '{tier_name}' thresholds are not monotonically increasing: "
                f"discard={thresholds['discard']}, working={thresholds['working']}, "
                f"episodic={thresholds['episodic']}"
            )
    return True


# VERIFICATION_STAMP
# Story: 3.06
# Verified By: parallel-builder
# Verified At: 2026-02-26T06:15:00Z
# Tests: 7/7 (TestStory306Config)
# Coverage: 100%
