"""Type definitions and protocols for real-time client."""

import asyncio
from collections import defaultdict
from collections.abc import Callable, Coroutine
from typing import TYPE_CHECKING, Any, Protocol

if TYPE_CHECKING:
    pass


class ProjectXRealtimeClientProtocol(Protocol):
    """Protocol defining the interface for ProjectXRealtimeClient components."""

    # Core attributes
    jwt_token: str
    account_id: str
    user_hub_url: str
    market_hub_url: str
    base_user_url: str
    base_market_url: str

    # Connection objects
    user_connection: Any | None
    market_connection: Any | None

    # Connection state
    user_connected: bool
    market_connected: bool
    setup_complete: bool

    # Callbacks and stats
    callbacks: defaultdict[str, list[Any]]
    stats: dict[str, Any]

    # Subscriptions
    _subscribed_contracts: list[str]

    # Logging and rate limiting
    logger: Any
    rate_limiter: Any

    # Async locks
    _callback_lock: asyncio.Lock
    _connection_lock: asyncio.Lock

    # Event loop
    _loop: asyncio.AbstractEventLoop | None

    # Methods required by mixins
    async def setup_connections(self) -> None: ...
    async def connect(self) -> bool: ...
    async def disconnect(self) -> None: ...
    async def _start_connection_async(self, connection: Any, name: str) -> None: ...
    def _on_user_hub_open(self) -> None: ...
    def _on_user_hub_close(self) -> None: ...
    def _on_market_hub_open(self) -> None: ...
    def _on_market_hub_close(self) -> None: ...
    def _on_connection_error(self, hub: str, error: Any) -> None: ...
    async def add_callback(
        self,
        event_type: str,
        callback: Callable[[dict[str, Any]], Coroutine[Any, Any, None] | None],
    ) -> None: ...
    async def remove_callback(
        self,
        event_type: str,
        callback: Callable[[dict[str, Any]], Coroutine[Any, Any, None] | None],
    ) -> None: ...
    async def _trigger_callbacks(
        self, event_type: str, data: dict[str, Any]
    ) -> None: ...
    def _forward_account_update(self, *args: Any) -> None: ...
    def _forward_position_update(self, *args: Any) -> None: ...
    def _forward_order_update(self, *args: Any) -> None: ...
    def _forward_trade_execution(self, *args: Any) -> None: ...
    def _forward_quote_update(self, *args: Any) -> None: ...
    def _forward_market_trade(self, *args: Any) -> None: ...
    def _forward_market_depth(self, *args: Any) -> None: ...
    def _schedule_async_task(self, event_type: str, data: Any) -> None: ...
    async def _forward_event_async(self, event_type: str, args: Any) -> None: ...
    async def subscribe_user_updates(self) -> bool: ...
    async def subscribe_market_data(self, contract_ids: list[str]) -> bool: ...
    async def unsubscribe_user_updates(self) -> bool: ...
    async def unsubscribe_market_data(self, contract_ids: list[str]) -> bool: ...
    def is_connected(self) -> bool: ...
    def get_stats(self) -> dict[str, Any]: ...
    async def update_jwt_token(self, new_jwt_token: str) -> bool: ...
    async def cleanup(self) -> None: ...
