from __future__ import annotations

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 ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.agent_step_event import AgentStepEvent
    from ..models.error_event import ErrorEvent
    from ..models.stream_start_event import StreamStartEvent
    from ..models.token_event import TokenEvent
    from ..models.user_input_request_event import UserInputRequestEvent


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


@_attrs_define
class SSESchemas:
    """Container for all SSE event schemas — drives OpenAPI component generation.

    Each field exposes an event type to the OpenAPI spec so the TS frontend
    can autogenerate types.  The endpoint returns defaults (None) — only the
    schema components matter.

        Attributes:
            stream_start (None | StreamStartEvent | Unset):
            agent_step (AgentStepEvent | None | Unset):
            user_input_request (None | Unset | UserInputRequestEvent):
            token (None | TokenEvent | Unset):
            error (ErrorEvent | None | Unset):
    """

    stream_start: None | StreamStartEvent | Unset = UNSET
    agent_step: AgentStepEvent | None | Unset = UNSET
    user_input_request: None | Unset | UserInputRequestEvent = UNSET
    token: None | TokenEvent | Unset = UNSET
    error: ErrorEvent | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.agent_step_event import AgentStepEvent
        from ..models.error_event import ErrorEvent
        from ..models.stream_start_event import StreamStartEvent
        from ..models.token_event import TokenEvent
        from ..models.user_input_request_event import UserInputRequestEvent

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

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

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

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

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if stream_start is not UNSET:
            field_dict["stream_start"] = stream_start
        if agent_step is not UNSET:
            field_dict["agent_step"] = agent_step
        if user_input_request is not UNSET:
            field_dict["user_input_request"] = user_input_request
        if token is not UNSET:
            field_dict["token"] = token
        if error is not UNSET:
            field_dict["error"] = error

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.agent_step_event import AgentStepEvent
        from ..models.error_event import ErrorEvent
        from ..models.stream_start_event import StreamStartEvent
        from ..models.token_event import TokenEvent
        from ..models.user_input_request_event import UserInputRequestEvent

        d = dict(src_dict)

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

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

        stream_start = _parse_stream_start(d.pop("stream_start", UNSET))

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

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

        agent_step = _parse_agent_step(d.pop("agent_step", UNSET))

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

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

        user_input_request = _parse_user_input_request(d.pop("user_input_request", UNSET))

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

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

        token = _parse_token(d.pop("token", UNSET))

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

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

        error = _parse_error(d.pop("error", UNSET))

        sse_schemas = cls(
            stream_start=stream_start,
            agent_step=agent_step,
            user_input_request=user_input_request,
            token=token,
            error=error,
        )

        sse_schemas.additional_properties = d
        return sse_schemas

    @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
