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

from __future__ import annotations
from .tracker import Tracker, TrackerTypedDict
from .tracker_create_request import TrackerCreateRequest, TrackerCreateRequestTypedDict
from enum import Enum
import pydantic
from pydantic import model_serializer
from ship24.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from typing import List, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class Status(str, Enum):
    r"""Status of the bulk creation.

    `success`: All trackers were created successfully or already existed. (Status code 200)

    `partial`: Operation contains both successes and errors. (Status code 207)

    `error`: All creations failed or error on request level. (Status codes 400, 403)
    """

    SUCCESS = "success"
    PARTIAL = "partial"
    ERROR = "error"


class SummaryTypedDict(TypedDict):
    r"""Summary of the bulk creation. Null if status is `error`."""

    total_inputs: NotRequired[int]
    r"""Total number of trackers to create."""
    total_created: NotRequired[int]
    r"""Total number of trackers created."""
    total_existing: NotRequired[int]
    r"""Total number of already existing trackers."""
    total_errors: NotRequired[int]
    r"""Total number of errors (failed creations)."""


class Summary(BaseModel):
    r"""Summary of the bulk creation. Null if status is `error`."""

    total_inputs: Annotated[Optional[int], pydantic.Field(alias="totalInputs")] = None
    r"""Total number of trackers to create."""

    total_created: Annotated[Optional[int], pydantic.Field(alias="totalCreated")] = None
    r"""Total number of trackers created."""

    total_existing: Annotated[Optional[int], pydantic.Field(alias="totalExisting")] = (
        None
    )
    r"""Total number of already existing trackers."""

    total_errors: Annotated[Optional[int], pydantic.Field(alias="totalErrors")] = None
    r"""Total number of errors (failed creations)."""


class ItemStatus(str, Enum):
    r"""Status of the tracker creation."""

    CREATED = "created"
    EXISTING = "existing"
    ERROR = "error"


class DataErrorTypedDict(TypedDict):
    code: str
    message: str


class DataError(BaseModel):
    code: str

    message: str


class BulkCreateTrackersResponseDataTypedDict(TypedDict):
    item_status: ItemStatus
    r"""Status of the tracker creation."""
    input_data: TrackerCreateRequestTypedDict
    tracker: NotRequired[TrackerTypedDict]
    errors: NotRequired[Nullable[List[DataErrorTypedDict]]]
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) about a single tracker creation. Null if `itemStatus` is not `error`."""


class BulkCreateTrackersResponseData(BaseModel):
    item_status: Annotated[ItemStatus, pydantic.Field(alias="itemStatus")]
    r"""Status of the tracker creation."""

    input_data: Annotated[TrackerCreateRequest, pydantic.Field(alias="inputData")]

    tracker: Optional[Tracker] = None

    errors: OptionalNullable[List[DataError]] = UNSET
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) about a single tracker creation. Null if `itemStatus` is not `error`."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["tracker", "errors"]
        nullable_fields = ["errors"]
        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 BulkCreateTrackersResponseErrorTypedDict(TypedDict):
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) of the request. Null if `status` is not `error`."""

    code: str
    message: str


class BulkCreateTrackersResponseError(BaseModel):
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) of the request. Null if `status` is not `error`."""

    code: str

    message: str


class BulkCreateTrackersResponseTypedDict(TypedDict):
    status: Status
    r"""Status of the bulk creation.

    `success`: All trackers were created successfully or already existed. (Status code 200)

    `partial`: Operation contains both successes and errors. (Status code 207)

    `error`: All creations failed or error on request level. (Status codes 400, 403)
    """
    summary: NotRequired[Nullable[SummaryTypedDict]]
    r"""Summary of the bulk creation. Null if status is `error`."""
    data: NotRequired[Nullable[List[BulkCreateTrackersResponseDataTypedDict]]]
    r"""Detailed information about each tracker creation. Null if status is `error`."""
    error: NotRequired[Nullable[BulkCreateTrackersResponseErrorTypedDict]]
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) of the request. Null if `status` is not `error`."""


class BulkCreateTrackersResponse(BaseModel):
    status: Status
    r"""Status of the bulk creation.

    `success`: All trackers were created successfully or already existed. (Status code 200)

    `partial`: Operation contains both successes and errors. (Status code 207)

    `error`: All creations failed or error on request level. (Status codes 400, 403)
    """

    summary: OptionalNullable[Summary] = UNSET
    r"""Summary of the bulk creation. Null if status is `error`."""

    data: OptionalNullable[List[BulkCreateTrackersResponseData]] = UNSET
    r"""Detailed information about each tracker creation. Null if status is `error`."""

    error: OptionalNullable[BulkCreateTrackersResponseError] = UNSET
    r"""[Error details](https://docs.ship24.com/errors#error-response-format) of the request. Null if `status` is not `error`."""

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