"""
Stream Pydantic model for Nginx Proxy Manager.

This module defines the Stream model representing TCP/UDP stream forwarding
configurations in Nginx Proxy Manager.
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class Stream(BaseModel):
    """
    Pydantic model for Nginx Proxy Manager Stream.

    A stream represents a TCP/UDP port forwarding configuration that routes
    traffic from an incoming port to a backend host and port.

    Attributes:
        id: Unique identifier (auto-generated by NPM)
        created_on: Timestamp when the stream was created
        modified_on: Timestamp when the stream was last modified
        incoming_port: Port to listen on (1-65535)
        forwarding_host: Backend host to forward traffic to (IP or hostname)
        forwarding_port: Backend port to forward traffic to (1-65535)
        tcp_forwarding: Whether to enable TCP forwarding
        udp_forwarding: Whether to enable UDP forwarding
        certificate_id: ID of SSL certificate for SSL termination (optional)
        enabled: Whether this stream is enabled
        meta: Additional metadata as key-value pairs

    Example:
        >>> # TCP stream for MySQL
        >>> stream = Stream(
        ...     incoming_port=3306,
        ...     forwarding_host="mysql.internal",
        ...     forwarding_port=3306,
        ...     tcp_forwarding=True,
        ...     udp_forwarding=False,
        ... )
        >>> stream.incoming_port
        3306

        >>> # DNS stream (TCP + UDP)
        >>> stream = Stream(
        ...     incoming_port=53,
        ...     forwarding_host="dns.internal",
        ...     forwarding_port=53,
        ...     tcp_forwarding=True,
        ...     udp_forwarding=True,
        ... )
        >>> stream.tcp_forwarding and stream.udp_forwarding
        True

        >>> # HTTPS stream with SSL termination
        >>> stream = Stream(
        ...     incoming_port=443,
        ...     forwarding_host="backend.internal",
        ...     forwarding_port=80,
        ...     tcp_forwarding=True,
        ...     udp_forwarding=False,
        ...     certificate_id=10,
        ... )
        >>> stream.certificate_id
        10
    """

    # Identifiers
    id: int | None = None
    created_on: datetime | None = None
    modified_on: datetime | None = None

    # Port configuration
    incoming_port: int = Field(..., ge=1, le=65535)
    forwarding_port: int = Field(..., ge=1, le=65535)

    # Forwarding configuration
    forwarding_host: str

    # Protocol configuration
    tcp_forwarding: bool
    udp_forwarding: bool

    # SSL configuration
    certificate_id: int | None = None

    # Status
    enabled: bool = True

    # Metadata
    meta: dict[str, Any] = Field(default_factory=dict)

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "incoming_port": 3306,
                    "forwarding_host": "mysql.internal",
                    "forwarding_port": 3306,
                    "tcp_forwarding": True,
                    "udp_forwarding": False,
                },
                {
                    "incoming_port": 53,
                    "forwarding_host": "dns.internal",
                    "forwarding_port": 53,
                    "tcp_forwarding": True,
                    "udp_forwarding": True,
                },
                {
                    "incoming_port": 443,
                    "forwarding_host": "backend.internal",
                    "forwarding_port": 80,
                    "tcp_forwarding": True,
                    "udp_forwarding": False,
                    "certificate_id": 10,
                },
            ]
        }
    }
