# coding: utf-8

"""
FastAPI

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

The version of the OpenAPI document: 0.1.0
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, StrictInt, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from whiskerrag_client.models.embedding_model_enum import EmbeddingModelEnum
from whiskerrag_client.models.knowledge_source_enum import KnowledgeSourceEnum
from whiskerrag_client.models.knowledge_split_config import KnowledgeSplitConfig
from whiskerrag_client.models.knowledge_type_enum import KnowledgeTypeEnum
from typing import Optional, Set
from typing_extensions import Self


class KnowledgeCreate(BaseModel):
    """
    KnowledgeCreate model for creating knowledge resources. Attributes:     knowledge_type (ResourceType): Type of knowledge resource.     space_id (str): Space ID, example: petercat bot ID.     knowledge_name (str): Name of the knowledge resource.     file_sha (Optional[str]): SHA of the file.     file_size (Optional[int]): Size of the file.     split_config (Optional[dict]): Configuration for splitting the knowledge.     source_data (Optional[str]): Source data of the knowledge.     source_url (Optional[str]): URL of the source.     auth_info (Optional[str]): Authentication information.     embedding_model_name (Optional[str]): Name of the embedding model.     metadata (Optional[dict]): Additional metadata.
    """  # noqa: E501

    source_type: Optional[KnowledgeSourceEnum] = Field(
        default=None, description="source type"
    )
    knowledge_type: Optional[KnowledgeTypeEnum] = Field(
        default=None, description="type of knowledge resource"
    )
    space_id: StrictStr = Field(description="space id, example: petercat bot id")
    knowledge_name: StrictStr = Field(description="name of the knowledge resource")
    file_sha: Optional[StrictStr] = None
    file_size: Optional[StrictInt] = None
    split_config: KnowledgeSplitConfig = Field(
        description="configuration for splitting the knowledge"
    )
    source_data: Optional[StrictStr] = None
    source_url: Optional[Annotated[str, Field(strict=True)]] = None
    auth_info: Optional[StrictStr] = None
    embedding_model_name: Optional[EmbeddingModelEnum] = Field(
        default=None, description="name of the embedding model"
    )
    metadata: Optional[Dict[str, Any]] = Field(
        default=None, description="additional metadata"
    )
    __properties: ClassVar[List[str]] = [
        "source_type",
        "knowledge_type",
        "space_id",
        "knowledge_name",
        "file_sha",
        "file_size",
        "split_config",
        "source_data",
        "source_url",
        "auth_info",
        "embedding_model_name",
        "metadata",
    ]

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

        if not re.match(r"^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$", value):
            raise ValueError(
                r"must validate the regular expression /^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/"
            )
        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 KnowledgeCreate 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 split_config
        if self.split_config:
            _dict["split_config"] = self.split_config.to_dict()
        # set to None if file_sha (nullable) is None
        # and model_fields_set contains the field
        if self.file_sha is None and "file_sha" in self.model_fields_set:
            _dict["file_sha"] = None

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "source_type": obj.get("source_type"),
                "knowledge_type": obj.get("knowledge_type"),
                "space_id": obj.get("space_id"),
                "knowledge_name": obj.get("knowledge_name"),
                "file_sha": obj.get("file_sha"),
                "file_size": obj.get("file_size"),
                "split_config": (
                    KnowledgeSplitConfig.from_dict(obj["split_config"])
                    if obj.get("split_config") is not None
                    else None
                ),
                "source_data": obj.get("source_data"),
                "source_url": obj.get("source_url"),
                "auth_info": obj.get("auth_info"),
                "embedding_model_name": obj.get("embedding_model_name"),
                "metadata": obj.get("metadata"),
            }
        )
        return _obj
