"""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, QueryParamMetadata
from typing import List, Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class ProcedureListRequestTypedDict(TypedDict):
    limit: NotRequired[int]
    cursor: NotRequired[float]
    search_query: NotRequired[str]
    created_after: NotRequired[datetime]
    created_before: NotRequired[datetime]


class ProcedureListRequest(BaseModel):
    limit: Annotated[
        Optional[int],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 50

    cursor: Annotated[
        Optional[float],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    search_query: Annotated[
        Optional[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    created_after: Annotated[
        Optional[datetime],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    created_before: Annotated[
        Optional[datetime],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None


class ProcedureListCreatedByUserTypedDict(TypedDict):
    r"""User who created the procedure."""

    id: str
    r"""Unique identifier for the user who created the procedure."""
    name: Nullable[str]
    r"""Name of the user who created the procedure."""
    email: str
    r"""Email of the user who created the procedure."""


class ProcedureListCreatedByUser(BaseModel):
    r"""User who created the procedure."""

    id: str
    r"""Unique identifier for the user who created the procedure."""

    name: Nullable[str]
    r"""Name of the user who created the procedure."""

    email: str
    r"""Email of the user who created the procedure."""

    @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


ProcedureListOutcome = Literal["PASS", "FAIL", "ERROR", "TIMEOUT", "ABORTED"]
r"""Result of the test run."""


class ProcedureListUnitTypedDict(TypedDict):
    r"""Unit associated with this run."""

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


class ProcedureListUnit(BaseModel):
    r"""Unit associated with this run."""

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


class RunTypedDict(TypedDict):
    id: str
    r"""Unique identifier for the run."""
    outcome: ProcedureListOutcome
    r"""Result of the test run."""
    started_at: datetime
    r"""ISO 8601 timestamp when the run was started."""
    unit: ProcedureListUnitTypedDict
    r"""Unit associated with this run."""


class Run(BaseModel):
    id: str
    r"""Unique identifier for the run."""

    outcome: ProcedureListOutcome
    r"""Result of the test run."""

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

    unit: ProcedureListUnit
    r"""Unit associated with this run."""


class ProcedureListDataTypedDict(TypedDict):
    id: str
    r"""Unique identifier for the procedure."""
    name: str
    r"""Name of the procedure."""
    created_at: datetime
    r"""ISO 8601 timestamp when the procedure was created."""
    created_by_user: ProcedureListCreatedByUserTypedDict
    r"""User who created the procedure."""
    runs: List[RunTypedDict]
    r"""Recent runs for this procedure."""
    fpy: Nullable[float]
    r"""First Pass Yield (FPY) as a percentage. Null if no runs available."""


class ProcedureListData(BaseModel):
    id: str
    r"""Unique identifier for the procedure."""

    name: str
    r"""Name of the procedure."""

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

    created_by_user: ProcedureListCreatedByUser
    r"""User who created the procedure."""

    runs: List[Run]
    r"""Recent runs for this procedure."""

    fpy: Nullable[float]
    r"""First Pass Yield (FPY) as a percentage. Null if no runs available."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["fpy"]
        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 ProcedureListMetaTypedDict(TypedDict):
    has_more: bool
    r"""Whether there are more results available for pagination."""
    next_cursor: Nullable[float]
    r"""Cursor value to fetch the next page of results. Use this value as the cursor parameter in the next request. Null if no more results available."""


class ProcedureListMeta(BaseModel):
    has_more: bool
    r"""Whether there are more results available for pagination."""

    next_cursor: Nullable[float]
    r"""Cursor value to fetch the next page of results. Use this value as the cursor parameter in the next request. Null if no more results available."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["next_cursor"]
        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 ProcedureListResponseTypedDict(TypedDict):
    r"""Procedures retrieved successfully"""

    data: List[ProcedureListDataTypedDict]
    meta: ProcedureListMetaTypedDict


class ProcedureListResponse(BaseModel):
    r"""Procedures retrieved successfully"""

    data: List[ProcedureListData]

    meta: ProcedureListMeta
