# coding: utf-8

"""
    Lookup/Ibis web service API

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

    The version of the OpenAPI document: 1.2
    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, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from identitylib.lookup_client.models.attribute import Attribute
from typing import Optional, Set
from typing_extensions import Self

class Institution(BaseModel):
    """
    Class representing an institution returned by the web service API.
    """ # noqa: E501
    cancelled: Optional[StrictBool] = Field(default=None, description="Flag indicating if the institution is cancelled.")
    instid: Optional[StrictStr] = Field(default=None, description="The institution's unique ID (e.g., \"CS\").")
    name: Optional[StrictStr] = Field(default=None, description="The institution's name.")
    acronym: Optional[StrictStr] = Field(default=None, description="The institution's acronym, if set (e.g., \"UCS\").")
    attributes: Optional[List[Attribute]] = Field(default=None, description="A list of the institution's attributes. This will only be populated if the `fetch` parameter includes the `\"all_attrs\"` option, or any specific attribute schemes such as `\"email\"` or `\"address\"`, or the special pseudo-attribute scheme `\"phone_numbers\"`.")
    contact_rows: Optional[List[ContactRow]] = Field(default=None, description="A list of the institution's contact rows. This will only be populated if the `fetch` parameter includes the `\"contact_rows\"` option.", alias="contactRows")
    members: Optional[List[Person]] = Field(default=None, description="A list of the institution's members. This will only be populated if the `fetch` parameter includes the `\"all_members\"` option.")
    parent_insts: Optional[List[Institution]] = Field(default=None, description="A list of the institution's parent institutions. This will only be populated if the `fetch` parameter includes the `\"parent_insts\"` option.  NOTE: Currently all institutions have one parent, but in the future institutions may have multiple parents.", alias="parentInsts")
    child_insts: Optional[List[Institution]] = Field(default=None, description="A list of the institution's child institutions. This will only be populated if the `fetch` parameter includes the `\"child_insts\"` option.", alias="childInsts")
    groups: Optional[List[Group]] = Field(default=None, description="A list of all the groups that belong to the institution. This will only be populated if the `fetch` parameter includes the `\"inst_groups\"` option.")
    members_groups: Optional[List[Group]] = Field(default=None, description="A list of the groups that form the institution's membership. This will only be populated if the `fetch` parameter includes the `\"members_groups\"` option.", alias="membersGroups")
    managed_by_groups: Optional[List[Group]] = Field(default=None, description="A list of the groups that manage this institution. This will only be populated if the `fetch` parameter includes the `\"managed_by_groups\"` option.", alias="managedByGroups")
    id: Optional[StrictStr] = Field(default=None, description="An ID that can uniquely identify this institution within the returned XML/JSON document. This is only used in the flattened XML/JSON representation (if the \"flatten\" parameter is specified).")
    ref: Optional[StrictStr] = Field(default=None, description="A reference (by id) to an institution element in the XML/JSON document. This is only used in the flattened XML/JSON representation (if the \"flatten\" parameter is specified).")
    __properties: ClassVar[List[str]] = ["cancelled", "instid", "name", "acronym", "attributes", "contactRows", "members", "parentInsts", "childInsts", "groups", "membersGroups", "managedByGroups", "id", "ref"]

    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 Institution 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 attributes (list)
        _items = []
        if self.attributes:
            for _item in self.attributes:
                if _item:
                    _items.append(_item.to_dict())
            _dict['attributes'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in contact_rows (list)
        _items = []
        if self.contact_rows:
            for _item in self.contact_rows:
                if _item:
                    _items.append(_item.to_dict())
            _dict['contactRows'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in members (list)
        _items = []
        if self.members:
            for _item in self.members:
                if _item:
                    _items.append(_item.to_dict())
            _dict['members'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in parent_insts (list)
        _items = []
        if self.parent_insts:
            for _item in self.parent_insts:
                if _item:
                    _items.append(_item.to_dict())
            _dict['parentInsts'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in child_insts (list)
        _items = []
        if self.child_insts:
            for _item in self.child_insts:
                if _item:
                    _items.append(_item.to_dict())
            _dict['childInsts'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in groups (list)
        _items = []
        if self.groups:
            for _item in self.groups:
                if _item:
                    _items.append(_item.to_dict())
            _dict['groups'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in members_groups (list)
        _items = []
        if self.members_groups:
            for _item in self.members_groups:
                if _item:
                    _items.append(_item.to_dict())
            _dict['membersGroups'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in managed_by_groups (list)
        _items = []
        if self.managed_by_groups:
            for _item in self.managed_by_groups:
                if _item:
                    _items.append(_item.to_dict())
            _dict['managedByGroups'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "cancelled": obj.get("cancelled"),
            "instid": obj.get("instid"),
            "name": obj.get("name"),
            "acronym": obj.get("acronym"),
            "attributes": [Attribute.from_dict(_item) for _item in obj["attributes"]] if obj.get("attributes") is not None else None,
            "contactRows": [ContactRow.from_dict(_item) for _item in obj["contactRows"]] if obj.get("contactRows") is not None else None,
            "members": [Person.from_dict(_item) for _item in obj["members"]] if obj.get("members") is not None else None,
            "parentInsts": [Institution.from_dict(_item) for _item in obj["parentInsts"]] if obj.get("parentInsts") is not None else None,
            "childInsts": [Institution.from_dict(_item) for _item in obj["childInsts"]] if obj.get("childInsts") is not None else None,
            "groups": [Group.from_dict(_item) for _item in obj["groups"]] if obj.get("groups") is not None else None,
            "membersGroups": [Group.from_dict(_item) for _item in obj["membersGroups"]] if obj.get("membersGroups") is not None else None,
            "managedByGroups": [Group.from_dict(_item) for _item in obj["managedByGroups"]] if obj.get("managedByGroups") is not None else None,
            "id": obj.get("id"),
            "ref": obj.get("ref")
        })
        return _obj

from identitylib.lookup_client.models.contact_row import ContactRow
from identitylib.lookup_client.models.group import Group
from identitylib.lookup_client.models.person import Person
# TODO: Rewrite to not use raise_errors
Institution.model_rebuild(raise_errors=False)

