# app/tests/unit/market_data/acquisition/adapters/dzengi/test_candle_mapper.py

from __future__ import annotations

from datetime import timezone
from decimal import Decimal

from src.market_data.acquisition.adapters.dzengi.mapper import (
    map_dzengi_klines_to_candles,
)
from src.market_data.acquisition.adapters.dzengi.models import (
    DzengiKline,
    DzengiKlinesResponse,
)


def _item(open_time: int) -> DzengiKline:
    return DzengiKline(
        open_time=open_time,
        open_price="100.10",
        high_price="110.20",
        low_price="90.30",
        close_price="105.40",
        volume="12.50",
    )


def test_map_dzengi_klines_to_candles_maps_canonical_values() -> None:
    response = DzengiKlinesResponse(items=(_item(1000),))

    result = map_dzengi_klines_to_candles(
        response,
        symbol="BTC/USD",
        interval="1m",
        source="rest_klines:bid",
    )

    assert len(result) == 1
    candle = result[0]
    assert candle.symbol == "BTC/USD"
    assert candle.interval == "1m"
    assert candle.open_time.tzinfo is timezone.utc
    assert candle.open_time.timestamp() == 1
    assert candle.open_price == Decimal("100.10")
    assert candle.high_price == Decimal("110.20")
    assert candle.low_price == Decimal("90.30")
    assert candle.close_price == Decimal("105.40")
    assert candle.volume == Decimal("12.50")
    assert candle.source == "rest_klines:bid"


def test_map_dzengi_klines_to_candles_sorts_by_open_time() -> None:
    response = DzengiKlinesResponse(
        items=(
            _item(3000),
            _item(1000),
            _item(2000),
        )
    )

    result = map_dzengi_klines_to_candles(
        response,
        symbol="BTC/USD",
        interval="1m",
        source="rest_klines:bid",
    )

    assert [item.open_time.timestamp() for item in result] == [1, 2, 3]


def test_map_dzengi_klines_to_candles_returns_tuple() -> None:
    result = map_dzengi_klines_to_candles(
        DzengiKlinesResponse(items=()),
        symbol="BTC/USD",
        interval="1m",
        source="rest_klines:bid",
    )

    assert result == ()
    assert isinstance(result, tuple)
