"""Trade signal model."""

from __future__ import annotations

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


@dataclass
class TradeSignal:
    """An analytical trade signal generated by a strategy or indicator.

    Attributes
    ----------
    symbol : str
        Target product symbol.
    side : str
        "BUY" or "SELL".
    reason : str
        Human-readable description of why the signal was triggered.
    confidence : float
        Signal confidence in [0.0, 1.0].
    price : float
        Price at which the signal was generated.
    ts_ms : int
        Timestamp in milliseconds when the signal was created.
    extra : Dict[str, Any]
        Additional metadata (indicator values, etc.).
    """

    symbol: str
    side: str  # "BUY" or "SELL"
    reason: str
    confidence: float = 1.0
    price: float = 0.0
    ts_ms: int = 0
    extra: Dict[str, Any] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "symbol": self.symbol,
            "side": self.side,
            "reason": self.reason,
            "confidence": self.confidence,
            "price": self.price,
            "ts_ms": self.ts_ms,
            "extra": self.extra,
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "TradeSignal":
        return cls(
            symbol=str(data["symbol"]),
            side=str(data["side"]),
            reason=str(data["reason"]),
            confidence=float(data.get("confidence", 1.0)),
            price=float(data.get("price", 0.0)),
            ts_ms=int(data.get("ts_ms", 0)),
            extra=dict(data.get("extra", {})),
        )

    @property
    def is_buy(self) -> bool:
        return self.side.upper() == "BUY"

    @property
    def is_sell(self) -> bool:
        return self.side.upper() == "SELL"
