"""Protocol-level constants and default configuration values."""

from __future__ import annotations

import ssl
from enum import IntEnum

__all__: list[str] = [
    "ALPN_H3",
    "BIDIRECTIONAL_STREAM",
    "CLOSE_WEBTRANSPORT_SESSION_TYPE",
    "DEFAULT_ALPN_PROTOCOLS",
    "DEFAULT_BIND_HOST",
    "DEFAULT_CERTFILE",
    "DEFAULT_CLIENT_MAX_CONNECTIONS",
    "DEFAULT_CLIENT_MAX_SESSIONS",
    "DEFAULT_CLIENT_VERIFY_MODE",
    "DEFAULT_CLOSE_TIMEOUT",
    "DEFAULT_CONGESTION_CONTROL_ALGORITHM",
    "DEFAULT_CONNECT_TIMEOUT",
    "DEFAULT_CONNECTION_IDLE_TIMEOUT",
    "DEFAULT_DEV_PORT",
    "DEFAULT_FLOW_CONTROL_WINDOW_AUTO_SCALE",
    "DEFAULT_FLOW_CONTROL_WINDOW_SIZE",
    "DEFAULT_INITIAL_MAX_DATA",
    "DEFAULT_INITIAL_MAX_STREAMS_BIDI",
    "DEFAULT_INITIAL_MAX_STREAMS_UNI",
    "DEFAULT_KEEP_ALIVE",
    "DEFAULT_KEYFILE",
    "DEFAULT_LOG_LEVEL",
    "DEFAULT_MAX_CAPSULE_SIZE",
    "DEFAULT_MAX_CONNECTION_RETRIES",
    "DEFAULT_MAX_DATAGRAM_SIZE",
    "DEFAULT_MAX_EVENT_HISTORY_SIZE",
    "DEFAULT_MAX_EVENT_LISTENERS",
    "DEFAULT_MAX_EVENT_QUEUE_SIZE",
    "DEFAULT_MAX_MESSAGE_SIZE",
    "DEFAULT_MAX_PENDING_EVENTS_PER_SESSION",
    "DEFAULT_MAX_RETRY_DELAY",
    "DEFAULT_MAX_STREAM_READ_BUFFER",
    "DEFAULT_MAX_STREAM_WRITE_BUFFER",
    "DEFAULT_MAX_TOTAL_PENDING_EVENTS",
    "DEFAULT_PENDING_EVENT_TTL",
    "DEFAULT_READ_TIMEOUT",
    "DEFAULT_RESOURCE_CLEANUP_INTERVAL",
    "DEFAULT_RETRY_BACKOFF",
    "DEFAULT_RETRY_DELAY",
    "DEFAULT_SERVER_MAX_CONNECTIONS",
    "DEFAULT_SERVER_MAX_SESSIONS",
    "DEFAULT_SERVER_VERIFY_MODE",
    "DEFAULT_STREAM_CREATION_TIMEOUT",
    "DEFAULT_WRITE_TIMEOUT",
    "DRAIN_WEBTRANSPORT_SESSION_TYPE",
    "ErrorCodes",
    "H3_FRAME_TYPE_CANCEL_PUSH",
    "H3_FRAME_TYPE_DATA",
    "H3_FRAME_TYPE_GOAWAY",
    "H3_FRAME_TYPE_HEADERS",
    "H3_FRAME_TYPE_MAX_PUSH_ID",
    "H3_FRAME_TYPE_PUSH_PROMISE",
    "H3_FRAME_TYPE_SETTINGS",
    "H3_FRAME_TYPE_WEBTRANSPORT_STREAM",
    "H3_STREAM_TYPE_CONTROL",
    "H3_STREAM_TYPE_PUSH",
    "H3_STREAM_TYPE_QPACK_DECODER",
    "H3_STREAM_TYPE_QPACK_ENCODER",
    "H3_STREAM_TYPE_WEBTRANSPORT",
    "MAX_CLOSE_REASON_BYTES",
    "MAX_DATAGRAM_SIZE",
    "MAX_PROTOCOL_STREAMS_LIMIT",
    "MAX_STREAM_ID",
    "SETTINGS_ENABLE_CONNECT_PROTOCOL",
    "SETTINGS_H3_DATAGRAM",
    "SETTINGS_QPACK_BLOCKED_STREAMS",
    "SETTINGS_QPACK_MAX_TABLE_CAPACITY",
    "SETTINGS_WT_INITIAL_MAX_DATA",
    "SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI",
    "SETTINGS_WT_INITIAL_MAX_STREAMS_UNI",
    "SUPPORTED_CONGESTION_CONTROL_ALGORITHMS",
    "UNIDIRECTIONAL_STREAM",
    "USER_AGENT_HEADER",
    "WEBTRANSPORT_DEFAULT_PORT",
    "WEBTRANSPORT_SCHEME",
    "WT_DATA_BLOCKED_TYPE",
    "WT_MAX_DATA_TYPE",
    "WT_MAX_STREAM_DATA_TYPE",
    "WT_MAX_STREAMS_BIDI_TYPE",
    "WT_MAX_STREAMS_UNI_TYPE",
    "WT_STREAM_DATA_BLOCKED_TYPE",
    "WT_STREAMS_BLOCKED_BIDI_TYPE",
    "WT_STREAMS_BLOCKED_UNI_TYPE",
]

ALPN_H3: str = "h3"
USER_AGENT_HEADER: str = "user-agent"
WEBTRANSPORT_SCHEME: str = "https"
WEBTRANSPORT_DEFAULT_PORT: int = 443

BIDIRECTIONAL_STREAM: int = 0x0
CLOSE_WEBTRANSPORT_SESSION_TYPE: int = 0x2843
DRAIN_WEBTRANSPORT_SESSION_TYPE: int = 0x78AE
H3_FRAME_TYPE_DATA: int = 0x0
H3_FRAME_TYPE_HEADERS: int = 0x1
H3_FRAME_TYPE_CANCEL_PUSH: int = 0x3
H3_FRAME_TYPE_SETTINGS: int = 0x4
H3_FRAME_TYPE_PUSH_PROMISE: int = 0x5
H3_FRAME_TYPE_GOAWAY: int = 0x7
H3_FRAME_TYPE_MAX_PUSH_ID: int = 0xD
H3_FRAME_TYPE_WEBTRANSPORT_STREAM: int = 0x41
H3_STREAM_TYPE_CONTROL: int = 0x0
H3_STREAM_TYPE_PUSH: int = 0x1
H3_STREAM_TYPE_QPACK_ENCODER: int = 0x2
H3_STREAM_TYPE_QPACK_DECODER: int = 0x3
H3_STREAM_TYPE_WEBTRANSPORT: int = 0x54
MAX_CLOSE_REASON_BYTES: int = 1024
MAX_DATAGRAM_SIZE: int = 65535
MAX_PROTOCOL_STREAMS_LIMIT: int = 2**60
MAX_STREAM_ID: int = 2**62 - 1
SETTINGS_ENABLE_CONNECT_PROTOCOL: int = 0x8
SETTINGS_H3_DATAGRAM: int = 0x33
SETTINGS_QPACK_BLOCKED_STREAMS: int = 0x7
SETTINGS_QPACK_MAX_TABLE_CAPACITY: int = 0x1
SETTINGS_WT_INITIAL_MAX_DATA: int = 0x2B61
SETTINGS_WT_INITIAL_MAX_STREAMS_BIDI: int = 0x2B65
SETTINGS_WT_INITIAL_MAX_STREAMS_UNI: int = 0x2B64
UNIDIRECTIONAL_STREAM: int = 0x2
WT_DATA_BLOCKED_TYPE: int = 0x190B4D41
WT_MAX_DATA_TYPE: int = 0x190B4D3D
WT_MAX_STREAM_DATA_TYPE: int = 0x190B4D3E
WT_MAX_STREAMS_BIDI_TYPE: int = 0x190B4D3F
WT_MAX_STREAMS_UNI_TYPE: int = 0x190B4D40
WT_STREAM_DATA_BLOCKED_TYPE: int = 0x190B4D42
WT_STREAMS_BLOCKED_BIDI_TYPE: int = 0x190B4D43
WT_STREAMS_BLOCKED_UNI_TYPE: int = 0x190B4D44

DEFAULT_ALPN_PROTOCOLS: tuple[str, ...] = (ALPN_H3,)
DEFAULT_BIND_HOST: str = "::"
DEFAULT_CERTFILE: str | None = None
DEFAULT_CLIENT_MAX_CONNECTIONS: int = 100
DEFAULT_CLIENT_MAX_SESSIONS: int = 100
DEFAULT_CLIENT_VERIFY_MODE: ssl.VerifyMode = ssl.CERT_REQUIRED
DEFAULT_CLOSE_TIMEOUT: float = 5.0
DEFAULT_CONNECT_TIMEOUT: float = 30.0
DEFAULT_CONGESTION_CONTROL_ALGORITHM: str = "cubic"
DEFAULT_CONNECTION_IDLE_TIMEOUT: float = 60.0
DEFAULT_DEV_PORT: int = 4433
DEFAULT_FLOW_CONTROL_WINDOW_AUTO_SCALE: bool = True
DEFAULT_FLOW_CONTROL_WINDOW_SIZE: int = 1024 * 1024
DEFAULT_INITIAL_MAX_DATA: int = 10 * 1024 * 1024
DEFAULT_INITIAL_MAX_STREAMS_BIDI: int = 100
DEFAULT_INITIAL_MAX_STREAMS_UNI: int = 100
DEFAULT_KEEP_ALIVE: bool = True
DEFAULT_KEYFILE: str | None = None
DEFAULT_LOG_LEVEL: str = "INFO"
DEFAULT_MAX_CAPSULE_SIZE: int = 65536
DEFAULT_MAX_CONNECTION_RETRIES: int = 3
DEFAULT_MAX_DATAGRAM_SIZE: int = 1350
DEFAULT_MAX_EVENT_HISTORY_SIZE: int = 0
DEFAULT_MAX_EVENT_LISTENERS: int = 100
DEFAULT_MAX_EVENT_QUEUE_SIZE: int = 1000
DEFAULT_MAX_MESSAGE_SIZE: int = 1024 * 1024
DEFAULT_MAX_PENDING_EVENTS_PER_SESSION: int = 100
DEFAULT_MAX_RETRY_DELAY: float = 30.0
DEFAULT_MAX_STREAM_READ_BUFFER: int = 2 * 1024 * 1024
DEFAULT_MAX_STREAM_WRITE_BUFFER: int = 2 * 1024 * 1024
DEFAULT_MAX_TOTAL_PENDING_EVENTS: int = 1000
DEFAULT_PENDING_EVENT_TTL: float = 5.0
DEFAULT_READ_TIMEOUT: float = 60.0
DEFAULT_RESOURCE_CLEANUP_INTERVAL: float = 15.0
DEFAULT_RETRY_BACKOFF: float = 2.0
DEFAULT_RETRY_DELAY: float = 1.0
DEFAULT_SERVER_MAX_CONNECTIONS: int = 3000
DEFAULT_SERVER_MAX_SESSIONS: int = 10000
DEFAULT_SERVER_VERIFY_MODE: ssl.VerifyMode = ssl.CERT_NONE
DEFAULT_STREAM_CREATION_TIMEOUT: float = 10.0
DEFAULT_WRITE_TIMEOUT: float = 30.0
SUPPORTED_CONGESTION_CONTROL_ALGORITHMS: tuple[str, str] = ("reno", "cubic")


class ErrorCodes(IntEnum):
    """Collection of standard WebTransport and QUIC error codes."""

    NO_ERROR = 0x0
    INTERNAL_ERROR = 0x1
    CONNECTION_REFUSED = 0x2
    FLOW_CONTROL_ERROR = 0x3
    STREAM_LIMIT_ERROR = 0x4
    STREAM_STATE_ERROR = 0x5
    FINAL_SIZE_ERROR = 0x6
    FRAME_ENCODING_ERROR = 0x7
    TRANSPORT_PARAMETER_ERROR = 0x8
    CONNECTION_ID_LIMIT_ERROR = 0x9
    PROTOCOL_VIOLATION = 0xA
    INVALID_TOKEN = 0xB
    APPLICATION_ERROR = 0xC
    CRYPTO_BUFFER_EXCEEDED = 0xD
    KEY_UPDATE_ERROR = 0xE
    AEAD_LIMIT_REACHED = 0xF
    NO_VIABLE_PATH = 0x10
    H3_DATAGRAM_ERROR = 0x33
    H3_NO_ERROR = 0x100
    H3_GENERAL_PROTOCOL_ERROR = 0x101
    H3_INTERNAL_ERROR = 0x102
    H3_STREAM_CREATION_ERROR = 0x103
    H3_CLOSED_CRITICAL_STREAM = 0x104
    H3_FRAME_UNEXPECTED = 0x105
    H3_FRAME_ERROR = 0x106
    H3_EXCESSIVE_LOAD = 0x107
    H3_ID_ERROR = 0x108
    H3_SETTINGS_ERROR = 0x109
    H3_MISSING_SETTINGS = 0x10A
    H3_REQUEST_REJECTED = 0x10B
    H3_REQUEST_CANCELLED = 0x10C
    H3_REQUEST_INCOMPLETE = 0x10D
    H3_MESSAGE_ERROR = 0x10E
    H3_CONNECT_ERROR = 0x10F
    H3_VERSION_FALLBACK = 0x110
    WT_SESSION_GONE = 0x170D7B68
    WT_BUFFERED_STREAM_REJECTED = 0x3994BD84
    WT_FLOW_CONTROL_ERROR = 0x045D4487
    WT_APPLICATION_ERROR_FIRST = 0x52E4A40FA8DB
    WT_APPLICATION_ERROR_LAST = 0x52E5AC983162
    QPACK_DECOMPRESSION_FAILED = 0x200
    QPACK_ENCODER_STREAM_ERROR = 0x201
    QPACK_DECODER_STREAM_ERROR = 0x202
    APP_CONNECTION_TIMEOUT = 0x1000
    APP_AUTHENTICATION_FAILED = 0x1001
    APP_PERMISSION_DENIED = 0x1002
    APP_RESOURCE_EXHAUSTED = 0x1003
    APP_INVALID_REQUEST = 0x1004
    APP_SERVICE_UNAVAILABLE = 0x1005
