from __future__ import annotations

from dataclasses import dataclass
import os
from typing import Any

LEGACY_EXECUTION_DISABLED_MESSAGE = "Legacy TradingExecutor disabled; use src/tools/preflight.py and v76 executors"


def require_legacy_execution_enabled() -> None:
    if os.getenv("CTB_ALLOW_LEGACY_EXECUTION", "").lower() != "true":
        raise PermissionError(LEGACY_EXECUTION_DISABLED_MESSAGE)


@dataclass
class TradingExecutor:
    """Small execution boundary that makes dry-run behaviour explicit and testable."""

    exchange: Any
    dry_run: bool = True

    def set_leverage(self, leverage: int | float, symbol: str) -> Any:
        if self.dry_run:
            return {"dry_run": True, "action": "set_leverage", "leverage": leverage, "symbol": symbol}
        require_legacy_execution_enabled()
        return self.exchange.set_leverage(leverage, symbol)

    def create_order(
        self,
        symbol: str,
        order_type: str,
        side: str,
        amount: float,
        price: float | None = None,
        params: dict[str, Any] | None = None,
    ) -> Any:
        if self.dry_run:
            return {
                "dry_run": True,
                "action": "create_order",
                "symbol": symbol,
                "type": order_type,
                "side": side,
                "amount": amount,
                "price": price,
                "params": params or {},
            }
        require_legacy_execution_enabled()
        return self.exchange.create_order(symbol, order_type, side, amount, price, params=params or {})
