# coding: utf-8

"""
    Identity Provider - Admin API

    Admin API endpoints (public tenant only)

    The version of the OpenAPI document: 1.0.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 datetime import datetime
from pydantic import BaseModel, ConfigDict, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from binoauth.admin.models.provider_type import ProviderType
from typing import Optional, Set
from typing_extensions import Self

class ExternalAccount(BaseModel):
    """
    ExternalAccount
    """ # noqa: E501
    id: Optional[StrictStr] = None
    user_id: StrictStr
    provider: ProviderType
    provider_user_id: StrictStr
    provider_email: StrictStr
    access_token: Optional[StrictStr] = None
    refresh_token: Optional[StrictStr] = None
    token_expires_at: Optional[datetime] = None
    profile_data: Dict[str, Any]
    last_used: Optional[datetime] = None
    login_count: Optional[StrictInt] = 0
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    __properties: ClassVar[List[str]] = ["id", "user_id", "provider", "provider_user_id", "provider_email", "access_token", "refresh_token", "token_expires_at", "profile_data", "last_used", "login_count", "created_at", "updated_at"]

    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 ExternalAccount 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,
        )
        # set to None if access_token (nullable) is None
        # and model_fields_set contains the field
        if self.access_token is None and "access_token" in self.model_fields_set:
            _dict['access_token'] = None

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "user_id": obj.get("user_id"),
            "provider": obj.get("provider"),
            "provider_user_id": obj.get("provider_user_id"),
            "provider_email": obj.get("provider_email"),
            "access_token": obj.get("access_token"),
            "refresh_token": obj.get("refresh_token"),
            "token_expires_at": obj.get("token_expires_at"),
            "profile_data": obj.get("profile_data"),
            "last_used": obj.get("last_used"),
            "login_count": obj.get("login_count") if obj.get("login_count") is not None else 0,
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at")
        })
        return _obj
