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

from __future__ import annotations
from .errorhandlingoptions import ErrorHandlingOptions
from .outputformat import OutputFormat
from dg_mol_track_client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from dg_mol_track_client.utils import FieldMetadata, MultipartFormMetadata
import io
import pydantic
from pydantic import model_serializer
from typing import IO, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


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


class BodyCreateAssayResultsV1AssayResultsPostFile(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 BodyCreateAssayResultsV1AssayResultsPostTypedDict(TypedDict):
    file: BodyCreateAssayResultsV1AssayResultsPostFileTypedDict
    mapping: NotRequired[Nullable[str]]
    error_handling: NotRequired[ErrorHandlingOptions]
    output_format: NotRequired[OutputFormat]


class BodyCreateAssayResultsV1AssayResultsPost(BaseModel):
    file: Annotated[
        BodyCreateAssayResultsV1AssayResultsPostFile,
        FieldMetadata(multipart=MultipartFormMetadata(file=True)),
    ]

    mapping: Annotated[OptionalNullable[str], FieldMetadata(multipart=True)] = UNSET

    error_handling: Annotated[
        Optional[ErrorHandlingOptions], FieldMetadata(multipart=True)
    ] = None

    output_format: Annotated[Optional[OutputFormat], FieldMetadata(multipart=True)] = (
        None
    )

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["mapping", "error_handling", "output_format"]
        nullable_fields = ["mapping"]
        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
