# coding: utf-8

"""
    Moodle Webservice API

    Auto-generated OpenAPI spec for Moodle's Webservice API.

    The version of the OpenAPI document: 5.0.2 (Build: 20250811)
    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, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from poodle_async_full.models.tool_dataprivacy_tree_extra_branches_response_branches_inner_branches_inner import ToolDataprivacyTreeExtraBranchesResponseBranchesInnerBranchesInner
from typing import Optional, Set
from typing_extensions import Self

class ToolDataprivacyTreeExtraBranchesResponseBranchesInner(BaseModel):
    """
    Node structure
    """ # noqa: E501
    branches: Optional[List[ToolDataprivacyTreeExtraBranchesResponseBranchesInnerBranchesInner]] = Field(default=None, description="Children node structure")
    contextid: Optional[StrictInt] = Field(description="The node contextid")
    contextlevel: Optional[StrictInt] = Field(description="The node contextlevel")
    expandcontextid: Optional[StrictInt] = Field(description="The contextid this node expands")
    expanded: Optional[StrictInt] = Field(description="Is it expanded")
    expandelement: Optional[StrictStr] = Field(description="What element is this node expanded to")
    text: Optional[StrictStr] = Field(description="The node text")
    __properties: ClassVar[List[str]] = ["branches", "contextid", "contextlevel", "expandcontextid", "expanded", "expandelement", "text"]

    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 ToolDataprivacyTreeExtraBranchesResponseBranchesInner 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 branches (list)
        _items = []
        if self.branches:
            for _item_branches in self.branches:
                if _item_branches:
                    _items.append(_item_branches.to_dict())
            _dict['branches'] = _items
        # set to None if contextid (nullable) is None
        # and model_fields_set contains the field
        if self.contextid is None and "contextid" in self.model_fields_set:
            _dict['contextid'] = None

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "branches": [ToolDataprivacyTreeExtraBranchesResponseBranchesInnerBranchesInner.from_dict(_item) for _item in obj["branches"]] if obj.get("branches") is not None else None,
            "contextid": obj.get("contextid"),
            "contextlevel": obj.get("contextlevel"),
            "expandcontextid": obj.get("expandcontextid"),
            "expanded": obj.get("expanded"),
            "expandelement": obj.get("expandelement"),
            "text": obj.get("text")
        })
        return _obj


