import pytest

from execution import TradingExecutor


class FakeExchange:
    def __init__(self):
        self.calls = []

    def create_order(self, *args, **kwargs):
        self.calls.append((args, kwargs))
        return {"status": "submitted"}

    def set_leverage(self, *args, **kwargs):
        self.calls.append((args, kwargs))
        return {"status": "leverage"}


def test_legacy_trading_executor_blocks_non_dry_run_without_guard(monkeypatch):
    monkeypatch.delenv("CTB_ALLOW_LEGACY_EXECUTION", raising=False)
    ex = FakeExchange()
    executor = TradingExecutor(ex, dry_run=False)

    with pytest.raises(PermissionError, match="Legacy TradingExecutor disabled"):
        executor.create_order("BTC/USDC:USDC", "market", "buy", 0.1)

    assert ex.calls == []


def test_legacy_trading_executor_dry_run_still_allowed(monkeypatch):
    monkeypatch.delenv("CTB_ALLOW_LEGACY_EXECUTION", raising=False)
    executor = TradingExecutor(FakeExchange(), dry_run=True)

    result = executor.create_order("BTC/USDC:USDC", "market", "buy", 0.1)

    assert result["dry_run"] is True
