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


PartListSortBy = Literal["name", "number", "created_at"]
r"""Field to sort results by."""

PartListSortOrder = Literal["asc", "desc"]
r"""Sort order direction."""


class PartListRequestTypedDict(TypedDict):
    limit: NotRequired[float]
    cursor: NotRequired[float]
    search_query: NotRequired[str]
    sort_by: NotRequired[PartListSortBy]
    r"""Field to sort results by."""
    sort_order: NotRequired[PartListSortOrder]
    r"""Sort order direction."""


class PartListRequest(BaseModel):
    limit: Annotated[
        Optional[float],
        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

    sort_by: Annotated[
        Optional[PartListSortBy],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = "created_at"
    r"""Field to sort results by."""

    sort_order: Annotated[
        Optional[PartListSortOrder],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = "desc"
    r"""Sort order direction."""


class PartListRevisionTypedDict(TypedDict):
    id: str
    r"""Unique identifier of the revision."""
    number: str
    r"""Revision number."""
    image: Nullable[str]
    r"""Revision image URL."""


class PartListRevision(BaseModel):
    id: str
    r"""Unique identifier of the revision."""

    number: str
    r"""Revision number."""

    image: Nullable[str]
    r"""Revision image URL."""

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


class PartListDataTypedDict(TypedDict):
    id: str
    r"""Unique database identifier of the part."""
    number: str
    r"""Unique part number identifier."""
    name: str
    r"""Human-readable name of the part."""
    created_at: datetime
    r"""Time at which the part was created."""
    revisions: List[PartListRevisionTypedDict]
    r"""List of revisions for this part."""


class PartListData(BaseModel):
    id: str
    r"""Unique database identifier of the part."""

    number: str
    r"""Unique part number identifier."""

    name: str
    r"""Human-readable name of the part."""

    created_at: datetime
    r"""Time at which the part was created."""

    revisions: List[PartListRevision]
    r"""List of revisions for this part."""


class PartListMetaTypedDict(TypedDict):
    r"""Pagination metadata."""

    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 PartListMeta(BaseModel):
    r"""Pagination metadata."""

    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 PartListResponseTypedDict(TypedDict):
    r"""Parts retrieved successfully"""

    data: List[PartListDataTypedDict]
    r"""List of parts matching the search criteria."""
    meta: PartListMetaTypedDict
    r"""Pagination metadata."""


class PartListResponse(BaseModel):
    r"""Parts retrieved successfully"""

    data: List[PartListData]
    r"""List of parts matching the search criteria."""

    meta: PartListMeta
    r"""Pagination metadata."""
