from __future__ import annotations

import datetime
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar, cast

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from dateutil.parser import isoparse

from ..models.mcp_doc_server_type import MCPDocServerType
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.mcp_doc_request_headers_type_0 import MCPDocRequestHeadersType0


T = TypeVar("T", bound="MCPDoc")


@_attrs_define
class MCPDoc:
    """
    Attributes:
        server_name (str): The name of the model context server.
        server_type (MCPDocServerType):
        server_url (str): The URL of the model context server.
        user_id (str):
        field_id (str):
        updated_at (datetime.datetime | Unset):
        created_at (datetime.datetime | Unset):
        description (None | str | Unset): A brief description of the model context server.
        allowed_tools (list[str] | None | Unset): A list of tool names that are permitted to use this model context
            server.
        request_headers (MCPDocRequestHeadersType0 | None | Unset): Optional HTTP headers to include in requests (e.g.,
            Authorization, Content-Type).
        oauth_token (None | str | Unset): Optional OAuth token for authenticating with the model context server.
    """

    server_name: str
    server_type: MCPDocServerType
    server_url: str
    user_id: str
    field_id: str
    updated_at: datetime.datetime | Unset = UNSET
    created_at: datetime.datetime | Unset = UNSET
    description: None | str | Unset = UNSET
    allowed_tools: list[str] | None | Unset = UNSET
    request_headers: MCPDocRequestHeadersType0 | None | Unset = UNSET
    oauth_token: None | str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.mcp_doc_request_headers_type_0 import MCPDocRequestHeadersType0

        server_name = self.server_name

        server_type = self.server_type.value

        server_url = self.server_url

        user_id = self.user_id

        field_id = self.field_id

        updated_at: str | Unset = UNSET
        if not isinstance(self.updated_at, Unset):
            updated_at = self.updated_at.isoformat()

        created_at: str | Unset = UNSET
        if not isinstance(self.created_at, Unset):
            created_at = self.created_at.isoformat()

        description: None | str | Unset
        if isinstance(self.description, Unset):
            description = UNSET
        else:
            description = self.description

        allowed_tools: list[str] | None | Unset
        if isinstance(self.allowed_tools, Unset):
            allowed_tools = UNSET
        elif isinstance(self.allowed_tools, list):
            allowed_tools = self.allowed_tools

        else:
            allowed_tools = self.allowed_tools

        request_headers: dict[str, Any] | None | Unset
        if isinstance(self.request_headers, Unset):
            request_headers = UNSET
        elif isinstance(self.request_headers, MCPDocRequestHeadersType0):
            request_headers = self.request_headers.to_dict()
        else:
            request_headers = self.request_headers

        oauth_token: None | str | Unset
        if isinstance(self.oauth_token, Unset):
            oauth_token = UNSET
        else:
            oauth_token = self.oauth_token

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "server_name": server_name,
                "server_type": server_type,
                "server_url": server_url,
                "user_id": user_id,
                "_id": field_id,
            }
        )
        if updated_at is not UNSET:
            field_dict["updated_at"] = updated_at
        if created_at is not UNSET:
            field_dict["created_at"] = created_at
        if description is not UNSET:
            field_dict["description"] = description
        if allowed_tools is not UNSET:
            field_dict["allowed_tools"] = allowed_tools
        if request_headers is not UNSET:
            field_dict["request_headers"] = request_headers
        if oauth_token is not UNSET:
            field_dict["oauth_token"] = oauth_token

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.mcp_doc_request_headers_type_0 import MCPDocRequestHeadersType0

        d = dict(src_dict)
        server_name = d.pop("server_name")

        server_type = MCPDocServerType(d.pop("server_type"))

        server_url = d.pop("server_url")

        user_id = d.pop("user_id")

        field_id = d.pop("_id")

        _updated_at = d.pop("updated_at", UNSET)
        updated_at: datetime.datetime | Unset
        if isinstance(_updated_at, Unset):
            updated_at = UNSET
        else:
            updated_at = isoparse(_updated_at)

        _created_at = d.pop("created_at", UNSET)
        created_at: datetime.datetime | Unset
        if isinstance(_created_at, Unset):
            created_at = UNSET
        else:
            created_at = isoparse(_created_at)

        def _parse_description(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        description = _parse_description(d.pop("description", UNSET))

        def _parse_allowed_tools(data: object) -> list[str] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                allowed_tools_type_0 = cast(list[str], data)

                return allowed_tools_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[str] | None | Unset, data)

        allowed_tools = _parse_allowed_tools(d.pop("allowed_tools", UNSET))

        def _parse_request_headers(data: object) -> MCPDocRequestHeadersType0 | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                request_headers_type_0 = MCPDocRequestHeadersType0.from_dict(data)

                return request_headers_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(MCPDocRequestHeadersType0 | None | Unset, data)

        request_headers = _parse_request_headers(d.pop("request_headers", UNSET))

        def _parse_oauth_token(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        oauth_token = _parse_oauth_token(d.pop("oauth_token", UNSET))

        mcp_doc = cls(
            server_name=server_name,
            server_type=server_type,
            server_url=server_url,
            user_id=user_id,
            field_id=field_id,
            updated_at=updated_at,
            created_at=created_at,
            description=description,
            allowed_tools=allowed_tools,
            request_headers=request_headers,
            oauth_token=oauth_token,
        )

        mcp_doc.additional_properties = d
        return mcp_doc

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
