from __future__ import annotations

from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.bulk_bag_dto import BulkBagDto
    from ..models.item import Item
    from ..models.order import Order


T = TypeVar("T", bound="Shipment")


@_attrs_define
class Shipment:
    """
    Example:
        {'awb': 'AWB123456789', 'order': {'customerEkp': '9012345678', 'orderId': 1234, 'orderStatus': 'FINALIZE',
            'paperwork': {'contactName': 'Max Mustermann', 'awbCopyCount': 3, 'jobReference': 'Job ref', 'pickupType':
            'CUSTOMER_DROP_OFF', 'telephoneNumber': '+4935120681234'}, 'items': [{'product': 'GMP', 'serviceLevel':
            'PRIORITY', 'recipient': 'Doris Bronson', 'addressLine1': 'Mustergasse 12', 'city': 'Dresden',
            'destinationCountry': 'DE', 'barcode': 'BC123456789', 'custRef': 'REF-2361890-AB', 'recipientPhone':
            '+4935120681234', 'recipientFax': '+4935120681234', 'recipientEmail': 'alfred.j.quack@somewhere.eu',
            'addressLine2': 'Hinterhaus', 'addressLine3': '1. Etage', 'state': 'Sachsen', 'postalCode': '01432',
            'shipmentAmount': 100, 'shipmentCurrency': 'EUR', 'shipmentGrossWeight': 1500, 'returnItemWanted': False,
            'shipmentNaturetype': 'SALE_GOODS', 'contents': [{'contentPieceHsCode': 1234567890, 'contentPieceDescription':
            'Trousers', 'contentPieceValue': '120.50', 'contentPieceNetweight': 1200, 'contentPieceOrigin': 'DE',
            'contentPieceAmount': 2}]}], 'shipments': [{}]}, 'items': {'product': 'GMP', 'serviceLevel': 'PRIORITY',
            'recipient': 'Doris Bronson', 'addressLine1': 'Mustergasse 12', 'city': 'Dresden', 'destinationCountry': 'DE',
            'barcode': 'BC123456789', 'custRef': 'REF-2361890-AB', 'recipientPhone': '+4935120681234', 'recipientFax':
            '+4935120681234', 'recipientEmail': 'alfred.j.quack@somewhere.eu', 'addressLine2': 'Hinterhaus', 'addressLine3':
            '1. Etage', 'state': 'Sachsen', 'postalCode': '01432', 'shipmentAmount': 100, 'shipmentCurrency': 'EUR',
            'shipmentGrossWeight': 1500, 'returnItemWanted': False, 'shipmentNaturetype': 'SALE_GOODS', 'contents':
            [{'contentPieceHsCode': 1234567890, 'contentPieceDescription': 'Trousers', 'contentPieceValue': '120.50',
            'contentPieceNetweight': 1200, 'contentPieceOrigin': 'DE', 'contentPieceAmount': 2}]}}

    Attributes:
        order (Order):  Example: {'customerEkp': '9012345678', 'orderId': 123456, 'orderStatus': 'FINALIZE',
            'shipments': {'awb': 9980000123456, 'items': {'id': 123456, 'barcode': 'RX12345678DE', 'product': 'GMP',
            'serviceLevel': 'PRIORITY', 'custRef': 'REF-2361890-AB', 'recipient': 'John Doe', 'recipientPhone':
            '+4935120681234', 'addressLine1': 'Mustergasse 12', 'addressLine2': 'Hinterhaus', 'addressLine3': '1. Etage',
            'city': 'Dresden', 'state': 'Sachsen', 'postalCode': '01432', 'destinationCountry': 'DE', 'shipmentGrossWeight':
            1500, 'senderTaxID': 'IOSS number', 'importerTaxId': 'IOSS number', 'returnItemWanted': False, 'contents': [],
            'items': {'id': 123456, 'barcode': 'RX123456DE', 'product': 'GPT', 'serviceLevel': 'PRIORITY', 'custRef':
            'BRE-2021-XIT', 'recipient': 'Doris Bronson', 'recipientPhone': '+449123456789', 'recipientEmail':
            'doris@examle.eu', 'addressLine1': 'Uptown street 10', 'addressLine2': '2nd floor', 'city': 'London',
            'postalCode': 'SW1A 2AA', 'destinationCountry': 'GB', 'shipmentCurrency': 'EUR', 'shipmentGrossWeight': 1500,
            'senderTaxID': 'IOSS number', 'importerTaxId': 'IOSS number', 'returnItemWanted': False, 'shipmentNaturetype':
            'SALE_GOODS', 'recipientFax': '+4935120681234', 'contents': [{'contentPieceHsCode': 1234567890,
            'contentPieceDescription': 'Hairspray', 'contentPieceValue': '120.50', 'contentPieceNetweight': 1200,
            'contentPieceOrigin': 'DE', 'contentPieceAmount': 2}]}}}}.
        awb (str | Unset): Transport document issued by a carrier or a forwarder towards the business customer. Example:
            AWB123456789.
        items (list[Item] | Unset): The items associated with this shipment.
        bags (list[BulkBagDto] | Unset): The bags associated with this order.
    """

    order: Order
    awb: str | Unset = UNSET
    items: list[Item] | Unset = UNSET
    bags: list[BulkBagDto] | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        order = self.order.to_dict()

        awb = self.awb

        items: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.items, Unset):
            items = []
            for items_item_data in self.items:
                items_item = items_item_data.to_dict()
                items.append(items_item)

        bags: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.bags, Unset):
            bags = []
            for bags_item_data in self.bags:
                bags_item = bags_item_data.to_dict()
                bags.append(bags_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "order": order,
            }
        )
        if awb is not UNSET:
            field_dict["awb"] = awb
        if items is not UNSET:
            field_dict["items"] = items
        if bags is not UNSET:
            field_dict["bags"] = bags

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.bulk_bag_dto import BulkBagDto
        from ..models.item import Item
        from ..models.order import Order

        d = dict(src_dict)
        order = Order.from_dict(d.pop("order"))

        awb = d.pop("awb", UNSET)

        _items = d.pop("items", UNSET)
        items: list[Item] | Unset = UNSET
        if _items is not UNSET:
            items = []
            for items_item_data in _items:
                items_item = Item.from_dict(items_item_data)

                items.append(items_item)

        _bags = d.pop("bags", UNSET)
        bags: list[BulkBagDto] | Unset = UNSET
        if _bags is not UNSET:
            bags = []
            for bags_item_data in _bags:
                bags_item = BulkBagDto.from_dict(bags_item_data)

                bags.append(bags_item)

        shipment = cls(
            order=order,
            awb=awb,
            items=items,
            bags=bags,
        )

        shipment.additional_properties = d
        return shipment

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
