# coding: utf-8

"""
    ETAPI

    External Trilium API

    The version of the OpenAPI document: 1.0.0
    Contact: zadam.apps@gmail.com
    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,
    field_validator,
)
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from typing import Optional, Set
from typing_extensions import Self


class Branch(BaseModel):
    """
    Branch places the note into the tree, it represents the relationship between a parent note and child note
    """  # noqa: E501

    branch_id: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, alias="branchId"
    )
    note_id: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, alias="noteId"
    )
    parent_note_id: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, alias="parentNoteId"
    )
    prefix: Optional[StrictStr] = None
    note_position: Optional[StrictInt] = Field(default=None, alias="notePosition")
    is_expanded: Optional[StrictBool] = Field(default=None, alias="isExpanded")
    utc_date_modified: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, alias="utcDateModified"
    )
    __properties: ClassVar[List[str]] = [
        "branchId",
        "noteId",
        "parentNoteId",
        "prefix",
        "notePosition",
        "isExpanded",
        "utcDateModified",
    ]

    @field_validator("branch_id")
    def branch_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"[a-zA-Z0-9_]{4,32}", value):
            raise ValueError(
                r"must validate the regular expression /[a-zA-Z0-9_]{4,32}/"
            )
        return value

    @field_validator("note_id")
    def note_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"[a-zA-Z0-9_]{4,32}", value):
            raise ValueError(
                r"must validate the regular expression /[a-zA-Z0-9_]{4,32}/"
            )
        return value

    @field_validator("parent_note_id")
    def parent_note_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"[a-zA-Z0-9_]{4,32}", value):
            raise ValueError(
                r"must validate the regular expression /[a-zA-Z0-9_]{4,32}/"
            )
        return value

    @field_validator("utc_date_modified")
    def utc_date_modified_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(
            r"[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z", value
        ):
            raise ValueError(
                r"must validate the regular expression /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z/"
            )
        return value

    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 Branch 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,
        )
        return _dict

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

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

        _obj = cls.model_validate(
            {
                "branchId": obj.get("branchId"),
                "noteId": obj.get("noteId"),
                "parentNoteId": obj.get("parentNoteId"),
                "prefix": obj.get("prefix"),
                "notePosition": obj.get("notePosition"),
                "isExpanded": obj.get("isExpanded"),
                "utcDateModified": obj.get("utcDateModified"),
            }
        )
        return _obj
