from __future__ import annotations

import pytest

from src.market_data.acquisition.adapters.dzengi.parser import (
    parse_dzengi_websocket_quote,
)
from src.market_data.acquisition.exceptions import QuoteParseError
from src.market_data.acquisition.validation.schema import (
    validate_dzengi_websocket_quote_schema,
)


def _parse(document: object):
    return parse_dzengi_websocket_quote(
        validate_dzengi_websocket_quote_schema(document)
    )


def test_parses_direct_quote() -> None:
    result = _parse(
        {"symbolName": "BTC/USD", "bid": "10", "ofr": "11", "timestamp": 1000}
    )
    assert result.symbol == "BTC/USD"
    assert result.bid_price == "10"
    assert result.ask_price == "11"
    assert result.timestamp == 1000


def test_parses_depth_list_entries() -> None:
    result = _parse(
        {"symbol": "BTC/USD", "bids": [["10", "2"]], "asks": [["11", "3"]]}
    )
    assert result.bid_price == "10"
    assert result.ask_price == "11"
    assert result.timestamp is None


def test_parses_depth_dict_aliases() -> None:
    result = _parse(
        {"symbol": "BTC/USD", "bids": [{"p": "10"}], "asks": [{"askPrice": "11"}]}
    )
    assert result.bid_price == "10"
    assert result.ask_price == "11"


def test_rejects_invalid_depth_item() -> None:
    validated = validate_dzengi_websocket_quote_schema(
        {"symbol": "BTC/USD", "bids": ["10"], "asks": [["11"]]}
    )
    with pytest.raises(QuoteParseError):
        parse_dzengi_websocket_quote(validated)
