"""Data types and exceptions for the quicproquo Python SDK."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Optional


# ---------------------------------------------------------------------------
# Exceptions
# ---------------------------------------------------------------------------


class QpqError(Exception):
    """Base exception for quicproquo SDK errors."""


class AuthError(QpqError):
    """OPAQUE authentication failed (bad credentials)."""


class TimeoutError(QpqError):
    """An operation timed out waiting for a response."""


class ConnectionError(QpqError):
    """Could not connect to or communicate with the server."""


# ---------------------------------------------------------------------------
# Connection options
# ---------------------------------------------------------------------------


@dataclass(frozen=True)
class ConnectOptions:
    """Options for connecting to a quicproquo server.

    Parameters
    ----------
    addr:
        Server address as ``host:port``.
    ca_cert_path:
        Path to PEM-encoded CA certificate.  Required for production.
    server_name:
        TLS SNI server name (defaults to the host part of *addr*).
    insecure_skip_verify:
        Disable TLS certificate verification (development only).
    connect_timeout_ms:
        Connection timeout in milliseconds (default 5000).
    request_timeout_ms:
        Per-request timeout in milliseconds (default 10000).
    """

    addr: str
    ca_cert_path: str = ""
    server_name: str = ""
    insecure_skip_verify: bool = False
    connect_timeout_ms: int = 5_000
    request_timeout_ms: int = 10_000


# ---------------------------------------------------------------------------
# Response types
# ---------------------------------------------------------------------------


@dataclass(frozen=True)
class Envelope:
    """A received message envelope."""

    seq: int
    data: bytes


@dataclass(frozen=True)
class ChannelResult:
    """Result of creating or joining a channel."""

    channel_id: bytes
    was_new: bool


@dataclass(frozen=True)
class HealthInfo:
    """Server health check response."""

    status: str
    node_id: str = ""
    version: str = ""
    uptime_secs: int = 0
    storage_backend: str = ""
