"""Product specification model."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict


@dataclass
class ProductSpec:
    """Trading product specification.

    Attributes
    ----------
    symbol : str
        Normalized symbol name.
    size : int
        Contract size (units per lot).
    lev : int
        Maximum leverage.
    currency : str
        Quote / profit currency.
    type : str
        Product category: forex, metal, commodity, index, crypto, stock.
    """

    symbol: str
    size: int
    lev: int
    currency: str
    type: str

    @classmethod
    def from_dict(cls, symbol: str, data: Dict[str, Any]) -> "ProductSpec":
        return cls(
            symbol=symbol,
            size=int(data.get("size", 100000)),
            lev=int(data.get("lev", 100)),
            currency=str(data.get("currency", "USD")),
            type=str(data.get("type", "forex")),
        )

    def to_dict(self) -> Dict[str, Any]:
        return {
            "symbol": self.symbol,
            "size": self.size,
            "lev": self.lev,
            "currency": self.currency,
            "type": self.type,
        }

    @property
    def pip_value(self) -> float:
        """Pip value per unit (assuming quote currency is the last 3 chars).

        Approximate; overridden for JPY pairs.
        """
        if self.symbol.endswith("JPY"):
            return 0.01
        return 0.0001
