from dataclasses import dataclass, field
import secrets

@dataclass(frozen=True)
class CertificateMetadata():
    """Certificate Metadata"""
    common_name: str = field(
        metadata={
            "description": "The common name assigned to the certificate."
        }
    )
    password: str = field(
        metadata={
            "description": "Autogenerated, secure, password for the certificate."
        },
        init=False,
        repr=False,
        default_factory=lambda: secrets.token_urlsafe(32)
    )

    def __post_init__(self):
        # Validate common name
        if not self.common_name.strip():
            raise ValueError("Common name cannot be empty.")

        if len(self.common_name) > 64:
            raise ValueError(
                f"Common name must not exceed 64 characters, but got {len(self.common_name)} characters."
            )

        # Validate password
        if len(self.password) < 16:
            raise ValueError(
                f"Password must be at least 16 characters long, "
                f"but got {len(self.password)} characters."
            )
