# app/src/market_data/acquisition/handlers/candles_handler.py

from __future__ import annotations

from src.market_data.acquisition.adapters.dzengi.mapper import (
    map_dzengi_klines_to_candles,
)
from src.market_data.acquisition.adapters.dzengi.parser import (
    parse_candles,
)
from src.market_data.acquisition.models.candle import Candle
from src.market_data.acquisition.validation.schema import (
    validate_candles_schema,
)
from src.market_data.acquisition.validation.values import (
    validate_candles_values,
)


# Обработчик документа Candles Feed формата Dzengi /api/v1/klines.
class DzengiCandlesDocumentHandler:
    def handle_candles_document(
        self,
        document: object,
        *,
        symbol: str,
        interval: str,
        source: str,
    ) -> tuple[Candle, ...]:
        """
        Преобразовать сырой документ Dzengi в канонические модели Candle.

        Порядок обработки:

        schema validation
            →
        parsing
            →
        value validation
            →
        mapping
        """

        validated_document = validate_candles_schema(document)
        response = parse_candles(validated_document)

        validate_candles_values(response)

        return map_dzengi_klines_to_candles(
            response,
            symbol=symbol,
            interval=interval,
            source=source,
        )