from __future__ import annotations

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

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

from ..models.vector_store_file_status import VectorStoreFileStatus
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.last_error import LastError
    from ..models.other_file_chunking_strategy_object import OtherFileChunkingStrategyObject
    from ..models.static_file_chunking_strategy_object import StaticFileChunkingStrategyObject
    from ..models.vector_store_file_attributes_type_0 import VectorStoreFileAttributesType0


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


@_attrs_define
class VectorStoreFile:
    """A list of files attached to a vector store.

    Attributes:
        id (str):
        created_at (int):
        object_ (Literal['vector_store.file']):
        status (VectorStoreFileStatus):
        usage_bytes (int):
        vector_store_id (str):
        last_error (LastError | None | Unset):
        attributes (None | Unset | VectorStoreFileAttributesType0):
        chunking_strategy (None | OtherFileChunkingStrategyObject | StaticFileChunkingStrategyObject | Unset):
    """

    id: str
    created_at: int
    object_: Literal["vector_store.file"]
    status: VectorStoreFileStatus
    usage_bytes: int
    vector_store_id: str
    last_error: LastError | None | Unset = UNSET
    attributes: None | Unset | VectorStoreFileAttributesType0 = UNSET
    chunking_strategy: None | OtherFileChunkingStrategyObject | StaticFileChunkingStrategyObject | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.last_error import LastError
        from ..models.other_file_chunking_strategy_object import OtherFileChunkingStrategyObject
        from ..models.static_file_chunking_strategy_object import StaticFileChunkingStrategyObject
        from ..models.vector_store_file_attributes_type_0 import VectorStoreFileAttributesType0

        id = self.id

        created_at = self.created_at

        object_ = self.object_

        status = self.status.value

        usage_bytes = self.usage_bytes

        vector_store_id = self.vector_store_id

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

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

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "id": id,
                "created_at": created_at,
                "object": object_,
                "status": status,
                "usage_bytes": usage_bytes,
                "vector_store_id": vector_store_id,
            }
        )
        if last_error is not UNSET:
            field_dict["last_error"] = last_error
        if attributes is not UNSET:
            field_dict["attributes"] = attributes
        if chunking_strategy is not UNSET:
            field_dict["chunking_strategy"] = chunking_strategy

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.last_error import LastError
        from ..models.other_file_chunking_strategy_object import OtherFileChunkingStrategyObject
        from ..models.static_file_chunking_strategy_object import StaticFileChunkingStrategyObject
        from ..models.vector_store_file_attributes_type_0 import VectorStoreFileAttributesType0

        d = dict(src_dict)
        id = d.pop("id")

        created_at = d.pop("created_at")

        object_ = cast(Literal["vector_store.file"], d.pop("object"))
        if object_ != "vector_store.file":
            raise ValueError(f"object must match const 'vector_store.file', got '{object_}'")

        status = VectorStoreFileStatus(d.pop("status"))

        usage_bytes = d.pop("usage_bytes")

        vector_store_id = d.pop("vector_store_id")

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

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

        last_error = _parse_last_error(d.pop("last_error", UNSET))

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

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

        attributes = _parse_attributes(d.pop("attributes", UNSET))

        def _parse_chunking_strategy(
            data: object,
        ) -> None | OtherFileChunkingStrategyObject | StaticFileChunkingStrategyObject | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                chunking_strategy_type_0 = StaticFileChunkingStrategyObject.from_dict(data)

                return chunking_strategy_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                chunking_strategy_type_1 = OtherFileChunkingStrategyObject.from_dict(data)

                return chunking_strategy_type_1
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | OtherFileChunkingStrategyObject | StaticFileChunkingStrategyObject | Unset, data)

        chunking_strategy = _parse_chunking_strategy(d.pop("chunking_strategy", UNSET))

        vector_store_file = cls(
            id=id,
            created_at=created_at,
            object_=object_,
            status=status,
            usage_bytes=usage_bytes,
            vector_store_id=vector_store_id,
            last_error=last_error,
            attributes=attributes,
            chunking_strategy=chunking_strategy,
        )

        vector_store_file.additional_properties = d
        return vector_store_file

    @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
