"""
Pricing Cache System

Caches pricing data to avoid repeated API calls and improve performance.
Uses bounded TTLCache to prevent memory leaks.
"""

from typing import Any, Dict, Optional

from cachetools import TTLCache  # type: ignore[import-untyped]
from loguru import logger


class PricingCache:
    """Cache for LLM pricing data."""

    def __init__(self, cache_dir: Optional[str] = None, cache_ttl: int = 3600):
        """
        Initialize pricing cache.

        Args:
            cache_dir: Directory to store cache files (DEPRECATED - no longer used)
            cache_ttl: Cache time-to-live in seconds (default: 1 hour)
        """
        self.cache_ttl = cache_ttl
        # Bounded TTLCache to prevent memory leaks
        self._cache: TTLCache = TTLCache(maxsize=500, ttl=cache_ttl)
        logger.info("PricingCache initialized with bounded TTLCache")

    def _load_cache(self):
        """DEPRECATED: No longer loads from disk."""
        pass

    def _save_cache(self):
        """DEPRECATED: No longer saves to disk."""
        pass

    def get(self, key: str) -> Optional[Any]:
        """Get cached pricing data. TTLCache handles expiration automatically."""
        return self._cache.get(key)

    def set(self, key: str, data: Any):
        """Set cached pricing data."""
        self._cache[key] = data

    def get_model_pricing(self, model_name: str) -> Optional[Dict[str, float]]:
        """Get cached pricing for a specific model."""
        return self.get(f"model:{model_name}")

    def set_model_pricing(self, model_name: str, pricing: Dict[str, float]):
        """Cache pricing for a specific model."""
        self.set(f"model:{model_name}", pricing)

    def get_all_pricing(self) -> Optional[Dict[str, Dict[str, float]]]:
        """Get cached pricing for all models."""
        return self.get("all_models")

    def set_all_pricing(self, pricing: Dict[str, Dict[str, float]]):
        """Cache pricing for all models."""
        self.set("all_models", pricing)

    def clear(self):
        """Clear all cached data."""
        self._cache.clear()
        logger.info("Pricing cache cleared")

    def clear_expired(self):
        """Remove expired cache entries. TTLCache handles this automatically via expire()."""
        self._cache.expire()
        logger.debug("Expired cache entries cleared")

    def get_cache_stats(self) -> Dict[str, Any]:
        """Get cache statistics."""
        # TTLCache automatically evicts expired entries on access
        self._cache.expire()
        return {
            "total_entries": len(self._cache),
            "max_entries": self._cache.maxsize,
            "cache_type": "TTLCache",
            "cache_ttl": self.cache_ttl,
        }
