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_data_order_status import OrderDataOrderStatus
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.item_data import ItemData
    from ..models.paperwork import Paperwork


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


@_attrs_define
class OrderData:
    """
    Example:
        {'customerEkp': '9012345678', 'orderStatus': 'FINALIZE', 'paperwork': {'contactName': 'John Doe',
            'awbCopyCount': 3, 'jobReference': 'Job ref', 'pickupType': 'CUSTOMER_DROP_OFF', 'telephoneNumber':
            '+4935120681234'}, 'items': [{'product': 'GMP', 'serviceLevel': 'PRIORITY', 'recipient': 'John Doe',
            'addressLine1': 'Any Street', 'addressLine2': 'Flat 2', 'addressLine3': '1st floor', 'postalCode': '01432',
            'city': 'Any City', 'state': 'Any State', 'destinationCountry': 'DE', 'custRef': 'REF-2361890-AB',
            'recipientPhone': '+44123456789', 'recipientFax': '+44123456789', 'recipientEmail': 'john.doe@example.eu',
            'senderTaxId': 'IOSS number', 'importerTaxId': 'IOSS number', '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:
        customer_ekp (str): Deutsche Post Customer Account number (EKP) of the customer who wants to create an order.
            Example: 9012345678.
        order_status (OrderDataOrderStatus): 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[ItemData] | Unset): The items associated with this order. A maximum of 2500 items per request are
            allowed. In case of "FINALIZE" at least one item is required.
    """

    customer_ekp: str
    order_status: OrderDataOrderStatus
    paperwork: Paperwork
    items: list[ItemData] | 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_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)

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

        return field_dict

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

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

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

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

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

                items.append(items_item)

        order_data = cls(
            customer_ekp=customer_ekp,
            order_status=order_status,
            paperwork=paperwork,
            items=items,
        )

        order_data.additional_properties = d
        return order_data

    @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
