"""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


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


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


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


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

    image_url: 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,
]


ImageGenerationServerToolType = Literal["image_generation",]


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

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


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

    type: ImageGenerationServerToolType

    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[int] = None

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

    partial_images: Optional[int] = 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
