"""
Trading-related data models
"""

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


@dataclass
class TradingSignal:
    """
    Trading signal generated by strategy

    Attributes:
        symbol: Trading pair symbol
        side: Trade direction ('long' | 'short')
        entry_price: Entry price
        stop_loss: Stop loss price
        take_profit: Take profit price
        confidence: Signal confidence score (0-1)
        timestamp: Unix timestamp
        strategy_name: Name of strategy that generated the signal
        metadata: Additional signal metadata
    """

    symbol: str
    side: str  # 'long' | 'short'
    entry_price: float
    stop_loss: float
    take_profit: float
    confidence: float
    timestamp: int
    strategy_name: str
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class Position:
    """
    Current position information

    Attributes:
        symbol: Trading pair symbol
        side: Position side ('long' | 'short')
        entry_price: Entry price
        current_price: Current market price
        quantity: Position size
        unrealized_pnl: Unrealized profit/loss
        realized_pnl: Realized profit/loss
        timestamp: Unix timestamp
    """

    symbol: str
    side: str  # 'long' | 'short'
    entry_price: float
    current_price: float
    quantity: float
    unrealized_pnl: float
    realized_pnl: float
    timestamp: int


@dataclass
class OrderAction:
    """
    Order action to be executed

    Attributes:
        action_type: Type of action ('open' | 'close' | 'update_sl' | 'update_tp')
        symbol: Trading pair symbol
        side: Order side ('long' | 'short')
        quantity: Order quantity
        price: Order price (None for market orders)
        stop_loss: Stop loss price
        take_profit: Take profit price
        reason: Reason for the action
    """

    action_type: str  # 'open' | 'close' | 'update_sl' | 'update_tp'
    symbol: str
    side: str  # 'long' | 'short'
    quantity: float
    price: Optional[float] = None
    stop_loss: Optional[float] = None
    take_profit: Optional[float] = None
    reason: str = ""


@dataclass
class ExecutionResult:
    """
    Strategy execution result

    Attributes:
        success: Whether execution was successful
        signals: List of generated trading signals
        orders: List of order actions
        errors: List of error messages
        execution_time: Execution time in seconds
        metadata: Additional execution metadata
    """

    success: bool
    signals: List[TradingSignal]
    orders: List[OrderAction]
    errors: List[str]
    execution_time: float
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class Trade:
    """
    Completed trade record

    Attributes:
        trade_id: Unique trade identifier
        symbol: Trading pair symbol
        side: Trade side ('long' | 'short')
        entry_time: Entry timestamp
        entry_price: Entry price
        exit_time: Exit timestamp (None if still open)
        exit_price: Exit price (None if still open)
        quantity: Trade quantity
        stop_loss: Stop loss price
        take_profit: Take profit price
        status: Trade status ('open' | 'closed' | 'cancelled')
        pnl: Profit/loss amount (None if still open)
        fees: Trading fees
        strategy_name: Strategy that generated the trade
        metadata: Additional trade metadata
    """

    trade_id: str
    symbol: str
    side: str  # 'long' | 'short'
    entry_time: int
    entry_price: float
    exit_time: Optional[int] = None
    exit_price: Optional[float] = None
    quantity: float = 0.0
    stop_loss: float = 0.0
    take_profit: float = 0.0
    status: str = "open"  # 'open' | 'closed' | 'cancelled'
    pnl: Optional[float] = None
    fees: float = 0.0
    strategy_name: str = ""
    metadata: Dict[str, Any] = field(default_factory=dict)
