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 ..models.order_order_status import OrderOrderStatus
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.item import Item
    from ..models.paperwork import Paperwork
    from ..models.shipment import Shipment


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


@_attrs_define
class 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}]}}}}

    Attributes:
        customer_ekp (str): Deutsche Post Customer Account number (EKP) of the customer who wants to create an order.
            Example: 9012345678.
        order_id (int): ID of the Order. Example: 1234.
        order_status (OrderOrderStatus): The status of the order. It indicates if an order shall be held open to add
            more items at a later point in time. "OPEN" means items can be added later, "FINALIZE" means that the order
            shall be processed immediately. Example: FINALIZE.
        paperwork (Paperwork):  Example: {'contactName': 'John Doe', 'awbCopyCount': 3, 'jobReference': 'Job ref',
            'pickupType': 'CUSTOMER_DROP_OFF', 'telephoneNumber': '+4935120681234'}.
        items (list[Item] | Unset): The items associated with this order.
        shipments (list[Shipment] | Unset): Array of shipments.
    """

    customer_ekp: str
    order_id: int
    order_status: OrderOrderStatus
    paperwork: Paperwork
    items: list[Item] | Unset = UNSET
    shipments: list[Shipment] | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        order_id = self.order_id

        order_status = self.order_status.value

        paperwork = self.paperwork.to_dict()

        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)

        shipments: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.shipments, Unset):
            shipments = []
            for componentsschemas_shipments_item_data in self.shipments:
                componentsschemas_shipments_item = componentsschemas_shipments_item_data.to_dict()
                shipments.append(componentsschemas_shipments_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "customerEkp": customer_ekp,
                "orderId": order_id,
                "orderStatus": order_status,
                "paperwork": paperwork,
            }
        )
        if items is not UNSET:
            field_dict["items"] = items
        if shipments is not UNSET:
            field_dict["shipments"] = shipments

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.item import Item
        from ..models.paperwork import Paperwork
        from ..models.shipment import Shipment

        d = dict(src_dict)
        customer_ekp = d.pop("customerEkp")

        order_id = d.pop("orderId")

        order_status = OrderOrderStatus(d.pop("orderStatus"))

        paperwork = Paperwork.from_dict(d.pop("paperwork"))

        _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)

        _shipments = d.pop("shipments", UNSET)
        shipments: list[Shipment] | Unset = UNSET
        if _shipments is not UNSET:
            shipments = []
            for componentsschemas_shipments_item_data in _shipments:
                componentsschemas_shipments_item = Shipment.from_dict(componentsschemas_shipments_item_data)

                shipments.append(componentsschemas_shipments_item)

        order = cls(
            customer_ekp=customer_ekp,
            order_id=order_id,
            order_status=order_status,
            paperwork=paperwork,
            items=items,
            shipments=shipments,
        )

        order.additional_properties = d
        return order

    @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
