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


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

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


class BatchListRequestTypedDict(TypedDict):
    ids: NotRequired[List[str]]
    numbers: NotRequired[List[str]]
    created_after: NotRequired[datetime]
    created_before: NotRequired[datetime]
    limit: NotRequired[int]
    cursor: NotRequired[int]
    search_query: NotRequired[str]
    part_numbers: NotRequired[List[str]]
    revision_numbers: NotRequired[List[str]]
    sort_by: NotRequired[BatchListSortBy]
    r"""Field to sort results by."""
    sort_order: NotRequired[BatchListSortOrder]
    r"""Sort order direction."""


class BatchListRequest(BaseModel):
    ids: Annotated[
        Optional[List[str]],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    numbers: Annotated[
        Optional[List[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

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

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

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

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

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

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

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


class BatchListCreatedByUserTypedDict(TypedDict):
    r"""User who created this batch."""

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


class BatchListCreatedByUser(BaseModel):
    r"""User who created this batch."""

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

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

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

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["name", "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 BatchListCreatedByStationTypedDict(TypedDict):
    r"""Station that created this batch."""

    id: str
    r"""Station ID."""
    name: str
    r"""Station name."""
    image: Nullable[str]
    r"""Station image URL."""


class BatchListCreatedByStation(BaseModel):
    r"""Station that created this batch."""

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

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

    image: Nullable[str]
    r"""Station 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 BatchListRevisionTypedDict(TypedDict):
    r"""Revision information for this unit."""

    id: str
    r"""Revision ID."""
    number: str
    r"""Revision number."""
    image: Nullable[str]
    r"""Revision image URL."""


class BatchListRevision(BaseModel):
    r"""Revision information for this unit."""

    id: str
    r"""Revision ID."""

    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 BatchListPartTypedDict(TypedDict):
    r"""Part information with revision details for this unit."""

    id: str
    r"""Part ID."""
    number: str
    r"""Part number."""
    name: str
    r"""Part name."""
    revision: BatchListRevisionTypedDict
    r"""Revision information for this unit."""


class BatchListPart(BaseModel):
    r"""Part information with revision details for this unit."""

    id: str
    r"""Part ID."""

    number: str
    r"""Part number."""

    name: str
    r"""Part name."""

    revision: BatchListRevision
    r"""Revision information for this unit."""


class BatchListUnitTypedDict(TypedDict):
    id: str
    r"""Unit ID."""
    serial_number: str
    r"""Unit serial number."""
    part: BatchListPartTypedDict
    r"""Part information with revision details for this unit."""


class BatchListUnit(BaseModel):
    id: str
    r"""Unit ID."""

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

    part: BatchListPart
    r"""Part information with revision details for this unit."""


class BatchListDataTypedDict(TypedDict):
    id: str
    r"""Unique identifier for the batch."""
    number: str
    r"""Batch number."""
    created_at: datetime
    r"""ISO timestamp when the batch was created."""
    units: List[BatchListUnitTypedDict]
    r"""Units in this batch with basic information."""
    created_by_user: NotRequired[Nullable[BatchListCreatedByUserTypedDict]]
    r"""User who created this batch."""
    created_by_station: NotRequired[Nullable[BatchListCreatedByStationTypedDict]]
    r"""Station that created this batch."""


class BatchListData(BaseModel):
    id: str
    r"""Unique identifier for the batch."""

    number: str
    r"""Batch number."""

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

    units: List[BatchListUnit]
    r"""Units in this batch with basic information."""

    created_by_user: OptionalNullable[BatchListCreatedByUser] = UNSET
    r"""User who created this batch."""

    created_by_station: OptionalNullable[BatchListCreatedByStation] = UNSET
    r"""Station that created this batch."""

    @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


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

    has_more: bool
    r"""Whether there are more results available."""
    next_cursor: Nullable[float]
    r"""Cursor for fetching the next page of results."""


class BatchListMeta(BaseModel):
    r"""Pagination metadata."""

    has_more: bool
    r"""Whether there are more results available."""

    next_cursor: Nullable[float]
    r"""Cursor for fetching the next page of results."""

    @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 BatchListResponseTypedDict(TypedDict):
    r"""Batches retrieved successfully"""

    data: List[BatchListDataTypedDict]
    r"""Array of batches matching the query."""
    meta: BatchListMetaTypedDict
    r"""Pagination metadata."""


class BatchListResponse(BaseModel):
    r"""Batches retrieved successfully"""

    data: List[BatchListData]
    r"""Array of batches matching the query."""

    meta: BatchListMeta
    r"""Pagination metadata."""
