"""Order request model."""

from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, Optional


class OrderSide(str, Enum):
    BUY = "BUY"
    SELL = "SELL"


@dataclass
class OrderRequest:
    """A request to execute a trade (simulated or real).

    Attributes
    ----------
    symbol : str
        Product symbol (e.g. "XAUUSD").
    side : str
        "BUY" or "SELL".
    volume : float
        Position size in lots.
    price : float
        Desired entry price (0 for market order).
    stop_loss : float, optional
        SL price.
    take_profit : float, optional
        TP price.
    comment : str
        Optional annotation.
    extra : Dict[str, Any]
        Additional metadata (signal confidence, etc.).
    """
    symbol: str
    side: str
    volume: float
    price: float = 0.0
    stop_loss: Optional[float] = None
    take_profit: Optional[float] = None
    comment: str = ""
    extra: Dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "symbol": self.symbol,
            "side": self.side,
            "volume": self.volume,
            "price": self.price,
            "stop_loss": self.stop_loss,
            "take_profit": self.take_profit,
            "comment": self.comment,
            "extra": self.extra,
        }

    @classmethod
    def from_signal(
        cls,
        symbol: str,
        side: str,
        price: float,
        volume: float = 0.01,
        **kwargs: Any,
    ) -> "OrderRequest":
        return cls(symbol=symbol, side=side, price=price, volume=volume, **kwargs)
