from __future__ import annotations

import datetime
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 dateutil.parser import isoparse

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.oracle_citation import OracleCitation
    from ..models.oracle_tool_call import OracleToolCall


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


@_attrs_define
class OracleResearchResponse:
    """
    Attributes:
        query (str | Unset): The research query that was executed
        final_report (str | Unset): The comprehensive research report generated by Oracle
        citations (list[OracleCitation] | Unset): Tool-result citations recorded during the run
        tool_calls (list[OracleToolCall] | Unset): List of tool calls made during research
        iterations (int | Unset): Number of research iterations performed
        duration_ms (int | Unset): Duration of the research in milliseconds
        session_id (str | Unset): Unique session identifier for this research
        created_at (datetime.datetime | Unset): When this research session was created
    """

    query: str | Unset = UNSET
    final_report: str | Unset = UNSET
    citations: list[OracleCitation] | Unset = UNSET
    tool_calls: list[OracleToolCall] | Unset = UNSET
    iterations: int | Unset = UNSET
    duration_ms: int | Unset = UNSET
    session_id: str | Unset = UNSET
    created_at: datetime.datetime | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        final_report = self.final_report

        citations: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.citations, Unset):
            citations = []
            for citations_item_data in self.citations:
                citations_item = citations_item_data.to_dict()
                citations.append(citations_item)

        tool_calls: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.tool_calls, Unset):
            tool_calls = []
            for tool_calls_item_data in self.tool_calls:
                tool_calls_item = tool_calls_item_data.to_dict()
                tool_calls.append(tool_calls_item)

        iterations = self.iterations

        duration_ms = self.duration_ms

        session_id = self.session_id

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if query is not UNSET:
            field_dict["query"] = query
        if final_report is not UNSET:
            field_dict["final_report"] = final_report
        if citations is not UNSET:
            field_dict["citations"] = citations
        if tool_calls is not UNSET:
            field_dict["tool_calls"] = tool_calls
        if iterations is not UNSET:
            field_dict["iterations"] = iterations
        if duration_ms is not UNSET:
            field_dict["duration_ms"] = duration_ms
        if session_id is not UNSET:
            field_dict["session_id"] = session_id
        if created_at is not UNSET:
            field_dict["created_at"] = created_at

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.oracle_citation import OracleCitation
        from ..models.oracle_tool_call import OracleToolCall

        d = dict(src_dict)
        query = d.pop("query", UNSET)

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

        _citations = d.pop("citations", UNSET)
        citations: list[OracleCitation] | Unset = UNSET
        if _citations is not UNSET:
            citations = []
            for citations_item_data in _citations:
                citations_item = OracleCitation.from_dict(citations_item_data)

                citations.append(citations_item)

        _tool_calls = d.pop("tool_calls", UNSET)
        tool_calls: list[OracleToolCall] | Unset = UNSET
        if _tool_calls is not UNSET:
            tool_calls = []
            for tool_calls_item_data in _tool_calls:
                tool_calls_item = OracleToolCall.from_dict(tool_calls_item_data)

                tool_calls.append(tool_calls_item)

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

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

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

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

        oracle_research_response = cls(
            query=query,
            final_report=final_report,
            citations=citations,
            tool_calls=tool_calls,
            iterations=iterations,
            duration_ms=duration_ms,
            session_id=session_id,
            created_at=created_at,
        )

        oracle_research_response.additional_properties = d
        return oracle_research_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
