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.data_blocks import DataBlocks


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


@_attrs_define
class PromptUpdate:
    """
    Attributes:
        name (None | str | Unset):
        tags (list[str] | None | Unset):
        is_published (bool | None | Unset):
        blocks (DataBlocks | None | Unset):
    """

    name: None | str | Unset = UNSET
    tags: list[str] | None | Unset = UNSET
    is_published: bool | None | Unset = UNSET
    blocks: DataBlocks | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.data_blocks import DataBlocks

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

        tags: list[str] | None | Unset
        if isinstance(self.tags, Unset):
            tags = UNSET
        elif isinstance(self.tags, list):
            tags = self.tags

        else:
            tags = self.tags

        is_published: bool | None | Unset
        if isinstance(self.is_published, Unset):
            is_published = UNSET
        else:
            is_published = self.is_published

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if name is not UNSET:
            field_dict["name"] = name
        if tags is not UNSET:
            field_dict["tags"] = tags
        if is_published is not UNSET:
            field_dict["is_published"] = is_published
        if blocks is not UNSET:
            field_dict["blocks"] = blocks

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.data_blocks import DataBlocks

        d = dict(src_dict)

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

        name = _parse_name(d.pop("name", UNSET))

        def _parse_tags(data: object) -> list[str] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                tags_type_0 = cast(list[str], data)

                return tags_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[str] | None | Unset, data)

        tags = _parse_tags(d.pop("tags", UNSET))

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

        is_published = _parse_is_published(d.pop("is_published", UNSET))

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

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

        blocks = _parse_blocks(d.pop("blocks", UNSET))

        prompt_update = cls(
            name=name,
            tags=tags,
            is_published=is_published,
            blocks=blocks,
        )

        prompt_update.additional_properties = d
        return prompt_update

    @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
