# coding: utf-8

"""
    Gmail API

    The Gmail API lets you view and manage Gmail mailbox data like threads, messages, and labels.

    The version of the OpenAPI document: v1
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from gmail_ti.models.history_label_added import HistoryLabelAdded
from gmail_ti.models.history_label_removed import HistoryLabelRemoved
from gmail_ti.models.history_message_added import HistoryMessageAdded
from gmail_ti.models.history_message_deleted import HistoryMessageDeleted
from gmail_ti.models.message import Message
from typing import Optional, Set
from typing_extensions import Self

class History(BaseModel):
    """
    A record of a change to the user's mailbox. Each history change may affect multiple messages in multiple ways.
    """ # noqa: E501
    id: Optional[StrictStr] = Field(default=None, description="The mailbox sequence ID.")
    labels_added: Optional[List[HistoryLabelAdded]] = Field(default=None, description="Labels added to messages in this history record.", alias="labelsAdded")
    labels_removed: Optional[List[HistoryLabelRemoved]] = Field(default=None, description="Labels removed from messages in this history record.", alias="labelsRemoved")
    messages: Optional[List[Message]] = Field(default=None, description="List of messages changed in this history record. The fields for specific change types, such as `messagesAdded` may duplicate messages in this field. We recommend using the specific change-type fields instead of this.")
    messages_added: Optional[List[HistoryMessageAdded]] = Field(default=None, description="Messages added to the mailbox in this history record.", alias="messagesAdded")
    messages_deleted: Optional[List[HistoryMessageDeleted]] = Field(default=None, description="Messages deleted (not Trashed) from the mailbox in this history record.", alias="messagesDeleted")
    __properties: ClassVar[List[str]] = ["id", "labelsAdded", "labelsRemoved", "messages", "messagesAdded", "messagesDeleted"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of History from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in labels_added (list)
        _items = []
        if self.labels_added:
            for _item_labels_added in self.labels_added:
                if _item_labels_added:
                    _items.append(_item_labels_added.to_dict())
            _dict['labelsAdded'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in labels_removed (list)
        _items = []
        if self.labels_removed:
            for _item_labels_removed in self.labels_removed:
                if _item_labels_removed:
                    _items.append(_item_labels_removed.to_dict())
            _dict['labelsRemoved'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in messages (list)
        _items = []
        if self.messages:
            for _item_messages in self.messages:
                if _item_messages:
                    _items.append(_item_messages.to_dict())
            _dict['messages'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in messages_added (list)
        _items = []
        if self.messages_added:
            for _item_messages_added in self.messages_added:
                if _item_messages_added:
                    _items.append(_item_messages_added.to_dict())
            _dict['messagesAdded'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in messages_deleted (list)
        _items = []
        if self.messages_deleted:
            for _item_messages_deleted in self.messages_deleted:
                if _item_messages_deleted:
                    _items.append(_item_messages_deleted.to_dict())
            _dict['messagesDeleted'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of History from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "labelsAdded": [HistoryLabelAdded.from_dict(_item) for _item in obj["labelsAdded"]] if obj.get("labelsAdded") is not None else None,
            "labelsRemoved": [HistoryLabelRemoved.from_dict(_item) for _item in obj["labelsRemoved"]] if obj.get("labelsRemoved") is not None else None,
            "messages": [Message.from_dict(_item) for _item in obj["messages"]] if obj.get("messages") is not None else None,
            "messagesAdded": [HistoryMessageAdded.from_dict(_item) for _item in obj["messagesAdded"]] if obj.get("messagesAdded") is not None else None,
            "messagesDeleted": [HistoryMessageDeleted.from_dict(_item) for _item in obj["messagesDeleted"]] if obj.get("messagesDeleted") is not None else None
        })
        return _obj


