from __future__ import annotations

from collections.abc import Mapping
from io import BytesIO
from typing import Any, TypeVar

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

from .. import types
from ..types import UNSET, File, FileTypes, Unset

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


@_attrs_define
class DocumentUploadBody:
    """
    Attributes:
        files (list[File] | Unset): Files to upload
        stack_id (str | Unset): MongoDB ObjectId
        source (str | Unset):  Default: 'Uploaded'.
        metadata (str | Unset): JSON string of metadata array
        import_key (str | Unset):
        skip_processing (bool | Unset): Skip document processing
    """

    files: list[File] | Unset = UNSET
    stack_id: str | Unset = UNSET
    source: str | Unset = "Uploaded"
    metadata: str | Unset = UNSET
    import_key: str | Unset = UNSET
    skip_processing: bool | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        files: list[FileTypes] | Unset = UNSET
        if not isinstance(self.files, Unset):
            files = []
            for files_item_data in self.files:
                files_item = files_item_data.to_tuple()

                files.append(files_item)

        stack_id = self.stack_id

        source = self.source

        metadata = self.metadata

        import_key = self.import_key

        skip_processing = self.skip_processing

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if files is not UNSET:
            field_dict["files"] = files
        if stack_id is not UNSET:
            field_dict["stackId"] = stack_id
        if source is not UNSET:
            field_dict["source"] = source
        if metadata is not UNSET:
            field_dict["metadata"] = metadata
        if import_key is not UNSET:
            field_dict["importKey"] = import_key
        if skip_processing is not UNSET:
            field_dict["skipProcessing"] = skip_processing

        return field_dict

    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        if not isinstance(self.files, Unset):
            for files_item_element in self.files:
                files.append(("files", files_item_element.to_tuple()))

        if not isinstance(self.stack_id, Unset):
            files.append(("stackId", (None, str(self.stack_id).encode(), "text/plain")))

        if not isinstance(self.source, Unset):
            files.append(("source", (None, str(self.source).encode(), "text/plain")))

        if not isinstance(self.metadata, Unset):
            files.append(("metadata", (None, str(self.metadata).encode(), "text/plain")))

        if not isinstance(self.import_key, Unset):
            files.append(("importKey", (None, str(self.import_key).encode(), "text/plain")))

        if not isinstance(self.skip_processing, Unset):
            files.append(("skipProcessing", (None, str(self.skip_processing).encode(), "text/plain")))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        _files = d.pop("files", UNSET)
        files: list[File] | Unset = UNSET
        if _files is not UNSET:
            files = []
            for files_item_data in _files:
                files_item = File(payload=BytesIO(files_item_data))

                files.append(files_item)

        stack_id = d.pop("stackId", UNSET)

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

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

        import_key = d.pop("importKey", UNSET)

        skip_processing = d.pop("skipProcessing", UNSET)

        document_upload_body = cls(
            files=files,
            stack_id=stack_id,
            source=source,
            metadata=metadata,
            import_key=import_key,
            skip_processing=skip_processing,
        )

        document_upload_body.additional_properties = d
        return document_upload_body

    @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
