"""
Synor Mining SDK Types.
"""

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, List


class DeviceType(str, Enum):
    """Device type."""
    CPU = "cpu"
    GPU_NVIDIA = "gpu_nvidia"
    GPU_AMD = "gpu_amd"
    ASIC = "asic"


class DeviceStatus(str, Enum):
    """Device status."""
    IDLE = "idle"
    MINING = "mining"
    ERROR = "error"
    OFFLINE = "offline"


class ConnectionStatus(str, Enum):
    """Connection status."""
    DISCONNECTED = "disconnected"
    CONNECTING = "connecting"
    CONNECTED = "connected"
    RECONNECTING = "reconnecting"


class TimePeriod(str, Enum):
    """Time period for stats."""
    HOUR = "hour"
    DAY = "day"
    WEEK = "week"
    MONTH = "month"
    ALL = "all"


class SubmitResultStatus(str, Enum):
    """Submit result status."""
    ACCEPTED = "accepted"
    REJECTED = "rejected"
    STALE = "stale"


@dataclass
class PoolConfig:
    """Pool configuration."""
    url: str
    user: str
    password: Optional[str] = None
    algorithm: Optional[str] = None
    difficulty: Optional[float] = None


@dataclass
class StratumConnection:
    """Stratum connection."""
    id: str
    pool: str
    status: ConnectionStatus
    algorithm: str
    difficulty: float
    connected_at: int
    accepted_shares: int
    rejected_shares: int
    stale_shares: int
    last_share_at: Optional[int] = None


@dataclass
class TemplateTransaction:
    """Template transaction."""
    txid: str
    data: str
    fee: str
    weight: int


@dataclass
class BlockTemplate:
    """Block template."""
    id: str
    previous_block_hash: str
    merkle_root: str
    timestamp: int
    bits: str
    height: int
    coinbase_value: str
    transactions: List[TemplateTransaction]
    target: str
    algorithm: str
    extra_nonce: str


@dataclass
class MinedWork:
    """Mined work to submit."""
    template_id: str
    nonce: str
    extra_nonce: str
    timestamp: int
    hash: str


@dataclass
class ShareInfo:
    """Share info."""
    hash: str
    difficulty: float
    timestamp: int
    accepted: bool


@dataclass
class SubmitResult:
    """Submit result."""
    status: SubmitResultStatus
    share: ShareInfo
    block_found: bool
    reason: Optional[str] = None
    block_hash: Optional[str] = None
    reward: Optional[str] = None


@dataclass
class Hashrate:
    """Hashrate."""
    current: float
    average_1h: float
    average_24h: float
    peak: float
    unit: str


@dataclass
class ShareStats:
    """Share stats."""
    accepted: int
    rejected: int
    stale: int
    total: int
    accept_rate: float


@dataclass
class DeviceTemperature:
    """Device temperature."""
    current: float
    max: float
    throttling: bool


@dataclass
class EarningsSnapshot:
    """Earnings snapshot."""
    today: str
    yesterday: str
    this_week: str
    this_month: str
    total: str
    currency: str


@dataclass
class MiningStats:
    """Mining stats."""
    hashrate: Hashrate
    shares: ShareStats
    uptime: int
    efficiency: float
    earnings: EarningsSnapshot
    power_consumption: Optional[float] = None
    temperature: Optional[DeviceTemperature] = None


@dataclass
class EarningsBreakdown:
    """Earnings breakdown."""
    date: int
    amount: str
    blocks: int
    shares: int
    hashrate: float


@dataclass
class Earnings:
    """Detailed earnings."""
    period: TimePeriod
    start_date: int
    end_date: int
    amount: str
    blocks: int
    shares: int
    average_hashrate: float
    currency: str
    breakdown: List[EarningsBreakdown]


@dataclass
class MiningDevice:
    """Mining device."""
    id: str
    name: str
    type: DeviceType
    status: DeviceStatus
    hashrate: float
    temperature: float
    fan_speed: float
    power_draw: float
    memory_used: int
    memory_total: int
    driver: Optional[str] = None
    firmware: Optional[str] = None


@dataclass
class DeviceConfig:
    """Device configuration."""
    enabled: bool
    intensity: Optional[int] = None
    power_limit: Optional[int] = None
    core_clock_offset: Optional[int] = None
    memory_clock_offset: Optional[int] = None
    fan_speed: Optional[int] = None


@dataclass
class WorkerInfo:
    """Worker info."""
    id: str
    name: str
    status: ConnectionStatus
    hashrate: Hashrate
    shares: ShareStats
    devices: List[MiningDevice]
    last_seen: int
    uptime: int


@dataclass
class PoolStats:
    """Pool stats."""
    url: str
    workers: int
    hashrate: float
    difficulty: float
    last_block: int
    blocks_found_24h: int
    luck: float


@dataclass
class MiningAlgorithm:
    """Mining algorithm."""
    name: str
    display_name: str
    hash_unit: str
    profitability: str
    difficulty: float
    block_reward: str
    block_time: int


@dataclass
class MiningConfig:
    """Mining SDK configuration."""
    api_key: str
    endpoint: str = "https://mining.synor.io/v1"
    timeout: float = 30.0
    retries: int = 3
    debug: bool = False
