# 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 typing_extensions import Annotated
from uuid import UUID
from iblai.models.auth_type_enum import AuthTypeEnum
from iblai.models.mcp_server_connection_scope_enum import MCPServerConnectionScopeEnum
from typing import Optional, Set
from typing_extensions import Self

class PatchedMCPServerConnection(BaseModel):
    """
    Serializer for managing `MCPServerConnection` records via the API.  The serializer enforces the business rules for combining scope, platform, and authentication configuration while exposing a masked view of any stored credentials.
    """ # noqa: E501
    id: Optional[StrictInt] = None
    server: Optional[StrictInt] = None
    server_name: Optional[StrictStr] = None
    scope: Optional[MCPServerConnectionScopeEnum] = Field(default=None, description="Whether this connection is scoped to a user or shared across a platform.  * `user` - User * `mentor` - Mentor * `platform` - Platform")
    auth_type: Optional[AuthTypeEnum] = None
    platform: Optional[StrictInt] = None
    platform_key: Optional[StrictStr] = None
    user: Optional[StrictStr] = Field(default=None, description="edX username")
    mentor: Optional[UUID] = None
    connected_service: Optional[StrictInt] = None
    connected_service_summary: Optional[Dict[str, Any]] = None
    credentials: Optional[StrictStr] = Field(default=None, description="Static credential/token value for token-based authentication.")
    authorization_scheme: Optional[Annotated[str, Field(strict=True, max_length=50)]] = Field(default=None, description="Authorization scheme to prefix tokens with (e.g. 'Bearer').")
    extra_headers: Optional[Any] = Field(default=None, description="Additional headers to include when connecting to the MCP server.")
    is_active: Optional[StrictBool] = None
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None
    __properties: ClassVar[List[str]] = ["id", "server", "server_name", "scope", "auth_type", "platform", "platform_key", "user", "mentor", "connected_service", "connected_service_summary", "credentials", "authorization_scheme", "extra_headers", "is_active", "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 PatchedMCPServerConnection 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.
        """
        excluded_fields: Set[str] = set([
            "id",
            "server_name",
            "platform_key",
            "connected_service_summary",
            "created_at",
            "updated_at",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # set to None if platform (nullable) is None
        # and model_fields_set contains the field
        if self.platform is None and "platform" in self.model_fields_set:
            _dict['platform'] = None

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

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

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

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

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

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

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of PatchedMCPServerConnection 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"),
            "server": obj.get("server"),
            "server_name": obj.get("server_name"),
            "scope": obj.get("scope"),
            "auth_type": obj.get("auth_type"),
            "platform": obj.get("platform"),
            "platform_key": obj.get("platform_key"),
            "user": obj.get("user"),
            "mentor": obj.get("mentor"),
            "connected_service": obj.get("connected_service"),
            "connected_service_summary": obj.get("connected_service_summary"),
            "credentials": obj.get("credentials"),
            "authorization_scheme": obj.get("authorization_scheme"),
            "extra_headers": obj.get("extra_headers"),
            "is_active": obj.get("is_active"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at")
        })
        return _obj


