# coding: utf-8

"""
    Mentortools Access Server

    Mentortools service for Access Packages with lazy access resolution

    The version of the OpenAPI document: 0.1.0
    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, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from access_server_connector_v2.models.lock_reason import LockReason
from access_server_connector_v2.models.resource_type import ResourceType
from access_server_connector_v2.models.state import State
from access_server_connector_v2.models.winning_layer import WinningLayer
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class NodeAccessOut(BaseModel):
    """
    NodeAccessOut
    """ # noqa: E501
    resource_type: ResourceType
    resource_id: StrictInt
    state: State
    allowed: StrictBool
    available_at: Optional[StrictInt]
    expires_at: Optional[StrictInt]
    lock_reason: Optional[LockReason]
    winning_layer: Optional[WinningLayer]
    reasons: List[StrictInt]
    expired_message: Optional[StrictStr] = None
    children: Optional[List[NodeAccessOut]] = None
    __properties: ClassVar[List[str]] = ["resource_type", "resource_id", "state", "allowed", "available_at", "expires_at", "lock_reason", "winning_layer", "reasons", "expired_message", "children"]

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "resource_type": obj.get("resource_type"),
            "resource_id": obj.get("resource_id"),
            "state": obj.get("state"),
            "allowed": obj.get("allowed"),
            "available_at": obj.get("available_at"),
            "expires_at": obj.get("expires_at"),
            "lock_reason": obj.get("lock_reason"),
            "winning_layer": obj.get("winning_layer"),
            "reasons": obj.get("reasons"),
            "expired_message": obj.get("expired_message"),
            "children": [NodeAccessOut.from_dict(_item) for _item in obj["children"]] if obj.get("children") is not None else None
        })
        return _obj

# TODO: Rewrite to not use raise_errors
NodeAccessOut.model_rebuild(raise_errors=False)

