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

class RangeBucket(BaseModel):
    """
    Configuration for range-based bucketing.  Groups numeric values into ranges/buckets for histogram-style analysis.  Requirements:     - field: REQUIRED, numeric field to bucket     - boundaries: REQUIRED, list of boundary values defining buckets     - default_bucket: OPTIONAL, name for values outside boundaries  Examples:     - Video duration buckets: [0, 60, 300, 600] creates: 0-60s, 60-300s, 300-600s, 600+s     - View count buckets: [0, 100, 1000, 10000] creates: 0-100, 100-1K, 1K-10K, 10K+ views
    """ # noqa: E501
    var_field: StrictStr = Field(description="Numeric field to create buckets for. REQUIRED, must be a numeric field. Supports dot notation for nested fields. Values will be grouped into ranges defined by boundaries.", alias="field")
    boundaries: Annotated[List[RangeBucketBoundariesInner], Field(min_length=1)] = Field(description="List of boundary values defining bucket ranges. REQUIRED, must be sorted in ascending order. Creates N+1 buckets for N boundaries: [0, 10, 20] creates: <0, 0-10, 10-20, >20. Values on boundaries go into the lower bucket.")
    default_bucket: Optional[StrictStr] = Field(default=None, description="Name for values outside defined boundaries. OPTIONAL, defaults to 'other'. Used for values below min or above max boundary.")
    __properties: ClassVar[List[str]] = ["field", "boundaries", "default_bucket"]

    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 RangeBucket 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 boundaries (list)
        _items = []
        if self.boundaries:
            for _item_boundaries in self.boundaries:
                if _item_boundaries:
                    _items.append(_item_boundaries.to_dict())
            _dict['boundaries'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "field": obj.get("field"),
            "boundaries": [RangeBucketBoundariesInner.from_dict(_item) for _item in obj["boundaries"]] if obj.get("boundaries") is not None else None,
            "default_bucket": obj.get("default_bucket")
        })
        return _obj


