# coding: utf-8

"""
    ibl-data-manager

    API for iblai

    The version of the OpenAPI document: 4.202.1-ai-plus
    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, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from iblai.models.o_auth_service import OAuthService
from iblai.models.oauth_credentials_masked import OauthCredentialsMasked
from typing import Optional, Set
from typing_extensions import Self

class OAuthProvider(BaseModel):
    """
    Read-only serializer for OAuth providers, including the services they expose.
    """ # noqa: E501
    id: StrictInt
    name: StrictStr
    description: StrictStr
    url: StrictStr
    image: Optional[StrictStr]
    scope_map: Optional[Any]
    auth_url: StrictStr
    token_url: StrictStr
    is_enabled: StrictBool
    created_at: datetime
    updated_at: datetime
    services: List[OAuthService]
    credentials: OauthCredentialsMasked
    use_basic_auth: StrictBool = Field(description="Whether to use basic authentication for the OAuth provider. By default client id and client secret are sent to the Authorization server as post data.")
    __properties: ClassVar[List[str]] = ["id", "name", "description", "url", "image", "scope_map", "auth_url", "token_url", "is_enabled", "created_at", "updated_at", "services", "credentials", "use_basic_auth"]

    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 OAuthProvider 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.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "id",
            "name",
            "description",
            "url",
            "image",
            "scope_map",
            "auth_url",
            "token_url",
            "is_enabled",
            "created_at",
            "updated_at",
            "services",
            "credentials",
            "use_basic_auth",
        ])

        _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 services (list)
        _items = []
        if self.services:
            for _item_services in self.services:
                if _item_services:
                    _items.append(_item_services.to_dict())
            _dict['services'] = _items
        # override the default output from pydantic by calling `to_dict()` of credentials
        if self.credentials:
            _dict['credentials'] = self.credentials.to_dict()
        # set to None if image (nullable) is None
        # and model_fields_set contains the field
        if self.image is None and "image" in self.model_fields_set:
            _dict['image'] = None

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

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of OAuthProvider 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"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "url": obj.get("url"),
            "image": obj.get("image"),
            "scope_map": obj.get("scope_map"),
            "auth_url": obj.get("auth_url"),
            "token_url": obj.get("token_url"),
            "is_enabled": obj.get("is_enabled"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at"),
            "services": [OAuthService.from_dict(_item) for _item in obj["services"]] if obj.get("services") is not None else None,
            "credentials": OauthCredentialsMasked.from_dict(obj["credentials"]) if obj.get("credentials") is not None else None,
            "use_basic_auth": obj.get("use_basic_auth")
        })
        return _obj


