# app/src/integrations/exchange/market_cache.py

from __future__ import annotations

from src.market_data.acquisition.models.quote import Quote
from src.storage.quote_store import InMemoryQuoteStore, QuoteStoreProtocol


_MARKET_PRICE_CACHE_SOURCE_NAME = "legacy-market-price-cache"


class MarketPriceCache:
    # Временный compatibility facade над каноническим Quote Store.
    _store: QuoteStoreProtocol = InMemoryQuoteStore()

    @classmethod
    def set_quote(
        cls,
        quote: Quote,
        *,
        runtime_key: str = "default",
    ) -> None:
        cls._store.set(
            _MARKET_PRICE_CACHE_SOURCE_NAME,
            quote,
            runtime_key=cls._normalize_runtime_key(runtime_key),
        )

    @classmethod
    def get_quote(
        cls,
        symbol: str,
        *,
        runtime_key: str = "default",
    ) -> Quote | None:
        return cls._store.get(
            _MARKET_PRICE_CACHE_SOURCE_NAME,
            cls._normalize_symbol(symbol),
            runtime_key=cls._normalize_runtime_key(runtime_key),
        )

    @classmethod
    def clear(
        cls,
        symbol: str | None = None,
        *,
        runtime_key: str | None = None,
    ) -> None:
        cls._store.clear(
            source_name=_MARKET_PRICE_CACHE_SOURCE_NAME,
            symbol=(
                cls._normalize_symbol(symbol)
                if symbol is not None
                else None
            ),
            runtime_key=(
                cls._normalize_runtime_key(runtime_key)
                if runtime_key is not None
                else None
            ),
        )

    @staticmethod
    def _normalize_symbol(symbol: str) -> str:
        return str(symbol).strip().upper()

    @staticmethod
    def _normalize_runtime_key(runtime_key: str) -> str:
        return str(runtime_key).strip().lower()
