# app/src/market_data/acquisition/adapters/dzengi/websocket.py

from __future__ import annotations

from datetime import datetime, timezone

from src.market_data.acquisition.adapters.dzengi.mapper import (
    map_dzengi_websocket_quote_to_quote,
)
from src.market_data.acquisition.adapters.dzengi.parser import (
    parse_dzengi_websocket_quote,
)
from src.market_data.acquisition.models.quote import Quote
from src.market_data.acquisition.validation.schema import (
    validate_dzengi_websocket_quote_schema,
)
from src.market_data.acquisition.validation.values import (
    validate_dzengi_websocket_quote_values,
)


# Преобразует одно декодированное сообщение Dzengi WebSocket в Quote.
class DzengiWebSocketQuoteAdapter:
    def map_message(
        self,
        document: object,
        *,
        received_at: datetime | None = None,
    ) -> Quote:
        validated = validate_dzengi_websocket_quote_schema(document)
        response = parse_dzengi_websocket_quote(validated)
        validate_dzengi_websocket_quote_values(response)

        return map_dzengi_websocket_quote_to_quote(
            response,
            received_at=received_at or datetime.now(timezone.utc),
        )
