# 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.wiki_page_section_holder import WikiPageSectionHolder
from perigon.models.wikidata_label_holder import WikidataLabelHolder


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

    id: Optional[StrictStr] = None
    scraped_at: Optional[StrictStr] = Field(default=None, alias="scrapedAt")
    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")
    url: Optional[StrictStr] = None
    top_image: Optional[StrictStr] = Field(default=None, alias="topImage")
    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"
    )
    summary: Optional[StrictStr] = None
    sections: Optional[List[WikiPageSectionHolder]] = None
    categories: Optional[List[StrictStr]] = None
    external_links: Optional[List[StrictStr]] = Field(
        default=None, alias="externalLinks"
    )
    references: Optional[List[StrictStr]] = None
    pageviews: Optional[StrictInt] = None
    __properties: ClassVar[List[str]] = [
        "id",
        "scrapedAt",
        "wikiPageId",
        "wikiRevisionId",
        "wikiRevisionTs",
        "wikiCode",
        "wikiNamespace",
        "wikiTitle",
        "url",
        "topImage",
        "wikidataId",
        "wikidataInstanceOf",
        "redirectTitles",
        "summary",
        "sections",
        "categories",
        "externalLinks",
        "references",
        "pageviews",
    ]

    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 WikiPage 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
        # override the default output from pydantic by calling `to_dict()` of each item in sections (list)
        _items = []
        if self.sections:
            for _item_sections in self.sections:
                if _item_sections:
                    _items.append(_item_sections.to_dict())
            _dict["sections"] = _items
        # set to None if id (nullable) is None
        # and model_fields_set contains the field
        if self.id is None and "id" in self.model_fields_set:
            _dict["id"] = None

        # set to None if scraped_at (nullable) is None
        # and model_fields_set contains the field
        if self.scraped_at is None and "scraped_at" in self.model_fields_set:
            _dict["scrapedAt"] = 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 url (nullable) is None
        # and model_fields_set contains the field
        if self.url is None and "url" in self.model_fields_set:
            _dict["url"] = None

        # set to None if top_image (nullable) is None
        # and model_fields_set contains the field
        if self.top_image is None and "top_image" in self.model_fields_set:
            _dict["topImage"] = 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 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 sections (nullable) is None
        # and model_fields_set contains the field
        if self.sections is None and "sections" in self.model_fields_set:
            _dict["sections"] = None

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

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

        # set to None if references (nullable) is None
        # and model_fields_set contains the field
        if self.references is None and "references" in self.model_fields_set:
            _dict["references"] = 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

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of WikiPage 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"),
                "scrapedAt": obj.get("scrapedAt"),
                "wikiPageId": obj.get("wikiPageId"),
                "wikiRevisionId": obj.get("wikiRevisionId"),
                "wikiRevisionTs": obj.get("wikiRevisionTs"),
                "wikiCode": obj.get("wikiCode"),
                "wikiNamespace": obj.get("wikiNamespace"),
                "wikiTitle": obj.get("wikiTitle"),
                "url": obj.get("url"),
                "topImage": obj.get("topImage"),
                "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"),
                "summary": obj.get("summary"),
                "sections": (
                    [
                        WikiPageSectionHolder.from_dict(_item)
                        for _item in obj["sections"]
                    ]
                    if obj.get("sections") is not None
                    else None
                ),
                "categories": obj.get("categories"),
                "externalLinks": obj.get("externalLinks"),
                "references": obj.get("references"),
                "pageviews": obj.get("pageviews"),
            }
        )
        return _obj
