"""
Data models for exchange-data-manager client.
"""

from dataclasses import dataclass, field
from typing import Optional
from datetime import datetime


@dataclass
class Candle:
    """OHLCV candle data."""
    time: int  # Unix timestamp in milliseconds
    open: float
    high: float
    low: float
    close: float
    volume: float
    symbol: Optional[str] = None
    exchange: Optional[str] = None
    timeframe: Optional[str] = None
    closed: bool = True

    def to_dict(self) -> dict:
        """Convert to dictionary."""
        return {
            'time': self.time,
            'open': self.open,
            'high': self.high,
            'low': self.low,
            'close': self.close,
            'volume': self.volume,
            'symbol': self.symbol,
            'exchange': self.exchange,
            'timeframe': self.timeframe,
            'closed': self.closed,
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'Candle':
        """Create Candle from dictionary."""
        return cls(
            time=data['time'],
            open=data['open'],
            high=data['high'],
            low=data['low'],
            close=data['close'],
            volume=data['volume'],
            symbol=data.get('symbol'),
            exchange=data.get('exchange'),
            timeframe=data.get('timeframe'),
            closed=data.get('closed', True),
        )


@dataclass
class Subscription:
    """WebSocket subscription to a candle stream."""
    id: str
    exchange: str
    symbol: str
    timeframe: str
    active: bool = True

    def to_key(self) -> str:
        """Generate unique key for this subscription."""
        return f"{self.exchange}:{self.symbol}:{self.timeframe}"


@dataclass
class EdmConfig:
    """Configuration for EDM client."""
    rest_url: str = "http://localhost:8080"
    ws_url: str = "ws://localhost:8765"
    timeout: float = 30.0
    enabled: bool = True
    reconnect_interval: float = 5.0
    max_reconnect_attempts: int = 10
