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

from __future__ import annotations
from postivo_client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import List, Optional
from typing_extensions import NotRequired, TypedDict


class MetadataResponseServiceTypedDict(TypedDict):
    service_id: NotRequired[int]
    r"""Unique service (dispatch type) identifier."""
    service_name: NotRequired[str]
    r"""Service (dispatch type) name."""
    service_return_fee: NotRequired[float]
    r"""Return fee charged when the shipment is returned."""


class MetadataResponseService(BaseModel):
    service_id: Optional[int] = None
    r"""Unique service (dispatch type) identifier."""

    service_name: Optional[str] = None
    r"""Service (dispatch type) name."""

    service_return_fee: Optional[float] = None
    r"""Return fee charged when the shipment is returned."""


class MetadataResponseCarrierTypedDict(TypedDict):
    carrier_id: NotRequired[int]
    r"""Unique carrier identifier."""
    carrier_name: NotRequired[str]
    r"""Carrier display name."""
    services: NotRequired[List[MetadataResponseServiceTypedDict]]
    r"""Services (dispatch types) available for this carrier."""


class MetadataResponseCarrier(BaseModel):
    carrier_id: Optional[int] = None
    r"""Unique carrier identifier."""

    carrier_name: Optional[str] = None
    r"""Carrier display name."""

    services: Optional[List[MetadataResponseService]] = None
    r"""Services (dispatch types) available for this carrier."""


class PaperTypedDict(TypedDict):
    paper_id: NotRequired[int]
    r"""Unique paper type identifier."""
    paper_name: NotRequired[str]
    r"""Paper type name."""


class Paper(BaseModel):
    paper_id: Optional[int] = None
    r"""Unique paper type identifier."""

    paper_name: Optional[str] = None
    r"""Paper type name."""


class EnvelopeTypedDict(TypedDict):
    envelope_id: NotRequired[int]
    r"""Unique envelope template identifier."""
    envelope_name: NotRequired[str]
    r"""Envelope template name."""
    max_sheets: NotRequired[int]
    r"""Maximum number of sheets supported by this envelope type."""


class Envelope(BaseModel):
    envelope_id: Optional[int] = None
    r"""Unique envelope template identifier."""

    envelope_name: Optional[str] = None
    r"""Envelope template name."""

    max_sheets: Optional[int] = None
    r"""Maximum number of sheets supported by this envelope type."""


class EnvelopeTemplateTypedDict(TypedDict):
    envelope_group_name: NotRequired[str]
    r"""Envelope template group name."""
    envelope_group_description: NotRequired[str]
    r"""Envelope template group description."""
    envelope: NotRequired[List[EnvelopeTypedDict]]
    r"""Envelope templates in this group."""


class EnvelopeTemplate(BaseModel):
    envelope_group_name: Optional[str] = None
    r"""Envelope template group name."""

    envelope_group_description: Optional[str] = None
    r"""Envelope template group description."""

    envelope: Optional[List[Envelope]] = None
    r"""Envelope templates in this group."""


class StatusCodeTypedDict(TypedDict):
    code: NotRequired[str]
    r"""Unique status code."""
    name: NotRequired[str]
    r"""Status name."""
    description: NotRequired[str]
    r"""Status description."""


class StatusCode(BaseModel):
    code: Optional[str] = None
    r"""Unique status code."""

    name: Optional[str] = None
    r"""Status name."""

    description: Optional[str] = None
    r"""Status description."""


class MetadataResponseTypedDict(TypedDict):
    r"""Metadata response."""

    carriers: NotRequired[Nullable[List[MetadataResponseCarrierTypedDict]]]
    r"""List of carriers and their available services."""
    papers: NotRequired[Nullable[List[PaperTypedDict]]]
    r"""Available paper types."""
    envelope_templates: NotRequired[Nullable[List[EnvelopeTemplateTypedDict]]]
    r"""Envelope template groups, each containing related templates."""
    status_codes: NotRequired[Nullable[List[StatusCodeTypedDict]]]
    r"""Available status codes."""


class MetadataResponse(BaseModel):
    r"""Metadata response."""

    carriers: OptionalNullable[List[MetadataResponseCarrier]] = UNSET
    r"""List of carriers and their available services."""

    papers: OptionalNullable[List[Paper]] = UNSET
    r"""Available paper types."""

    envelope_templates: OptionalNullable[List[EnvelopeTemplate]] = UNSET
    r"""Envelope template groups, each containing related templates."""

    status_codes: OptionalNullable[List[StatusCode]] = UNSET
    r"""Available status codes."""

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