# coding: utf-8

"""
    Kanidm

    API for interacting with the Kanidm system. This is a work in progress.

    The version of the OpenAPI document: 1.9.0-dev
    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 datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from uuid import UUID
from kanidm_openapi_client.models.uat_purpose import UatPurpose
from kanidm_openapi_client.models.ui_hint import UiHint
from typing import Optional, Set
from typing_extensions import Self

class UserAuthToken(BaseModel):
    """
    The currently authenticated user, and any required metadata for them to properly authorise them. This is similar in nature to oauth and the krb PAC/PAD structures. This information is transparent to clients and CAN be parsed by them!  This structure and how it works will *very much* change over time from this point onward! This means on updates, that sessions will invalidate in many cases.
    """ # noqa: E501
    session_id: UUID
    issued_at: datetime
    expiry: Optional[datetime] = Field(default=None, description="If none, there is no expiry, and this is always valid. If there is an expiry, check that the current time < expiry.")
    purpose: UatPurpose
    uuid: UUID
    displayname: StrictStr
    spn: StrictStr
    mail_primary: Optional[StrictStr] = None
    ui_hints: List[UiHint]
    limit_search_max_results: Optional[Annotated[int, Field(strict=True, ge=0)]] = None
    limit_search_max_filter_test: Optional[Annotated[int, Field(strict=True, ge=0)]] = None
    __properties: ClassVar[List[str]] = ["session_id", "issued_at", "expiry", "purpose", "uuid", "displayname", "spn", "mail_primary", "ui_hints", "limit_search_max_results", "limit_search_max_filter_test"]

    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"""
        return self.model_dump_json(by_alias=True, exclude_unset=True)

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of UserAuthToken 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 purpose
        if self.purpose:
            _dict['purpose'] = self.purpose.to_dict()
        # set to None if expiry (nullable) is None
        # and model_fields_set contains the field
        if self.expiry is None and "expiry" in self.model_fields_set:
            _dict['expiry'] = None

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "session_id": obj.get("session_id"),
            "issued_at": obj.get("issued_at"),
            "expiry": obj.get("expiry"),
            "purpose": UatPurpose.from_dict(obj["purpose"]) if obj.get("purpose") is not None else None,
            "uuid": obj.get("uuid"),
            "displayname": obj.get("displayname"),
            "spn": obj.get("spn"),
            "mail_primary": obj.get("mail_primary"),
            "ui_hints": obj.get("ui_hints"),
            "limit_search_max_results": obj.get("limit_search_max_results"),
            "limit_search_max_filter_test": obj.get("limit_search_max_filter_test")
        })
        return _obj


