from __future__ import annotations

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

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.types_attachment import TypesAttachment
    from ..models.types_chat_message_reaction import TypesChatMessageReaction


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


@_attrs_define
class TypesChatMessage:
    """
    Attributes:
        field_id (str | Unset):
        attachments (list[TypesAttachment] | Unset):
        author (str | Unset):
        content (str | Unset):
        conversation (str | Unset):
        reactions (list[TypesChatMessageReaction] | Unset):
        sent_at (str | Unset):
    """

    field_id: str | Unset = UNSET
    attachments: list[TypesAttachment] | Unset = UNSET
    author: str | Unset = UNSET
    content: str | Unset = UNSET
    conversation: str | Unset = UNSET
    reactions: list[TypesChatMessageReaction] | Unset = UNSET
    sent_at: str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        attachments: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.attachments, Unset):
            attachments = []
            for attachments_item_data in self.attachments:
                attachments_item = attachments_item_data.to_dict()
                attachments.append(attachments_item)

        author = self.author

        content = self.content

        conversation = self.conversation

        reactions: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.reactions, Unset):
            reactions = []
            for reactions_item_data in self.reactions:
                reactions_item = reactions_item_data.to_dict()
                reactions.append(reactions_item)

        sent_at = self.sent_at

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if field_id is not UNSET:
            field_dict["_id"] = field_id
        if attachments is not UNSET:
            field_dict["attachments"] = attachments
        if author is not UNSET:
            field_dict["author"] = author
        if content is not UNSET:
            field_dict["content"] = content
        if conversation is not UNSET:
            field_dict["conversation"] = conversation
        if reactions is not UNSET:
            field_dict["reactions"] = reactions
        if sent_at is not UNSET:
            field_dict["sent_at"] = sent_at

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.types_attachment import TypesAttachment
        from ..models.types_chat_message_reaction import TypesChatMessageReaction

        d = dict(src_dict)
        field_id = d.pop("_id", UNSET)

        _attachments = d.pop("attachments", UNSET)
        attachments: list[TypesAttachment] | Unset = UNSET
        if _attachments is not UNSET:
            attachments = []
            for attachments_item_data in _attachments:
                attachments_item = TypesAttachment.from_dict(attachments_item_data)

                attachments.append(attachments_item)

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

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

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

        _reactions = d.pop("reactions", UNSET)
        reactions: list[TypesChatMessageReaction] | Unset = UNSET
        if _reactions is not UNSET:
            reactions = []
            for reactions_item_data in _reactions:
                reactions_item = TypesChatMessageReaction.from_dict(reactions_item_data)

                reactions.append(reactions_item)

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

        types_chat_message = cls(
            field_id=field_id,
            attachments=attachments,
            author=author,
            content=content,
            conversation=conversation,
            reactions=reactions,
            sent_at=sent_at,
        )

        types_chat_message.additional_properties = d
        return types_chat_message

    @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
