"""Tier system for JAE-AI agent — access control + model routing."""

TIERS = ['anonymous', 'operator', 'elite', 'admin']
TIER_RANK = {name: idx for idx, name in enumerate(TIERS)}

# Admin wallets — Solana base58 pubkeys. Populate when confirmed.
ADMIN_WALLETS = [
    '9NuiHh5wgRPx69BFGP1ZR8kHiBENGoJrXs5GpZzKAyn8',  # jaeswift.sol
]

# Model routing per tier
MODEL_ROUTING = {
    'anonymous': 'zai-org-glm-4.7-flash',
    'operator':  'zai-org-glm-4.7-flash',
    'elite':     'kimi-k2-thinking',
    'admin':     'kimi-k2-thinking',
}


def compute_tier(address: str | None, balance_sol: float = 0.0) -> str:
    """Derive tier from wallet address + SOL holdings."""
    if not address:
        return 'anonymous'
    if address in ADMIN_WALLETS:
        return 'admin'
    try:
        if float(balance_sol) >= 1.0:
            return 'elite'
    except (TypeError, ValueError):
        pass
    return 'operator'


def tier_rank(tier: str) -> int:
    return TIER_RANK.get(tier, 0)


def tier_allows(user_tier: str, required_tier: str) -> bool:
    """True when user_tier >= required_tier in the hierarchy."""
    return tier_rank(user_tier) >= tier_rank(required_tier)


def pick_model(tier: str) -> str:
    return MODEL_ROUTING.get(tier, MODEL_ROUTING['anonymous'])
