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

class RateLimitConfig(BaseModel):
    """
    Rate limiting configuration for public retriever API.  Controls how many requests can be made to the public endpoint to prevent abuse and manage infrastructure costs.  You can either use a preset tier or specify custom limits. If both tier and custom limits are provided, custom limits override the tier defaults.
    """ # noqa: E501
    enabled: Optional[StrictBool] = Field(default=True, description="Enable rate limiting for this published retriever. Set to False to disable all rate limiting (not recommended for public retrievers).")
    tier: Optional[RateLimitTier] = Field(default=None, description="Rate limit tier preset. STANDARD (default): 60/min, 1k/hour, 10k/day. ELEVATED: 300/min, 10k/hour, 100k/day. ENTERPRISE: 1000/min, 50k/hour, 500k/day. UNLIMITED: No limits. Custom limits override tier defaults.")
    requests_per_minute: Optional[Annotated[int, Field(strict=True, ge=1)]] = Field(default=None, description="Maximum requests per minute (OPTIONAL). If not specified, uses tier default. If specified, overrides tier default.")
    requests_per_hour: Optional[Annotated[int, Field(strict=True, ge=1)]] = Field(default=None, description="Maximum requests per hour (OPTIONAL). If not specified, uses tier default. If specified, overrides tier default.")
    requests_per_day: Optional[Annotated[int, Field(strict=True, ge=1)]] = Field(default=None, description="Maximum requests per day (OPTIONAL). If not specified, uses tier default. If specified, overrides tier default.")
    max_results_per_query: Optional[Annotated[int, Field(le=1000, strict=True, ge=1)]] = Field(default=100, description="Maximum number of results per search query (1-1000)")
    enable_ip_limits: Optional[StrictBool] = Field(default=False, description="Enable IP-based rate limiting in addition to retriever-level limits. Useful for preventing individual IPs from monopolizing the retriever.")
    max_requests_per_ip_hour: Optional[Annotated[int, Field(strict=True, ge=1)]] = Field(default=None, description="Maximum requests per IP address per hour (OPTIONAL). Only enforced if enable_ip_limits=True. Recommended: 100-500 for standard tier, 1000+ for elevated/enterprise.")
    __properties: ClassVar[List[str]] = ["enabled", "tier", "requests_per_minute", "requests_per_hour", "requests_per_day", "max_results_per_query", "enable_ip_limits", "max_requests_per_ip_hour"]

    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 RateLimitConfig 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 RateLimitConfig 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,
            "tier": obj.get("tier"),
            "requests_per_minute": obj.get("requests_per_minute"),
            "requests_per_hour": obj.get("requests_per_hour"),
            "requests_per_day": obj.get("requests_per_day"),
            "max_results_per_query": obj.get("max_results_per_query") if obj.get("max_results_per_query") is not None else 100,
            "enable_ip_limits": obj.get("enable_ip_limits") if obj.get("enable_ip_limits") is not None else False,
            "max_requests_per_ip_hour": obj.get("max_requests_per_ip_hour")
        })
        return _obj


