from __future__ import annotations

import datetime
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 dateutil.parser import isoparse

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.tracking_event_status import TrackingEventStatus
    from ..models.tracking_event_status_message import TrackingEventStatusMessage


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


@_attrs_define
class TrackingEvent:
    """Version 1

    Attributes:
        status (TrackingEventStatus): Item tracking status
        status_message (TrackingEventStatusMessage | Unset): Item tracking status message
        timestamp (datetime.datetime | Unset): Timestamp
    """

    status: TrackingEventStatus
    status_message: TrackingEventStatusMessage | Unset = UNSET
    timestamp: datetime.datetime | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        status_message: dict[str, Any] | Unset = UNSET
        if not isinstance(self.status_message, Unset):
            status_message = self.status_message.to_dict()

        timestamp: str | Unset = UNSET
        if not isinstance(self.timestamp, Unset):
            timestamp = self.timestamp.isoformat()

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "status": status,
            }
        )
        if status_message is not UNSET:
            field_dict["statusMessage"] = status_message
        if timestamp is not UNSET:
            field_dict["timestamp"] = timestamp

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.tracking_event_status import TrackingEventStatus
        from ..models.tracking_event_status_message import TrackingEventStatusMessage

        d = dict(src_dict)
        status = TrackingEventStatus.from_dict(d.pop("status"))

        _status_message = d.pop("statusMessage", UNSET)
        status_message: TrackingEventStatusMessage | Unset
        if isinstance(_status_message, Unset):
            status_message = UNSET
        else:
            status_message = TrackingEventStatusMessage.from_dict(_status_message)

        _timestamp = d.pop("timestamp", UNSET)
        timestamp: datetime.datetime | Unset
        if isinstance(_timestamp, Unset):
            timestamp = UNSET
        else:
            timestamp = isoparse(_timestamp)

        tracking_event = cls(
            status=status,
            status_message=status_message,
            timestamp=timestamp,
        )

        tracking_event.additional_properties = d
        return tracking_event

    @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
