"""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, UNSET_SENTINEL
from typing import Any, List, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


TrackerCourierCodeTypedDict = TypeAliasType(
    "TrackerCourierCodeTypedDict", Union[List[Any], str]
)


TrackerCourierCode = TypeAliasType("TrackerCourierCode", Union[List[Any], str])


class TrackerTypedDict(TypedDict):
    tracker_id: str
    r"""The id of the tracker that is providing this tracking."""
    tracking_number: str
    r"""The tracking number which the tracker is following."""
    shipment_reference: Nullable[str]
    r"""Your reference for this shipment, provided at the tracker's creation. Unlike clientTrackerId, this value is not validated for uniqueness."""
    client_tracker_id: Nullable[str]
    r"""Your unique identifier for this shipment, provided at the tracker's creation. Ship24 validates its uniqueness across all your active trackers."""
    is_subscribed: bool
    r"""Indicates whether the tracker is active. A value of `false` means the tracker is archived and will not be used for tracking."""
    is_tracked: bool
    r"""Indicates whether we are actively tracking the parcel. A value of `true` means new data is being searched for, while `false` indicates tracking has stopped due to delivery, inactivity, or unsubscription. Existing tracking results will remain accessible; however, new data will not be fetched, and notifications will no longer be sent."""
    created_at: datetime
    r"""The date and time at which the tracker was created."""
    courier_code: NotRequired[TrackerCourierCodeTypedDict]
    r"""Code of the courier(s) handling the shipment."""


class Tracker(BaseModel):
    tracker_id: Annotated[str, pydantic.Field(alias="trackerId")]
    r"""The id of the tracker that is providing this tracking."""

    tracking_number: Annotated[str, pydantic.Field(alias="trackingNumber")]
    r"""The tracking number which the tracker is following."""

    shipment_reference: Annotated[
        Nullable[str], pydantic.Field(alias="shipmentReference")
    ]
    r"""Your reference for this shipment, provided at the tracker's creation. Unlike clientTrackerId, this value is not validated for uniqueness."""

    client_tracker_id: Annotated[Nullable[str], pydantic.Field(alias="clientTrackerId")]
    r"""Your unique identifier for this shipment, provided at the tracker's creation. Ship24 validates its uniqueness across all your active trackers."""

    is_subscribed: Annotated[bool, pydantic.Field(alias="isSubscribed")]
    r"""Indicates whether the tracker is active. A value of `false` means the tracker is archived and will not be used for tracking."""

    is_tracked: Annotated[bool, pydantic.Field(alias="isTracked")]
    r"""Indicates whether we are actively tracking the parcel. A value of `true` means new data is being searched for, while `false` indicates tracking has stopped due to delivery, inactivity, or unsubscription. Existing tracking results will remain accessible; however, new data will not be fetched, and notifications will no longer be sent."""

    created_at: Annotated[datetime, pydantic.Field(alias="createdAt")]
    r"""The date and time at which the tracker was created."""

    courier_code: Annotated[
        Optional[TrackerCourierCode], pydantic.Field(alias="courierCode")
    ] = None
    r"""Code of the courier(s) handling the shipment."""

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