"""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, UNSET_SENTINEL
from tofupilot.v2.utils import FieldMetadata, PathParamMetadata
from typing import List, Literal
from typing_extensions import Annotated, TypedDict


class ProcedureGetRequestTypedDict(TypedDict):
    id: str
    r"""Unique identifier of the procedure to retrieve."""


class ProcedureGetRequest(BaseModel):
    id: Annotated[
        str, FieldMetadata(path=PathParamMetadata(style="simple", explode=False))
    ]
    r"""Unique identifier of the procedure to retrieve."""


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

    id: str
    r"""User ID."""
    name: Nullable[str]
    r"""User display name."""
    email: Nullable[str]
    r"""User email address."""
    image: Nullable[str]
    r"""User profile image URL."""


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

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

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

    email: Nullable[str]
    r"""User email address."""

    image: Nullable[str]
    r"""User profile image URL."""

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


ProcedureGetOutcome = Literal["PASS", "FAIL", "ERROR", "TIMEOUT", "ABORTED"]
r"""Run outcome."""


class ProcedureGetUnitTypedDict(TypedDict):
    r"""Unit information."""

    serial_number: str
    r"""Unit serial number."""


class ProcedureGetUnit(BaseModel):
    r"""Unit information."""

    serial_number: str
    r"""Unit serial number."""


class RecentRunTypedDict(TypedDict):
    id: str
    r"""Run ID."""
    started_at: datetime
    r"""ISO 8601 timestamp when the run started."""
    outcome: ProcedureGetOutcome
    r"""Run outcome."""
    unit: Nullable[ProcedureGetUnitTypedDict]
    r"""Unit information."""


class RecentRun(BaseModel):
    id: str
    r"""Run ID."""

    started_at: datetime
    r"""ISO 8601 timestamp when the run started."""

    outcome: ProcedureGetOutcome
    r"""Run outcome."""

    unit: Nullable[ProcedureGetUnit]
    r"""Unit information."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["unit"]
        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 StationTypedDict(TypedDict):
    id: str
    r"""Station ID."""
    name: str
    r"""Station name."""
    identifier: str
    r"""Station identifier."""


class Station(BaseModel):
    id: str
    r"""Station ID."""

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

    identifier: str
    r"""Station identifier."""


class ProcedureGetResponseTypedDict(TypedDict):
    r"""Procedure retrieved successfully"""

    id: str
    r"""Unique identifier for the procedure."""
    identifier: Nullable[str]
    r"""Optional unique identifier for the procedure."""
    name: str
    r"""Procedure name."""
    created_at: datetime
    r"""ISO 8601 timestamp when the procedure was created."""
    created_by_user: Nullable[ProcedureGetCreatedByUserTypedDict]
    r"""User who created this procedure."""
    runs_count: float
    r"""Total number of runs for this procedure."""
    recent_runs: List[RecentRunTypedDict]
    r"""List of recent runs for this procedure."""
    stations: List[StationTypedDict]
    r"""Stations linked to this procedure."""


class ProcedureGetResponse(BaseModel):
    r"""Procedure retrieved successfully"""

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

    identifier: Nullable[str]
    r"""Optional unique identifier for the procedure."""

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

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

    created_by_user: Nullable[ProcedureGetCreatedByUser]
    r"""User who created this procedure."""

    runs_count: float
    r"""Total number of runs for this procedure."""

    recent_runs: List[RecentRun]
    r"""List of recent runs for this procedure."""

    stations: List[Station]
    r"""Stations linked to this procedure."""

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