"""
Certificate Pydantic model for Nginx Proxy Manager.

This module defines the Certificate model representing SSL/TLS certificates
in Nginx Proxy Manager. It supports both Let's Encrypt automatic certificates
and custom uploaded certificates.
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field, field_validator


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

    A certificate represents an SSL/TLS certificate that can be used to secure
    proxy hosts. Certificates can be automatically generated via Let's Encrypt
    or manually uploaded as custom certificates.

    Attributes:
        id: Unique identifier (auto-generated by NPM)
        created_on: Timestamp when the certificate was created
        modified_on: Timestamp when the certificate was last modified
        provider: Certificate provider ('letsencrypt' or 'custom')
        nice_name: Human-readable name for the certificate
        domain_names: List of domain names covered by this certificate (max 100)
        expires_on: Expiration date/time of the certificate (read-only)
        meta: Additional metadata including Let's Encrypt settings, DNS challenge config

    Example:
        >>> # Let's Encrypt certificate
        >>> cert = Certificate(
        ...     provider="letsencrypt",
        ...     nice_name="Production SSL",
        ...     domain_names=["example.com", "www.example.com"],
        ...     meta={"letsencrypt_email": "admin@example.com"},
        ... )
        >>> cert.provider
        'letsencrypt'

        >>> # Custom certificate
        >>> cert = Certificate(
        ...     provider="custom",
        ...     nice_name="Wildcard Certificate",
        ...     domain_names=["*.example.com"],
        ... )
        >>> cert.provider
        'custom'
    """

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

    # Provider configuration
    provider: str = Field(..., pattern="^(letsencrypt|custom)$")

    # Certificate details
    nice_name: str
    domain_names: list[str] = Field(..., min_length=1, max_length=100)

    # Expiration (read-only, set by NPM)
    expires_on: datetime | None = None

    # Metadata (Let's Encrypt settings, DNS challenge config, etc.)
    meta: dict[str, Any] = Field(default_factory=dict)

    @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": [
                {
                    "provider": "letsencrypt",
                    "nice_name": "Production Certificate",
                    "domain_names": ["example.com", "www.example.com"],
                    "meta": {
                        "letsencrypt_email": "admin@example.com",
                        "dns_challenge": True,
                        "dns_provider": "cloudflare",
                    },
                },
                {
                    "provider": "custom",
                    "nice_name": "Wildcard Certificate",
                    "domain_names": ["*.example.com"],
                },
            ]
        }
    }
