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

from __future__ import annotations
from datetime import datetime
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 DeliveryTypedDict(TypedDict):
    estimated_delivery_date: NotRequired[Nullable[datetime]]
    r"""Estimated delivery date of the shipment, if provided by the courier. Format: [Date and Time in UTC](http://docs.ship24.com/data-format#logistics-date-and-time)"""
    service: NotRequired[Nullable[str]]
    r"""Name of logistics service or product for the shipment."""
    signed_by: NotRequired[Nullable[str]]
    r"""Name of the person who signed for the shipment."""


class Delivery(BaseModel):
    estimated_delivery_date: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="estimatedDeliveryDate")
    ] = UNSET
    r"""Estimated delivery date of the shipment, if provided by the courier. Format: [Date and Time in UTC](http://docs.ship24.com/data-format#logistics-date-and-time)"""

    service: OptionalNullable[str] = UNSET
    r"""Name of logistics service or product for the shipment."""

    signed_by: Annotated[OptionalNullable[str], pydantic.Field(alias="signedBy")] = (
        UNSET
    )
    r"""Name of the person who signed for the shipment."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["estimatedDeliveryDate", "service", "signedBy"]
        nullable_fields = ["estimatedDeliveryDate", "service", "signedBy"]
        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 TrackingNumberTypedDict(TypedDict):
    tn: NotRequired[str]
    r"""Tracking number."""


class TrackingNumber(BaseModel):
    tn: Optional[str] = None
    r"""Tracking number."""


class ShipmentRecipientTypedDict(TypedDict):
    r"""Information on the recipient."""

    name: NotRequired[Nullable[str]]
    address: NotRequired[Nullable[str]]
    post_code: NotRequired[Nullable[str]]
    city: NotRequired[Nullable[str]]
    subdivision: NotRequired[Nullable[str]]


class ShipmentRecipient(BaseModel):
    r"""Information on the recipient."""

    name: OptionalNullable[str] = UNSET

    address: OptionalNullable[str] = UNSET

    post_code: Annotated[OptionalNullable[str], pydantic.Field(alias="postCode")] = (
        UNSET
    )

    city: OptionalNullable[str] = UNSET

    subdivision: OptionalNullable[str] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["name", "address", "postCode", "city", "subdivision"]
        nullable_fields = ["name", "address", "postCode", "city", "subdivision"]
        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 ShipmentTypedDict(TypedDict):
    shipment_id: NotRequired[Nullable[str]]
    r"""Unique identifier of the parcel in Ship24 system."""
    status_code: NotRequired[Nullable[str]]
    r"""[statusCode](https://docs.ship24.com/status/#statuscode-and-statuscategory) of the shipment."""
    status_category: NotRequired[Nullable[str]]
    r"""[statusCategory](https://docs.ship24.com/status/#statuscode-and-statuscategory) of the shipment."""
    status_milestone: NotRequired[str]
    r"""[statusMilestone](https:docs.ship24.com/status/#statusmilestone) of the shipment."""
    origin_country_code: NotRequired[Nullable[str]]
    r"""Detected country code of origin."""
    destination_country_code: NotRequired[Nullable[str]]
    r"""Detected country code of destination."""
    delivery: NotRequired[DeliveryTypedDict]
    tracking_numbers: NotRequired[List[TrackingNumberTypedDict]]
    r"""List of tracking numbers linked to the shipment."""
    recipient: NotRequired[Nullable[ShipmentRecipientTypedDict]]
    r"""Information on the recipient."""


class Shipment(BaseModel):
    shipment_id: Annotated[
        OptionalNullable[str], pydantic.Field(alias="shipmentId")
    ] = UNSET
    r"""Unique identifier of the parcel in Ship24 system."""

    status_code: Annotated[
        OptionalNullable[str], pydantic.Field(alias="statusCode")
    ] = UNSET
    r"""[statusCode](https://docs.ship24.com/status/#statuscode-and-statuscategory) of the shipment."""

    status_category: Annotated[
        OptionalNullable[str], pydantic.Field(alias="statusCategory")
    ] = UNSET
    r"""[statusCategory](https://docs.ship24.com/status/#statuscode-and-statuscategory) of the shipment."""

    status_milestone: Annotated[
        Optional[str], pydantic.Field(alias="statusMilestone")
    ] = None
    r"""[statusMilestone](https:docs.ship24.com/status/#statusmilestone) of the shipment."""

    origin_country_code: Annotated[
        OptionalNullable[str], pydantic.Field(alias="originCountryCode")
    ] = UNSET
    r"""Detected country code of origin."""

    destination_country_code: Annotated[
        OptionalNullable[str], pydantic.Field(alias="destinationCountryCode")
    ] = UNSET
    r"""Detected country code of destination."""

    delivery: Optional[Delivery] = None

    tracking_numbers: Annotated[
        Optional[List[TrackingNumber]], pydantic.Field(alias="trackingNumbers")
    ] = None
    r"""List of tracking numbers linked to the shipment."""

    recipient: OptionalNullable[ShipmentRecipient] = UNSET
    r"""Information on the recipient."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "shipmentId",
            "statusCode",
            "statusCategory",
            "statusMilestone",
            "originCountryCode",
            "destinationCountryCode",
            "delivery",
            "trackingNumbers",
            "recipient",
        ]
        nullable_fields = [
            "shipmentId",
            "statusCode",
            "statusCategory",
            "originCountryCode",
            "destinationCountryCode",
            "recipient",
        ]
        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
