import datetime
from typing import (
    TYPE_CHECKING,
    Any,
    BinaryIO,
    Dict,
    List,
    Optional,
    TextIO,
    Tuple,
    Type,
    TypeVar,
    Union,
    cast,
)

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.base_chat_output import BaseChatOutput
    from ..models.competion_usage import CompetionUsage
    from ..models.doc_reference import DocReference


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


@_attrs_define
class MessageChatResponse:
    """
    Attributes:
        choices (List['BaseChatOutput']): The deltas generated by the model
        jobs (List[str]): Background job IDs that may have been spawned from this interaction.
        docs (List['DocReference']): Documents referenced for this request (for citation purposes).
        created_at (datetime.datetime): When this resource was created as UTC date-time
        id (str):
        usage (Union[Unset, CompetionUsage]): Usage statistics for the completion request
    """

    choices: List["BaseChatOutput"]
    jobs: List[str]
    docs: List["DocReference"]
    created_at: datetime.datetime
    id: str
    usage: Union[Unset, "CompetionUsage"] = UNSET
    additional_properties: Dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        from ..models.base_chat_output import BaseChatOutput
        from ..models.competion_usage import CompetionUsage
        from ..models.doc_reference import DocReference

        choices = []
        for choices_item_data in self.choices:
            choices_item: Dict[str, Any]
            if isinstance(choices_item_data, BaseChatOutput):
                choices_item = choices_item_data.to_dict()
            else:
                choices_item = choices_item_data.to_dict()

            choices.append(choices_item)

        jobs = self.jobs

        docs = []
        for docs_item_data in self.docs:
            docs_item = docs_item_data.to_dict()
            docs.append(docs_item)

        created_at = self.created_at.isoformat()

        id = self.id

        usage: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.usage, Unset):
            usage = self.usage.to_dict()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "choices": choices,
                "jobs": jobs,
                "docs": docs,
                "created_at": created_at,
                "id": id,
            }
        )
        if usage is not UNSET:
            field_dict["usage"] = usage

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        from ..models.base_chat_output import BaseChatOutput
        from ..models.competion_usage import CompetionUsage
        from ..models.doc_reference import DocReference

        d = src_dict.copy()
        choices = []
        _choices = d.pop("choices")
        for choices_item_data in _choices:

            def _parse_choices_item(data: object) -> "BaseChatOutput":
                try:
                    if not isinstance(data, dict):
                        raise TypeError()
                    choices_item_type_0 = BaseChatOutput.from_dict(data)

                    return choices_item_type_0
                except:  # noqa: E722
                    pass
                if not isinstance(data, dict):
                    raise TypeError()
                choices_item_type_1 = BaseChatOutput.from_dict(data)

                return choices_item_type_1

            choices_item = _parse_choices_item(choices_item_data)

            choices.append(choices_item)

        jobs = cast(List[str], d.pop("jobs"))

        docs = []
        _docs = d.pop("docs")
        for docs_item_data in _docs:
            docs_item = DocReference.from_dict(docs_item_data)

            docs.append(docs_item)

        created_at = isoparse(d.pop("created_at"))

        id = d.pop("id")

        _usage = d.pop("usage", UNSET)
        usage: Union[Unset, CompetionUsage]
        if isinstance(_usage, Unset):
            usage = UNSET
        else:
            usage = CompetionUsage.from_dict(_usage)

        message_chat_response = cls(
            choices=choices,
            jobs=jobs,
            docs=docs,
            created_at=created_at,
            id=id,
            usage=usage,
        )

        message_chat_response.additional_properties = d
        return message_chat_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
