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

from __future__ import annotations
from .applypatchcalloperation import (
    ApplyPatchCallOperation,
    ApplyPatchCallOperationTypedDict,
)
from .applypatchcallstatus import ApplyPatchCallStatus
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import validate_open_enum
from pydantic import model_serializer
from pydantic.functional_validators import PlainValidator
from typing import Literal
from typing_extensions import Annotated, NotRequired, TypedDict


ApplyPatchCallItemType = Literal["apply_patch_call",]


class ApplyPatchCallItemTypedDict(TypedDict):
    r"""A tool call emitted by the model requesting a V4A patch operation. The client applies the patch and echoes an `apply_patch_call_output` on the next turn."""

    call_id: str
    operation: ApplyPatchCallOperationTypedDict
    r"""The patch operation requested by an `apply_patch_call`. `create_file` and `update_file` carry a V4A diff; `delete_file` omits it."""
    status: ApplyPatchCallStatus
    r"""Lifecycle state of an `apply_patch_call` output item."""
    type: ApplyPatchCallItemType
    id: NotRequired[Nullable[str]]


class ApplyPatchCallItem(BaseModel):
    r"""A tool call emitted by the model requesting a V4A patch operation. The client applies the patch and echoes an `apply_patch_call_output` on the next turn."""

    call_id: str

    operation: ApplyPatchCallOperation
    r"""The patch operation requested by an `apply_patch_call`. `create_file` and `update_file` carry a V4A diff; `delete_file` omits it."""

    status: Annotated[ApplyPatchCallStatus, PlainValidator(validate_open_enum(False))]
    r"""Lifecycle state of an `apply_patch_call` output item."""

    type: ApplyPatchCallItemType

    id: OptionalNullable[str] = UNSET

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