from __future__ import annotations

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

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

from ..models.knowledge_base_role import KnowledgeBaseRole
from ..types import UNSET, Unset

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


@_attrs_define
class KnowledgeBaseMembershipResponse:
    """Membership enriched with user details.

    Attributes:
        field_id (str):
        kb_id (str):
        user_id (str):
        role (KnowledgeBaseRole): Role within a knowledge base / workspace.
        created_at (datetime.datetime | Unset):
        updated_at (datetime.datetime | Unset):
        email (None | str | Unset):
        is_owner (bool | Unset):  Default: False.
    """

    field_id: str
    kb_id: str
    user_id: str
    role: KnowledgeBaseRole
    created_at: datetime.datetime | Unset = UNSET
    updated_at: datetime.datetime | Unset = UNSET
    email: None | str | Unset = UNSET
    is_owner: bool | Unset = False
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        field_id = self.field_id

        kb_id = self.kb_id

        user_id = self.user_id

        role = self.role.value

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

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

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

        is_owner = self.is_owner

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "_id": field_id,
                "kb_id": kb_id,
                "user_id": user_id,
                "role": role,
            }
        )
        if created_at is not UNSET:
            field_dict["created_at"] = created_at
        if updated_at is not UNSET:
            field_dict["updated_at"] = updated_at
        if email is not UNSET:
            field_dict["email"] = email
        if is_owner is not UNSET:
            field_dict["is_owner"] = is_owner

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        field_id = d.pop("_id")

        kb_id = d.pop("kb_id")

        user_id = d.pop("user_id")

        role = KnowledgeBaseRole(d.pop("role"))

        _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)

        _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)

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

        email = _parse_email(d.pop("email", UNSET))

        is_owner = d.pop("is_owner", UNSET)

        knowledge_base_membership_response = cls(
            field_id=field_id,
            kb_id=kb_id,
            user_id=user_id,
            role=role,
            created_at=created_at,
            updated_at=updated_at,
            email=email,
            is_owner=is_owner,
        )

        knowledge_base_membership_response.additional_properties = d
        return knowledge_base_membership_response

    @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
