"""
Performance analysis data models
"""

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


@dataclass
class TradeMetrics:
    """
    Metrics for a single trade

    Attributes:
        trade_id: Trade identifier
        symbol: Trading pair symbol
        side: Trade side ('long' | 'short')
        entry_price: Entry price
        exit_price: Exit price
        quantity: Trade quantity
        pnl: Profit/loss amount
        pnl_percent: Profit/loss percentage
        holding_time: Holding time in seconds
        fees: Trading fees
    """

    trade_id: str
    symbol: str
    side: str
    entry_price: float
    exit_price: float
    quantity: float
    pnl: float
    pnl_percent: float
    holding_time: int
    fees: float


@dataclass
class PeriodMetrics:
    """
    Performance metrics for a time period

    Attributes:
        total_trades: Total number of trades
        winning_trades: Number of winning trades
        losing_trades: Number of losing trades
        win_rate: Win rate (0-1)
        total_pnl: Total profit/loss
        avg_win: Average winning trade amount
        avg_loss: Average losing trade amount
        profit_factor: Profit factor (gross profit / gross loss)
        max_drawdown: Maximum drawdown (0-1)
        sharpe_ratio: Sharpe ratio
        sortino_ratio: Sortino ratio
    """

    total_trades: int
    winning_trades: int
    losing_trades: int
    win_rate: float
    total_pnl: float
    avg_win: float
    avg_loss: float
    profit_factor: float
    max_drawdown: float
    sharpe_ratio: float
    sortino_ratio: float


@dataclass
class PerformanceReport:
    """
    Complete performance analysis report

    Attributes:
        period: Analysis period ('daily' | 'weekly' | 'monthly')
        start_time: Period start timestamp
        end_time: Period end timestamp
        metrics: Period metrics
        trade_list: List of individual trade metrics
        equity_curve: Equity curve values
        anomalies: List of detected anomalies
    """

    period: str
    start_time: int
    end_time: int
    metrics: PeriodMetrics
    trade_list: List[TradeMetrics]
    equity_curve: List[float]
    anomalies: List[str] = field(default_factory=list)

    def to_dict(self) -> Dict[str, Any]:
        """
        Convert report to dictionary for serialization

        Returns:
            Dictionary representation of the report
        """
        from dataclasses import asdict

        data = asdict(self)
        # Handle infinity in profit_factor
        if data["metrics"]["profit_factor"] == float("inf"):
            data["metrics"]["profit_factor"] = "infinity"
        return data

    def to_json(self) -> str:
        """
        Convert report to JSON string

        Returns:
            JSON string representation
        """
        import json

        return json.dumps(self.to_dict(), indent=2)
