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

from __future__ import annotations
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypedDict


OpenResponsesImageGenerationToolType = Literal["image_generation",]


Background = Union[
    Literal[
        "transparent",
        "opaque",
        "auto",
    ],
    UnrecognizedStr,
]


InputFidelity = Union[
    Literal[
        "high",
        "low",
    ],
    UnrecognizedStr,
]


class InputImageMaskTypedDict(TypedDict):
    image_url: NotRequired[str]
    file_id: NotRequired[str]


class InputImageMask(BaseModel):
    image_url: Optional[str] = None

    file_id: Optional[str] = None


ModelEnum = Union[
    Literal[
        "gpt-image-1",
        "gpt-image-1-mini",
    ],
    UnrecognizedStr,
]


Moderation = Union[
    Literal[
        "auto",
        "low",
    ],
    UnrecognizedStr,
]


OutputFormat = Union[
    Literal[
        "png",
        "webp",
        "jpeg",
    ],
    UnrecognizedStr,
]


Quality = Union[
    Literal[
        "low",
        "medium",
        "high",
        "auto",
    ],
    UnrecognizedStr,
]


Size = Union[
    Literal[
        "1024x1024",
        "1024x1536",
        "1536x1024",
        "auto",
    ],
    UnrecognizedStr,
]


class OpenResponsesImageGenerationToolTypedDict(TypedDict):
    r"""Image generation tool configuration"""

    type: OpenResponsesImageGenerationToolType
    background: NotRequired[Background]
    input_fidelity: NotRequired[Nullable[InputFidelity]]
    input_image_mask: NotRequired[InputImageMaskTypedDict]
    model: NotRequired[ModelEnum]
    moderation: NotRequired[Moderation]
    output_compression: NotRequired[float]
    output_format: NotRequired[OutputFormat]
    partial_images: NotRequired[float]
    quality: NotRequired[Quality]
    size: NotRequired[Size]


class OpenResponsesImageGenerationTool(BaseModel):
    r"""Image generation tool configuration"""

    type: OpenResponsesImageGenerationToolType

    background: Annotated[
        Optional[Background], PlainValidator(validate_open_enum(False))
    ] = None

    input_fidelity: Annotated[
        OptionalNullable[InputFidelity], PlainValidator(validate_open_enum(False))
    ] = UNSET

    input_image_mask: Optional[InputImageMask] = None

    model: Annotated[Optional[ModelEnum], PlainValidator(validate_open_enum(False))] = (
        None
    )

    moderation: Annotated[
        Optional[Moderation], PlainValidator(validate_open_enum(False))
    ] = None

    output_compression: Optional[float] = None

    output_format: Annotated[
        Optional[OutputFormat], PlainValidator(validate_open_enum(False))
    ] = None

    partial_images: Optional[float] = None

    quality: Annotated[Optional[Quality], PlainValidator(validate_open_enum(False))] = (
        None
    )

    size: Annotated[Optional[Size], PlainValidator(validate_open_enum(False))] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "background",
            "input_fidelity",
            "input_image_mask",
            "model",
            "moderation",
            "output_compression",
            "output_format",
            "partial_images",
            "quality",
            "size",
        ]
        nullable_fields = ["input_fidelity"]
        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
