# coding: utf-8

"""
    Pulp 3 API

    Fetch, Upload, Organize, and Distribute Software Packages

    The version of the OpenAPI document: v3
    Contact: pulp-list@redhat.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictBytes, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional, Tuple, Union
from typing_extensions import Annotated
from pulpcore.client.pulp_ansible.models.namespace_link import NamespaceLink
from typing import Optional, Set
from typing_extensions import Self

class AnsibleAnsibleNamespaceMetadata(BaseModel):
    """
    A serializer for Namespaces.
    """ # noqa: E501
    name: Annotated[str, Field(min_length=3, strict=True, max_length=64)] = Field(description="Required named, only accepts lowercase, numbers and underscores.")
    company: Optional[Annotated[str, Field(strict=True, max_length=64)]] = Field(default=None, description="Optional namespace company owner.")
    email: Optional[Annotated[str, Field(strict=True, max_length=256)]] = Field(default=None, description="Optional namespace contact email.")
    description: Optional[Annotated[str, Field(strict=True, max_length=256)]] = Field(default=None, description="Optional short description.")
    resources: Optional[StrictStr] = Field(default=None, description="Optional resource page in markdown format.")
    links: Optional[List[NamespaceLink]] = Field(default=None, description="Labeled related links.")
    avatar: Optional[Union[StrictBytes, StrictStr, Tuple[StrictStr, StrictBytes]]] = Field(default=None, description="Optional avatar image for Namespace")
    __properties: ClassVar[List[str]] = ["name", "company", "email", "description", "resources", "links", "avatar"]

    @field_validator('name')
    def name_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^(?!.*__)[a-z]+[0-9a-z_]*$", value):
            raise ValueError(r"must validate the regular expression /^(?!.*__)[a-z]+[0-9a-z_]*$/")
        return value

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of AnsibleAnsibleNamespaceMetadata from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in links (list)
        _items = []
        if self.links:
            for _item_links in self.links:
                if _item_links:
                    _items.append(_item_links.to_dict())
            _dict['links'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of AnsibleAnsibleNamespaceMetadata from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate(obj)
        return _obj


