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

from __future__ import annotations
from datetime import datetime
from pydantic import model_serializer
from tofupilot.v2.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from tofupilot.v2.utils import FieldMetadata, PathParamMetadata
from typing_extensions import Annotated, NotRequired, TypedDict


class ProcedureGetVersionRequestTypedDict(TypedDict):
    procedure_id: str
    r"""ID of the procedure that owns this version."""
    tag: str
    r"""Version tag to retrieve."""


class ProcedureGetVersionRequest(BaseModel):
    procedure_id: Annotated[
        str, FieldMetadata(path=PathParamMetadata(style="simple", explode=False))
    ]
    r"""ID of the procedure that owns this version."""

    tag: Annotated[
        str, FieldMetadata(path=PathParamMetadata(style="simple", explode=False))
    ]
    r"""Version tag to retrieve."""


class ProcedureGetVersionCreatedByUserTypedDict(TypedDict):
    r"""User who created this procedure version."""

    id: str
    r"""User ID."""
    name: Nullable[str]
    r"""User display name."""


class ProcedureGetVersionCreatedByUser(BaseModel):
    r"""User who created this procedure version."""

    id: str
    r"""User ID."""

    name: Nullable[str]
    r"""User display name."""

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


class ProcedureGetVersionCreatedByStationTypedDict(TypedDict):
    r"""Station that created this procedure version."""

    id: str
    r"""Station ID."""
    name: str
    r"""Station name."""


class ProcedureGetVersionCreatedByStation(BaseModel):
    r"""Station that created this procedure version."""

    id: str
    r"""Station ID."""

    name: str
    r"""Station name."""


class ProcedureGetVersionProcedureTypedDict(TypedDict):
    r"""Procedure this version belongs to."""

    id: str
    r"""Procedure ID."""
    name: str
    r"""Procedure name."""


class ProcedureGetVersionProcedure(BaseModel):
    r"""Procedure this version belongs to."""

    id: str
    r"""Procedure ID."""

    name: str
    r"""Procedure name."""


class ProcedureGetVersionResponseTypedDict(TypedDict):
    r"""Procedure version retrieved successfully"""

    id: str
    r"""Unique identifier for the procedure version."""
    tag: str
    r"""Version tag."""
    created_at: datetime
    r"""ISO 8601 timestamp when the procedure version was created."""
    procedure: ProcedureGetVersionProcedureTypedDict
    r"""Procedure this version belongs to."""
    run_count: int
    r"""Number of runs using this procedure version."""
    created_by_user: NotRequired[Nullable[ProcedureGetVersionCreatedByUserTypedDict]]
    r"""User who created this procedure version."""
    created_by_station: NotRequired[
        Nullable[ProcedureGetVersionCreatedByStationTypedDict]
    ]
    r"""Station that created this procedure version."""


class ProcedureGetVersionResponse(BaseModel):
    r"""Procedure version retrieved successfully"""

    id: str
    r"""Unique identifier for the procedure version."""

    tag: str
    r"""Version tag."""

    created_at: datetime
    r"""ISO 8601 timestamp when the procedure version was created."""

    procedure: ProcedureGetVersionProcedure
    r"""Procedure this version belongs to."""

    run_count: int
    r"""Number of runs using this procedure version."""

    created_by_user: OptionalNullable[ProcedureGetVersionCreatedByUser] = UNSET
    r"""User who created this procedure version."""

    created_by_station: OptionalNullable[ProcedureGetVersionCreatedByStation] = UNSET
    r"""Station that created this procedure version."""

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