# 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, StrictInt, StrictStr
from typing_extensions import Self

from perigon.models.wikidata_label_holder import WikidataLabelHolder


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

    page_id: Optional[StrictStr] = Field(default=None, alias="pageId")
    section_id: Optional[StrictStr] = Field(default=None, alias="sectionId")
    wiki_page_id: Optional[StrictInt] = Field(default=None, alias="wikiPageId")
    wiki_revision_id: Optional[StrictInt] = Field(default=None, alias="wikiRevisionId")
    wiki_revision_ts: Optional[StrictStr] = Field(default=None, alias="wikiRevisionTs")
    wiki_code: Optional[StrictStr] = Field(default=None, alias="wikiCode")
    wiki_namespace: Optional[StrictInt] = Field(default=None, alias="wikiNamespace")
    wiki_title: Optional[StrictStr] = Field(default=None, alias="wikiTitle")
    wikidata_id: Optional[StrictStr] = Field(default=None, alias="wikidataId")
    wikidata_instance_of: Optional[List[WikidataLabelHolder]] = Field(
        default=None, alias="wikidataInstanceOf"
    )
    redirect_titles: Optional[List[StrictStr]] = Field(
        default=None, alias="redirectTitles"
    )
    pageviews: Optional[StrictInt] = None
    title: Optional[StrictStr] = None
    style_level: Optional[StrictInt] = Field(default=None, alias="styleLevel")
    content: Optional[StrictStr] = None
    __properties: ClassVar[List[str]] = [
        "pageId",
        "sectionId",
        "wikiPageId",
        "wikiRevisionId",
        "wikiRevisionTs",
        "wikiCode",
        "wikiNamespace",
        "wikiTitle",
        "wikidataId",
        "wikidataInstanceOf",
        "redirectTitles",
        "pageviews",
        "title",
        "styleLevel",
        "content",
    ]

    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 WikiData 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 wikidata_instance_of (list)
        _items = []
        if self.wikidata_instance_of:
            for _item_wikidata_instance_of in self.wikidata_instance_of:
                if _item_wikidata_instance_of:
                    _items.append(_item_wikidata_instance_of.to_dict())
            _dict["wikidataInstanceOf"] = _items
        # set to None if page_id (nullable) is None
        # and model_fields_set contains the field
        if self.page_id is None and "page_id" in self.model_fields_set:
            _dict["pageId"] = None

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "pageId": obj.get("pageId"),
                "sectionId": obj.get("sectionId"),
                "wikiPageId": obj.get("wikiPageId"),
                "wikiRevisionId": obj.get("wikiRevisionId"),
                "wikiRevisionTs": obj.get("wikiRevisionTs"),
                "wikiCode": obj.get("wikiCode"),
                "wikiNamespace": obj.get("wikiNamespace"),
                "wikiTitle": obj.get("wikiTitle"),
                "wikidataId": obj.get("wikidataId"),
                "wikidataInstanceOf": (
                    [
                        WikidataLabelHolder.from_dict(_item)
                        for _item in obj["wikidataInstanceOf"]
                    ]
                    if obj.get("wikidataInstanceOf") is not None
                    else None
                ),
                "redirectTitles": obj.get("redirectTitles"),
                "pageviews": obj.get("pageviews"),
                "title": obj.get("title"),
                "styleLevel": obj.get("styleLevel"),
                "content": obj.get("content"),
            }
        )
        return _obj
