# coding: utf-8

"""
Perigon API

The Perigon API provides access to comprehensive news and web content data. To use the API, simply sign up for a Perigon Business Solutions account to obtain your API key. Your available features may vary based on your plan. See the Authentication section for details on how to use your API key.

The version of the OpenAPI document: 1.0.0
Contact: data@perigon.io
Generated by OpenAPI Generator (https://openapi-generator.tech)

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


from __future__ import annotations

import json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar, Dict, List, Optional, Set

from pydantic import BaseModel, ConfigDict, Field, StrictStr
from typing_extensions import Self

from perigon.models.key_point import KeyPoint
from perigon.models.question import Question


class StoryHistoryRecord(BaseModel):
    """
    StoryHistoryRecord
    """  # noqa: E501

    cluster_id: Optional[StrictStr] = Field(default=None, alias="clusterId")
    created_at: Optional[StrictStr] = Field(default=None, alias="createdAt")
    name: Optional[StrictStr] = None
    triggered_at: Optional[StrictStr] = Field(default=None, alias="triggeredAt")
    summary: Optional[StrictStr] = None
    short_summary: Optional[StrictStr] = Field(default=None, alias="shortSummary")
    changelog: Optional[StrictStr] = None
    key_points: Optional[List[KeyPoint]] = Field(default=None, alias="keyPoints")
    questions: Optional[List[Question]] = None
    __properties: ClassVar[List[str]] = [
        "clusterId",
        "createdAt",
        "name",
        "triggeredAt",
        "summary",
        "shortSummary",
        "changelog",
        "keyPoints",
        "questions",
    ]

    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 StoryHistoryRecord 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 key_points (list)
        _items = []
        if self.key_points:
            for _item_key_points in self.key_points:
                if _item_key_points:
                    _items.append(_item_key_points.to_dict())
            _dict["keyPoints"] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in questions (list)
        _items = []
        if self.questions:
            for _item_questions in self.questions:
                if _item_questions:
                    _items.append(_item_questions.to_dict())
            _dict["questions"] = _items
        # set to None if cluster_id (nullable) is None
        # and model_fields_set contains the field
        if self.cluster_id is None and "cluster_id" in self.model_fields_set:
            _dict["clusterId"] = None

        # set to None if created_at (nullable) is None
        # and model_fields_set contains the field
        if self.created_at is None and "created_at" in self.model_fields_set:
            _dict["createdAt"] = None

        # set to None if name (nullable) is None
        # and model_fields_set contains the field
        if self.name is None and "name" in self.model_fields_set:
            _dict["name"] = None

        # set to None if triggered_at (nullable) is None
        # and model_fields_set contains the field
        if self.triggered_at is None and "triggered_at" in self.model_fields_set:
            _dict["triggeredAt"] = None

        # set to None if summary (nullable) is None
        # and model_fields_set contains the field
        if self.summary is None and "summary" in self.model_fields_set:
            _dict["summary"] = None

        # set to None if short_summary (nullable) is None
        # and model_fields_set contains the field
        if self.short_summary is None and "short_summary" in self.model_fields_set:
            _dict["shortSummary"] = None

        # set to None if changelog (nullable) is None
        # and model_fields_set contains the field
        if self.changelog is None and "changelog" in self.model_fields_set:
            _dict["changelog"] = None

        # set to None if key_points (nullable) is None
        # and model_fields_set contains the field
        if self.key_points is None and "key_points" in self.model_fields_set:
            _dict["keyPoints"] = None

        # set to None if questions (nullable) is None
        # and model_fields_set contains the field
        if self.questions is None and "questions" in self.model_fields_set:
            _dict["questions"] = None

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "clusterId": obj.get("clusterId"),
                "createdAt": obj.get("createdAt"),
                "name": obj.get("name"),
                "triggeredAt": obj.get("triggeredAt"),
                "summary": obj.get("summary"),
                "shortSummary": obj.get("shortSummary"),
                "changelog": obj.get("changelog"),
                "keyPoints": (
                    [KeyPoint.from_dict(_item) for _item in obj["keyPoints"]]
                    if obj.get("keyPoints") is not None
                    else None
                ),
                "questions": (
                    [Question.from_dict(_item) for _item in obj["questions"]]
                    if obj.get("questions") is not None
                    else None
                ),
            }
        )
        return _obj
