# coding: utf-8

"""
    

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

    The version of the OpenAPI document: 
    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 goodmem._generated.models.context_item import ContextItem
from goodmem._generated.models.hnsw_options import HnswOptions
from goodmem._generated.models.post_processor import PostProcessor
from goodmem._generated.models.space_key import SpaceKey
from typing import Optional, Set
from typing_extensions import Self

class RetrieveMemoryRequest(BaseModel):
    """
    Request body for semantic memory retrieval with optional embedder weight overrides.
    """ # noqa: E501
    message: StrictStr = Field(description="Primary query/message for semantic search.")
    context: Optional[List[ContextItem]] = Field(default=None, description="Optional context items (text or binary) to provide additional context for the search.")
    space_keys: List[SpaceKey] = Field(description="List of spaces to search with optional per-embedder weight overrides.", alias="spaceKeys")
    requested_size: Optional[StrictInt] = Field(default=None, description="Maximum number of memories to retrieve.", alias="requestedSize")
    fetch_memory: Optional[StrictBool] = Field(default=None, description="Whether to include full Memory objects in the response.", alias="fetchMemory")
    fetch_memory_content: Optional[StrictBool] = Field(default=None, description="Whether to include memory content in the response. Requires fetchMemory=true.", alias="fetchMemoryContent")
    hnsw: Optional[HnswOptions] = None
    post_processor: Optional[PostProcessor] = Field(default=None, alias="postProcessor")
    __properties: ClassVar[List[str]] = ["message", "context", "spaceKeys", "requestedSize", "fetchMemory", "fetchMemoryContent", "hnsw", "postProcessor"]

    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 RetrieveMemoryRequest 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 context (list)
        _items = []
        if self.context:
            for _item_context in self.context:
                if _item_context:
                    _items.append(_item_context.to_dict())
            _dict['context'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in space_keys (list)
        _items = []
        if self.space_keys:
            for _item_space_keys in self.space_keys:
                if _item_space_keys:
                    _items.append(_item_space_keys.to_dict())
            _dict['spaceKeys'] = _items
        # override the default output from pydantic by calling `to_dict()` of hnsw
        if self.hnsw:
            _dict['hnsw'] = self.hnsw.to_dict()
        # override the default output from pydantic by calling `to_dict()` of post_processor
        if self.post_processor:
            _dict['postProcessor'] = self.post_processor.to_dict()
        # set to None if context (nullable) is None
        # and model_fields_set contains the field
        if self.context is None and "context" in self.model_fields_set:
            _dict['context'] = None

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "message": obj.get("message"),
            "context": [ContextItem.from_dict(_item) for _item in obj["context"]] if obj.get("context") is not None else None,
            "spaceKeys": [SpaceKey.from_dict(_item) for _item in obj["spaceKeys"]] if obj.get("spaceKeys") is not None else None,
            "requestedSize": obj.get("requestedSize"),
            "fetchMemory": obj.get("fetchMemory"),
            "fetchMemoryContent": obj.get("fetchMemoryContent"),
            "hnsw": HnswOptions.from_dict(obj["hnsw"]) if obj.get("hnsw") is not None else None,
            "postProcessor": PostProcessor.from_dict(obj["postProcessor"]) if obj.get("postProcessor") is not None else None
        })
        return _obj


