"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
import io
from opperai.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from opperai.utils import FieldMetadata, MultipartFormMetadata
import pydantic
from pydantic import model_serializer
from typing import IO, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


class BodyUploadFileKnowledgeKnowledgeBaseIDUploadPostFileTypedDict(TypedDict):
    file_name: str
    content: Union[bytes, IO[bytes], io.BufferedReader]
    content_type: NotRequired[str]


class BodyUploadFileKnowledgeKnowledgeBaseIDUploadPostFile(BaseModel):
    file_name: Annotated[
        str, pydantic.Field(alias="fileName"), FieldMetadata(multipart=True)
    ]

    content: Annotated[
        Union[bytes, IO[bytes], io.BufferedReader],
        pydantic.Field(alias=""),
        FieldMetadata(multipart=MultipartFormMetadata(content=True)),
    ]

    content_type: Annotated[
        Optional[str],
        pydantic.Field(alias="Content-Type"),
        FieldMetadata(multipart=True),
    ] = None


class BodyUploadFileKnowledgeKnowledgeBaseIDUploadPostTypedDict(TypedDict):
    file: BodyUploadFileKnowledgeKnowledgeBaseIDUploadPostFileTypedDict
    r"""The file to upload"""
    chunk_size: NotRequired[int]
    r"""The chunk size to use for the document (number of characters)"""
    chunk_overlap: NotRequired[int]
    r"""The chunk overlap to use for the document (number of characters)"""
    metadata: NotRequired[Nullable[str]]
    r"""Optional JSON object metadata to attach to the file"""


class BodyUploadFileKnowledgeKnowledgeBaseIDUploadPost(BaseModel):
    file: Annotated[
        BodyUploadFileKnowledgeKnowledgeBaseIDUploadPostFile,
        FieldMetadata(multipart=MultipartFormMetadata(file=True)),
    ]
    r"""The file to upload"""

    chunk_size: Annotated[Optional[int], FieldMetadata(multipart=True)] = 2000
    r"""The chunk size to use for the document (number of characters)"""

    chunk_overlap: Annotated[Optional[int], FieldMetadata(multipart=True)] = 200
    r"""The chunk overlap to use for the document (number of characters)"""

    metadata: Annotated[OptionalNullable[str], FieldMetadata(multipart=True)] = UNSET
    r"""Optional JSON object metadata to attach to the file"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["chunk_size", "chunk_overlap", "metadata"]
        nullable_fields = ["metadata"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m
