# coding: utf-8

"""
    Verity471 Unified API

      - creds: 1.0.0
  - girs: 1.0.0
  - indicators: 1.0.0
  - malware: 1.0.0
  - reports: 1.0.0
  - sources: 1.0.0


    The version of the OpenAPI document: 1.0.0
    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 verity471.models.attachment_data import AttachmentData
from verity471.models.author_actor1 import AuthorActor1
from verity471.models.entity_item import EntityItem
from verity471.models.translation_status import TranslationStatus
from typing import Optional, Set, TYPE_CHECKING
from typing_extensions import Self

if TYPE_CHECKING:
    # Only for type checkers; does not run at runtime.
    from verity471.verity_stix import STIXMapperSettings


class PostResponse1(BaseModel):
    """
    PostResponse1
    """ # noqa: E501
    attachments: Optional[List[AttachmentData]] = Field(default=None, description="Attachments and images in the post")
    author: Optional[AuthorActor1] = None
    creation_ts: StrictStr = Field(description="Publish date in ISO 8601 format")
    entities: Optional[List[EntityItem]] = Field(default=None, description="Entities extracted from the post")
    html: StrictStr = Field(description="HTML version of message")
    id: StrictStr = Field(description="Post unique id")
    last_updated_ts: StrictStr = Field(description="First time updated entity was seen")
    message: StrictStr = Field(description="Original Message")
    translated_message: Optional[StrictStr] = Field(default=None, description="Contains translated message if available")
    translation_status: Optional[TranslationStatus] = None
    __properties: ClassVar[List[str]] = ["attachments", "author", "creation_ts", "entities", "html", "id", "last_updated_ts", "message", "translated_message", "translation_status"]

    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_stix(self, settings: Optional["STIXMapperSettings"] = None):
        try:
            from verity471.verity_stix.mappers.common import StixMapper
        except ImportError as e:
            raise ImportError(
                "STIX support is an optional feature. "
                "Install with: pip install 'verity471[stix]'"
            ) from e
        stix_mapper = StixMapper(settings)
        return stix_mapper.map(self.to_dict())

    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 PostResponse1 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 attachments (list)
        _items = []
        if self.attachments:
            for _item_attachments in self.attachments:
                if _item_attachments:
                    _items.append(_item_attachments.to_dict())
            _dict['attachments'] = _items
        # override the default output from pydantic by calling `to_dict()` of author
        if self.author:
            _dict['author'] = self.author.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in entities (list)
        _items = []
        if self.entities:
            for _item_entities in self.entities:
                if _item_entities:
                    _items.append(_item_entities.to_dict())
            _dict['entities'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "attachments": [AttachmentData.from_dict(_item) for _item in obj["attachments"]] if obj.get("attachments") is not None else None,
            "author": AuthorActor1.from_dict(obj["author"]) if obj.get("author") is not None else None,
            "creation_ts": obj.get("creation_ts"),
            "entities": [EntityItem.from_dict(_item) for _item in obj["entities"]] if obj.get("entities") is not None else None,
            "html": obj.get("html"),
            "id": obj.get("id"),
            "last_updated_ts": obj.get("last_updated_ts"),
            "message": obj.get("message"),
            "translated_message": obj.get("translated_message"),
            "translation_status": obj.get("translation_status")
        })
        return _obj


