# coding: utf-8

"""
    Elastic Email REST API

    This API is based on the REST API architecture, allowing the user to easily manage their data with this resource-based approach.    Every API call is established on which specific request type (GET, POST, PUT, DELETE) will be used.    The API has a limit of 20 concurrent connections and a hard timeout of 600 seconds per request.    To start using this API, you will need your Access Token (available <a target=\"_blank\" href=\"https://app.elasticemail.com/marketing/settings/new/manage-api\">here</a>). Remember to keep it safe. Required access levels are listed in the given request’s description.    Downloadable library clients can be found in our Github repository <a target=\"_blank\" href=\"https://github.com/ElasticEmail?tab=repositories&q=%22rest+api%22+in%3Areadme\">here</a>

    The version of the OpenAPI document: 4.0.0
    Contact: support@elasticemail.com
    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 ElasticEmail.models.body_part import BodyPart
from ElasticEmail.models.message_attachment import MessageAttachment
from ElasticEmail.models.utm import Utm
from typing import Optional, Set
from typing_extensions import Self

class EmailContent(BaseModel):
    """
    Proper e-mail content
    """ # noqa: E501
    body: Optional[List[BodyPart]] = Field(default=None, description="List of e-mail body parts, with user-provided MIME types (text/html, text/plain etc)", alias="Body")
    merge: Optional[Dict[str, StrictStr]] = Field(default=None, description="A key-value collection of custom merge fields, shared between recipients. Should be used in e-mail body like so: {firstname}, {lastname} etc.", alias="Merge")
    attachments: Optional[List[MessageAttachment]] = Field(default=None, description="Attachments provided by sending binary data", alias="Attachments")
    headers: Optional[Dict[str, StrictStr]] = Field(default=None, description="A key-value collection of custom e-mail headers.", alias="Headers")
    postback: Optional[StrictStr] = Field(default=None, description="Postback header.", alias="Postback")
    envelope_from: Optional[StrictStr] = Field(default=None, description="E-mail with an optional name to be used as the envelope from address (e.g.: John Doe <email@domain.com>)", alias="EnvelopeFrom")
    var_from: StrictStr = Field(description="Your e-mail with an optional name (e.g.: John Doe <email@domain.com>)", alias="From")
    reply_to: Optional[StrictStr] = Field(default=None, description="To what address should the recipients reply to (e.g. John Doe <email@domain.com>)", alias="ReplyTo")
    subject: Optional[StrictStr] = Field(default=None, description="Default subject of email.", alias="Subject")
    template_name: Optional[StrictStr] = Field(default=None, description="Name of template.", alias="TemplateName")
    attach_files: Optional[List[StrictStr]] = Field(default=None, description="Names of previously uploaded files that should be sent as downloadable attachments", alias="AttachFiles")
    utm: Optional[Utm] = Field(default=None, alias="Utm")
    __properties: ClassVar[List[str]] = ["Body", "Merge", "Attachments", "Headers", "Postback", "EnvelopeFrom", "From", "ReplyTo", "Subject", "TemplateName", "AttachFiles", "Utm"]

    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 EmailContent 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 body (list)
        _items = []
        if self.body:
            for _item in self.body:
                if _item:
                    _items.append(_item.to_dict())
            _dict['Body'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in attachments (list)
        _items = []
        if self.attachments:
            for _item in self.attachments:
                if _item:
                    _items.append(_item.to_dict())
            _dict['Attachments'] = _items
        # override the default output from pydantic by calling `to_dict()` of utm
        if self.utm:
            _dict['Utm'] = self.utm.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "Body": [BodyPart.from_dict(_item) for _item in obj["Body"]] if obj.get("Body") is not None else None,
            "Merge": obj.get("Merge"),
            "Attachments": [MessageAttachment.from_dict(_item) for _item in obj["Attachments"]] if obj.get("Attachments") is not None else None,
            "Headers": obj.get("Headers"),
            "Postback": obj.get("Postback"),
            "EnvelopeFrom": obj.get("EnvelopeFrom"),
            "From": obj.get("From"),
            "ReplyTo": obj.get("ReplyTo"),
            "Subject": obj.get("Subject"),
            "TemplateName": obj.get("TemplateName"),
            "AttachFiles": obj.get("AttachFiles"),
            "Utm": Utm.from_dict(obj["Utm"]) if obj.get("Utm") is not None else None
        })
        return _obj


