"""
Settings Pydantic model for Nginx Proxy Manager.

This module defines the Settings model representing global system
configuration and default settings for Nginx Proxy Manager.
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field, field_validator


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

    Settings represents the global system configuration including
    default values for Let's Encrypt, HTTP/2, HSTS, and other
    system-wide preferences.

    Attributes:
        id: Unique identifier (auto-generated by NPM)
        created_on: Timestamp when settings were created
        modified_on: Timestamp when settings were last modified
        default_letsencrypt_email: Default email for Let's Encrypt certificates
        default_letsencrypt_key_size: Default RSA key size (2048 or 4096)
        default_http2_support: Default HTTP/2 support for new proxy hosts
        default_hsts_enabled: Default HSTS setting for new proxy hosts
        default_block_exploits: Default exploit blocking for new proxy hosts
        disable_ipv6: Whether to disable IPv6 system-wide
        meta: Additional metadata as key-value pairs

    Example:
        >>> # Production settings
        >>> settings = Settings(
        ...     default_letsencrypt_email="admin@example.com",
        ...     default_letsencrypt_key_size=4096,
        ...     default_http2_support=True,
        ...     default_hsts_enabled=True,
        ... )
        >>> settings.default_letsencrypt_key_size
        4096

        >>> # Development settings
        >>> settings = Settings(
        ...     default_http2_support=False,
        ...     default_block_exploits=False,
        ... )
        >>> settings.default_block_exploits
        False
    """

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

    # Let's Encrypt default settings
    default_letsencrypt_email: str | None = None
    default_letsencrypt_key_size: int = 4096

    # Default feature flags for new proxy hosts
    default_http2_support: bool = True
    default_hsts_enabled: bool = False
    default_block_exploits: bool = True

    # System-wide settings
    disable_ipv6: bool = False

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

    @field_validator("default_letsencrypt_key_size")
    @classmethod
    def validate_letsencrypt_key_size(cls, v: int) -> int:
        """
        Validate that Let's Encrypt key size is 2048 or 4096.

        Args:
            v: Key size value

        Returns:
            Validated key size

        Raises:
            ValueError: If key size is not 2048 or 4096
        """
        valid_sizes = {2048, 4096}
        if v not in valid_sizes:
            msg = f"default_letsencrypt_key_size must be one of {valid_sizes}, got {v}"
            raise ValueError(msg)
        return v

    model_config = {
        "json_schema_extra": {
            "examples": [
                {
                    "default_letsencrypt_email": "admin@example.com",
                    "default_letsencrypt_key_size": 4096,
                    "default_http2_support": True,
                    "default_hsts_enabled": True,
                    "default_block_exploits": True,
                },
            ]
        }
    }
