# coding: utf-8

"""
    Moodle Webservice API

    Auto-generated OpenAPI spec for Moodle's Webservice API.

    The version of the OpenAPI document: 5.0.3 (Build: 20251006)
    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, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from poodle_async_full.models.core_blog_get_entries_response_entries_inner_tags_inner import CoreBlogGetEntriesResponseEntriesInnerTagsInner
from typing import Optional, Set
from typing_extensions import Self

from poodle_async_full.configuration import settings

class ModWikiGetPageContentsResponsePage(BaseModel):
    """
    Page
    """ # noqa: E501
    cachedcontent: Optional[StrictStr] = Field(description="Page contents.")
    caneditpage: Optional[StrictBool] = Field(description="True if user can edit the page.")
    contentformat: Optional[StrictInt] = Field(default=None, description="cachedcontent format (1 = HTML, 0 = MOODLE, 2 = PLAIN, or 4 = MARKDOWN)")
    groupid: Optional[StrictInt] = Field(description="Page's group ID.")
    id: Optional[StrictInt] = Field(description="Page ID.")
    subwikiid: Optional[StrictInt] = Field(description="Page's subwiki ID.")
    tags: Optional[List[CoreBlogGetEntriesResponseEntriesInnerTagsInner]] = Field(default=None, description="Tags")
    title: Optional[StrictStr] = Field(description="Page title.")
    userid: Optional[StrictInt] = Field(description="Page's user ID.")
    version: Optional[StrictInt] = Field(default=None, description="Latest version of the page.")
    wikiid: Optional[StrictInt] = Field(description="Page's wiki ID.")
    __properties: ClassVar[List[str]] = ["cachedcontent", "caneditpage", "contentformat", "groupid", "id", "subwikiid", "tags", "title", "userid", "version", "wikiid"]

    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 ModWikiGetPageContentsResponsePage 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 tags (list)
        _items = []
        if self.tags:
            for _item_tags in self.tags:
                if _item_tags:
                    _items.append(_item_tags.to_dict())
            _dict['tags'] = _items
        # set to None if cachedcontent (nullable) is None
        # and model_fields_set contains the field
        if self.cachedcontent is None and "cachedcontent" in self.model_fields_set:
            _dict['cachedcontent'] = None

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

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

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

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

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

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

        return _dict

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

        relaxed = settings.relaxe_all_models or "ModWikiGetPageContentsResponsePage" in settings.relaxed_models

        if not isinstance(obj, dict):
            return cls.model_construct(**obj) if relaxed else cls.model_validate(obj)

        data = {
            "cachedcontent": obj.get("cachedcontent"),
            "caneditpage": obj.get("caneditpage"),
            "contentformat": obj.get("contentformat"),
            "groupid": obj.get("groupid"),
            "id": obj.get("id"),
            "subwikiid": obj.get("subwikiid"),
            "tags": [CoreBlogGetEntriesResponseEntriesInnerTagsInner.from_dict(_item) for _item in obj["tags"]] if obj.get("tags") is not None else None,
            "title": obj.get("title"),
            "userid": obj.get("userid"),
            "version": obj.get("version"),
            "wikiid": obj.get("wikiid")
        }
        _obj = cls.model_construct(**data) if relaxed else cls.model_validate(data)
        return _obj


