"""
ProxyHost Pydantic model for Nginx Proxy Manager.

This module defines the ProxyHost model representing a reverse proxy configuration
in Nginx Proxy Manager. It includes validation for all fields and supports
serialization/deserialization for API communication.
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field, field_validator


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

    A proxy host represents a reverse proxy configuration that routes traffic
    from one or more domain names to a backend service.

    Attributes:
        id: Unique identifier (auto-generated by NPM)
        created_on: Timestamp when the proxy host was created
        modified_on: Timestamp when the proxy host was last modified
        owner_user_id: ID of the user who owns this proxy host
        domain_names: List of domain names this proxy host responds to
        forward_scheme: Protocol for forwarding (http or https)
        forward_host: Backend host to forward requests to
        forward_port: Backend port to forward requests to (1-65535)
        certificate_id: ID of the SSL certificate (if any)
        ssl_forced: Whether to force SSL (redirect HTTP to HTTPS)
        hsts_enabled: Whether to enable HTTP Strict Transport Security
        hsts_subdomains: Whether to include subdomains in HSTS
        http2_support: Whether to enable HTTP/2 support
        allow_websocket_upgrade: Whether to allow WebSocket upgrades
        caching_enabled: Whether to enable caching
        block_exploits: Whether to block common exploits
        access_list_id: ID of the access list (if any)
        advanced_config: Custom Nginx configuration
        enabled: Whether this proxy host is enabled
        meta: Additional metadata as key-value pairs
        locations: List of location-based routing rules

    Example:
        >>> proxy_host = ProxyHost(
        ...     domain_names=["example.com", "www.example.com"],
        ...     forward_scheme="https",
        ...     forward_host="backend.internal",
        ...     forward_port=443,
        ...     ssl_forced=True,
        ... )
        >>> proxy_host.enabled
        True
    """

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

    # Domain configuration
    domain_names: list[str] = Field(..., min_length=1)

    # Forwarding configuration
    forward_scheme: str = Field(..., pattern="^(http|https)$")
    forward_host: str
    forward_port: int = Field(..., ge=1, le=65535)

    # SSL/TLS configuration
    certificate_id: int | None = None
    ssl_forced: bool = False
    hsts_enabled: bool = False
    hsts_subdomains: bool = False

    # Feature flags
    http2_support: bool = False
    allow_websocket_upgrade: bool = False
    caching_enabled: bool = False
    block_exploits: bool = True

    # Access control
    access_list_id: int | None = None

    # Advanced configuration
    advanced_config: str | None = None

    # Status
    enabled: bool = True

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

    # Locations (location-based routing rules)
    locations: list[dict[str, Any]] = Field(default_factory=list)

    @field_validator("domain_names")
    @classmethod
    def validate_domain_names(cls, v: list[str]) -> list[str]:
        """
        Validate that domain_names is not empty.

        Args:
            v: List of domain names

        Returns:
            Validated list of domain names

        Raises:
            ValueError: If domain_names is empty
        """
        if not v:
            msg = "domain_names cannot be empty"
            raise ValueError(msg)
        return v

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "domain_names": ["example.com", "www.example.com"],
                    "forward_scheme": "https",
                    "forward_host": "backend.internal",
                    "forward_port": 443,
                    "certificate_id": 10,
                    "ssl_forced": True,
                    "hsts_enabled": True,
                    "enabled": True,
                }
            ]
        }
    }
