# coding: utf-8

"""
    Docling Serve

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 
    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, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from yfkm3_services_parse_client.models.base_meta import BaseMeta
from yfkm3_services_parse_client.models.content_layer import ContentLayer
from yfkm3_services_parse_client.models.ref_item import RefItem
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class ListGroup(BaseModel):
    """
    ListGroup.
    """ # noqa: E501
    self_ref: Annotated[str, Field(strict=True)]
    parent: Optional[RefItem] = None
    children: Optional[List[RefItem]] = None
    content_layer: Optional[ContentLayer] = None
    meta: Optional[BaseMeta] = None
    name: Optional[StrictStr] = 'group'
    label: Optional[StrictStr] = 'list'
    additional_properties: Dict[str, Any] = {}
    __properties: ClassVar[List[str]] = ["self_ref", "parent", "children", "content_layer", "meta", "name", "label"]

    @field_validator('self_ref')
    def self_ref_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^#(?:\/([\w-]+)(?:\/(\d+))?)?$", value):
            raise ValueError(r"must validate the regular expression /^#(?:\/([\w-]+)(?:\/(\d+))?)?$/")
        return value

    @field_validator('label')
    def label_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(['list']):
            raise ValueError("must be one of enum values ('list')")
        return value

    model_config = ConfigDict(
        validate_by_name=True,
        validate_by_alias=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"""
        return json.dumps(to_jsonable_python(self.to_dict()))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of ListGroup 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.
        * Fields in `self.additional_properties` are added to the output dict.
        """
        excluded_fields: Set[str] = set([
            "additional_properties",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of parent
        if self.parent:
            _dict['parent'] = self.parent.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in children (list)
        _items = []
        if self.children:
            for _item_children in self.children:
                if _item_children:
                    _items.append(_item_children.to_dict())
            _dict['children'] = _items
        # override the default output from pydantic by calling `to_dict()` of meta
        if self.meta:
            _dict['meta'] = self.meta.to_dict()
        # puts key-value pairs in additional_properties in the top level
        if self.additional_properties is not None:
            for _key, _value in self.additional_properties.items():
                _dict[_key] = _value

        # set to None if parent (nullable) is None
        # and model_fields_set contains the field
        if self.parent is None and "parent" in self.model_fields_set:
            _dict['parent'] = None

        # set to None if meta (nullable) is None
        # and model_fields_set contains the field
        if self.meta is None and "meta" in self.model_fields_set:
            _dict['meta'] = None

        return _dict

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

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

        _obj = cls.model_validate({
            "self_ref": obj.get("self_ref"),
            "parent": RefItem.from_dict(obj["parent"]) if obj.get("parent") is not None else None,
            "children": [RefItem.from_dict(_item) for _item in obj["children"]] if obj.get("children") is not None else None,
            "content_layer": obj.get("content_layer"),
            "meta": BaseMeta.from_dict(obj["meta"]) if obj.get("meta") is not None else None,
            "name": obj.get("name") if obj.get("name") is not None else 'group',
            "label": obj.get("label") if obj.get("label") is not None else 'list'
        })
        # store additional fields in additional_properties
        for _key in obj.keys():
            if _key not in cls.__properties:
                _obj.additional_properties[_key] = obj.get(_key)

        return _obj


