# coding: utf-8

"""
    Mixpeek API

    This is the Mixpeek API, providing access to various endpoints for data processing and retrieval.

    The version of the OpenAPI document: 1.3.0
    Contact: info@mixpeek.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, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from mixpeek.models.feature_model import FeatureModel
from mixpeek.models.lineage_step import LineageStep
from typing import Optional, Set
from typing_extensions import Self

class DocumentCreateRequest(BaseModel):
    """
    Request model for creating a document.
    """ # noqa: E501
    collection_id: StrictStr = Field(description="ID of the collection the document belongs to.")
    root_object_id: Optional[StrictStr] = Field(default=None, description="Optional denormalized root object identifier provided during creation.")
    root_bucket_id: Optional[StrictStr] = Field(default=None, description="Optional denormalized bucket identifier provided during creation.")
    source_type: Optional[StrictStr] = Field(default=None, description="Optional immediate parent type for the document.")
    source_collection_id: Optional[StrictStr] = Field(default=None, description="Optional parent collection identifier when sourced from a collection.")
    source_document_id: Optional[StrictStr] = Field(default=None, description="Optional parent document identifier when sourced from a collection.")
    source_object_id: Optional[StrictStr] = Field(default=None, description="Optional parent object identifier when sourced directly from a bucket.")
    lineage_path: Optional[StrictStr] = Field(default=None, description="Optional materialized lineage path to set during creation.")
    lineage_chain: Optional[List[LineageStep]] = Field(default=None, description="Processing steps from root object to this document. Recommended for decomposition trees.")
    document_schema_version: Optional[StrictStr] = Field(default=None, description="Optional document schema version (v1 or v2). If not provided, uses system default.")
    metadata: Optional[Dict[str, Any]] = Field(default=None, description="Optional metadata dictionary for user-defined fields and custom attributes.")
    features: Optional[List[FeatureModel]] = Field(default=None, description="Features to associate with the document")
    additional_properties: Dict[str, Any] = {}
    __properties: ClassVar[List[str]] = ["collection_id", "root_object_id", "root_bucket_id", "source_type", "source_collection_id", "source_document_id", "source_object_id", "lineage_path", "lineage_chain", "document_schema_version", "metadata", "features"]

    @field_validator('source_type')
    def source_type_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(['bucket', 'collection']):
            raise ValueError("must be one of enum values ('bucket', 'collection')")
        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 DocumentCreateRequest 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.
        * Fields in `self.additional_properties` are added to the output dict.
        """
        excluded_fields: Set[str] = set([
            "additional_properties",
        ])

        _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 lineage_chain (list)
        _items = []
        if self.lineage_chain:
            for _item_lineage_chain in self.lineage_chain:
                if _item_lineage_chain:
                    _items.append(_item_lineage_chain.to_dict())
            _dict['lineage_chain'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in features (list)
        _items = []
        if self.features:
            for _item_features in self.features:
                if _item_features:
                    _items.append(_item_features.to_dict())
            _dict['features'] = _items
        # puts key-value pairs in additional_properties in the top level
        if self.additional_properties is not None:
            for _key, _value in self.additional_properties.items():
                _dict[_key] = _value

        return _dict

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

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

        _obj = cls.model_validate({
            "collection_id": obj.get("collection_id"),
            "root_object_id": obj.get("root_object_id"),
            "root_bucket_id": obj.get("root_bucket_id"),
            "source_type": obj.get("source_type"),
            "source_collection_id": obj.get("source_collection_id"),
            "source_document_id": obj.get("source_document_id"),
            "source_object_id": obj.get("source_object_id"),
            "lineage_path": obj.get("lineage_path"),
            "lineage_chain": [LineageStep.from_dict(_item) for _item in obj["lineage_chain"]] if obj.get("lineage_chain") is not None else None,
            "document_schema_version": obj.get("document_schema_version"),
            "metadata": obj.get("metadata"),
            "features": [FeatureModel.from_dict(_item) for _item in obj["features"]] if obj.get("features") is not None else None
        })
        # store additional fields in additional_properties
        for _key in obj.keys():
            if _key not in cls.__properties:
                _obj.additional_properties[_key] = obj.get(_key)

        return _obj


