# 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, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from mixpeek.models.cache_statistics import CacheStatistics
from typing import Optional, Set
from typing_extensions import Self

class CacheConfig(BaseModel):
    """
    Configuration for retriever result caching.  Controls how retriever results are cached to improve performance and reduce redundant compute.  Caching can be configured at specific stages in the retriever pipeline. If no stages are specified, the final results are cached by default.
    """ # noqa: E501
    enabled: Optional[StrictBool] = Field(default=True, description="Whether caching is enabled for this retriever")
    ttl_seconds: Optional[Annotated[int, Field(strict=True, ge=0)]] = Field(default=3600, description="Time-to-live for cached results in seconds. Default: 1 hour")
    cache_stage_names: Optional[List[StrictStr]] = Field(default=None, description="List of stage names to cache results after. Stage names must match the stage_name field in the retriever's stages. If not specified, caches the final results after all stages. Examples: ['semantic_search'], ['semantic_search', 'rerank']")
    exclude_fields: Optional[List[StrictStr]] = Field(default=None, description="Fields to exclude from caching (e.g., PII fields)")
    stats: Optional[CacheStatistics] = Field(default=None, description="Cache performance statistics")
    __properties: ClassVar[List[str]] = ["enabled", "ttl_seconds", "cache_stage_names", "exclude_fields", "stats"]

    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 CacheConfig 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 stats
        if self.stats:
            _dict['stats'] = self.stats.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "enabled": obj.get("enabled") if obj.get("enabled") is not None else True,
            "ttl_seconds": obj.get("ttl_seconds") if obj.get("ttl_seconds") is not None else 3600,
            "cache_stage_names": obj.get("cache_stage_names"),
            "exclude_fields": obj.get("exclude_fields"),
            "stats": CacheStatistics.from_dict(obj["stats"]) if obj.get("stats") is not None else None
        })
        return _obj


