# coding: utf-8

"""
    Ringer Business API

    This API provides comprehensive endpoints for the Ringer Business system. It includes functionality for managing customer accounts, addresses, sites,  phone numbers, payments, users, and other business resources.  Operations typically require authentication and appropriate permissions. 

    The version of the OpenAPI document: 1.0.4
    Contact: support@ringer.tel
    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 ringer_business.models.site_destination import SiteDestination
from ringer_business.models.template_feature_attribute import TemplateFeatureAttribute
from typing import Optional, Set
from typing_extensions import Self

class TemplateFeature(BaseModel):
    """
    Represents a configurable feature (switchable) within a template, forming a tree structure.
    """ # noqa: E501
    switchable: StrictStr
    position: StrictInt
    attrs: Optional[List[TemplateFeatureAttribute]] = None
    destinations: Optional[List[TemplateFeature]] = None
    feature_type: Optional[StrictStr] = Field(default=None, description="Type of feature (e.g., device, extension, menu, voicemail).")
    failover: Optional[SiteDestination] = Field(default=None, description="Optional. System code for the failover destination if this feature is unavailable.")
    target_type: Optional[StrictStr] = Field(default=None, description="The type of the target object for the feature (e.g. Device, RingGroup).")
    __properties: ClassVar[List[str]] = ["switchable", "position", "attrs", "destinations", "feature_type", "failover", "target_type"]

    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 TemplateFeature 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 attrs (list)
        _items = []
        if self.attrs:
            for _item_attrs in self.attrs:
                if _item_attrs:
                    _items.append(_item_attrs.to_dict())
            _dict['attrs'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in destinations (list)
        _items = []
        if self.destinations:
            for _item_destinations in self.destinations:
                if _item_destinations:
                    _items.append(_item_destinations.to_dict())
            _dict['destinations'] = _items
        # override the default output from pydantic by calling `to_dict()` of failover
        if self.failover:
            _dict['failover'] = self.failover.to_dict()
        # set to None if destinations (nullable) is None
        # and model_fields_set contains the field
        if self.destinations is None and "destinations" in self.model_fields_set:
            _dict['destinations'] = None

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

        return _dict

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

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

        _obj = cls.model_validate({
            "switchable": obj.get("switchable"),
            "position": obj.get("position"),
            "attrs": [TemplateFeatureAttribute.from_dict(_item) for _item in obj["attrs"]] if obj.get("attrs") is not None else None,
            "destinations": [TemplateFeature.from_dict(_item) for _item in obj["destinations"]] if obj.get("destinations") is not None else None,
            "feature_type": obj.get("feature_type"),
            "failover": SiteDestination.from_dict(obj["failover"]) if obj.get("failover") is not None else None,
            "target_type": obj.get("target_type")
        })
        return _obj

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

