# app/tests/unit/trading/auto/test_signal_runtime_quote.py

from __future__ import annotations

import inspect
from datetime import datetime, timezone
from decimal import Decimal
from types import SimpleNamespace

import pytest

import src.trading.auto.signal_runtime as module
from src.market_data.acquisition.models.quote import Quote
from src.trading.auto.signal_runtime import AutoSignalRuntimeMixin


def _quote() -> Quote:
    return Quote(
        symbol="BTC/USD_LEVERAGE",
        last_price=Decimal("100.5"),
        bid_price=Decimal("100.0"),
        ask_price=Decimal("101.0"),
        exchange_timestamp=None,
        received_at=datetime.now(timezone.utc),
        source="dzengi",
    )


class Harness(AutoSignalRuntimeMixin):
    _ready_confidence = 0.3


def test_ready_signal_uses_canonical_quote(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    quote = _quote()
    requested: list[tuple[str, str]] = []
    captured: dict[str, object] = {}

    class Service:
        def get_quote(self, symbol: str, *, runtime_key: str) -> Quote:
            requested.append((symbol, runtime_key))
            return quote

    class Journal:
        def log_ui_info(self, **kwargs: object) -> None:
            captured.update(kwargs)

    harness = Harness()

    def build_payload(**kwargs: object) -> dict[str, object]:
        captured["quote"] = kwargs["quote"]
        return {"ok": True}

    monkeypatch.setattr(module, "ExchangeService", Service)
    monkeypatch.setattr(module, "JournalService", Journal)
    monkeypatch.setattr(harness, "_build_ready_signal_payload", build_payload)

    state = SimpleNamespace(symbol="BTC/USD_LEVERAGE")
    harness._log_ready_signal(
        state=state,  # type: ignore[arg-type]
        signal="BUY",
        reason="test",
        confidence=0.9,
        signal_intent="ENTRY",
    )

    assert requested == [("BTC/USD_LEVERAGE", "auto")]
    assert captured["quote"] is quote


def test_signal_runtime_has_no_legacy_market_snapshot_call() -> None:
    source = inspect.getsource(module.AutoSignalRuntimeMixin)

    assert "get_quote(" in source
