# app/tests/unit/integrations/exchange/test_status.py

from __future__ import annotations

import pytest

from src.integrations.exchange.status import (
    ExchangeRuntimeStatus,
    ExchangeStatusCode,
    build_market_status_from_symbol_status,
)


_SYMBOL = "BTC/USD_LEVERAGE"


@pytest.mark.parametrize(
    "raw_status",
    [
        "TRADING",
        "OPEN",
        "ACTIVE",
        "ENABLED",
        "ONLINE",
        " trading ",
    ],
)
def test_open_instrument_status_preserves_legacy_contract(
    raw_status: str,
) -> None:
    status = build_market_status_from_symbol_status(
        raw_status=raw_status,
        symbol=_SYMBOL,
    )

    assert status == ExchangeRuntimeStatus(
        code=ExchangeStatusCode.OPEN,
        is_open=True,
        is_available=True,
        is_auth_ok=True,
        title="Биржа доступна",
        message="Рынок открыт.",
        ui_line="🟢 Биржа доступна",
        reason="market_open",
        raw_status=raw_status.strip().upper(),
        symbol=_SYMBOL,
    )


@pytest.mark.parametrize(
    "raw_status",
    [
        "NOT_TRADABLE",
        "TRADING_DISABLED",
        "MARKET_DISABLED",
        "UNAVAILABLE_FOR_TRADING",
        "CLOSE_ONLY",
        "REDUCE_ONLY",
        "VIEW_ONLY",
    ],
)
def test_not_tradable_status_preserves_legacy_contract(
    raw_status: str,
) -> None:
    status = build_market_status_from_symbol_status(
        raw_status=raw_status,
        symbol=_SYMBOL,
    )

    assert status == ExchangeRuntimeStatus(
        code=ExchangeStatusCode.BREAK,
        is_open=False,
        is_available=True,
        is_auth_ok=True,
        title="Рынок недоступен",
        message=(
            "Этот рынок недоступен для торговли: "
            "BTC/USD_LEVERAGE."
        ),
        ui_line="⛔️ Рынок недоступен для торговли",
        reason="market_not_tradable",
        raw_status=raw_status,
        symbol=_SYMBOL,
    )


@pytest.mark.parametrize(
    "raw_status",
    [
        "BREAK",
        "CLOSED",
        "HALT",
        "HALTED",
        "PAUSED",
        "SUSPENDED",
        "DISABLED",
        "SETTLING",
        "POST_ONLY",
    ],
)
def test_break_status_preserves_legacy_contract(
    raw_status: str,
) -> None:
    status = build_market_status_from_symbol_status(
        raw_status=raw_status,
        symbol=_SYMBOL,
    )

    assert status == ExchangeRuntimeStatus(
        code=ExchangeStatusCode.BREAK,
        is_open=False,
        is_available=True,
        is_auth_ok=True,
        title="Перерыв в торгах",
        message="Торги по BTC/USD_LEVERAGE временно остановлены.",
        ui_line="⏸️ Перерыв в торгах",
        reason="market_break",
        raw_status=raw_status,
        symbol=_SYMBOL,
    )


def test_unknown_status_preserves_legacy_contract() -> None:
    status = build_market_status_from_symbol_status(
        raw_status=" maintenance ",
        symbol=_SYMBOL,
    )

    assert status == ExchangeRuntimeStatus(
        code=ExchangeStatusCode.UNKNOWN,
        is_open=False,
        is_available=True,
        is_auth_ok=True,
        title="Статус торгов неизвестен",
        message=(
            "Биржа вернула неизвестный статус инструмента: "
            "MAINTENANCE."
        ),
        ui_line="⚠️ Статус торгов неизвестен",
        reason="market_status_unknown",
        raw_status="MAINTENANCE",
        symbol=_SYMBOL,
    )


@pytest.mark.parametrize(
    "raw_status",
    [
        None,
        "",
        "   ",
    ],
)
def test_empty_status_preserves_legacy_unknown_contract(
    raw_status: str | None,
) -> None:
    status = build_market_status_from_symbol_status(
        raw_status=raw_status,
        symbol=_SYMBOL,
    )

    assert status == ExchangeRuntimeStatus(
        code=ExchangeStatusCode.UNKNOWN,
        is_open=False,
        is_available=True,
        is_auth_ok=True,
        title="Статус торгов неизвестен",
        message="Биржа вернула неизвестный статус инструмента.",
        ui_line="⚠️ Статус торгов неизвестен",
        reason="market_status_unknown",
        raw_status=None,
        symbol=_SYMBOL,
    )