# 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 mixpeek.models.and_inner2 import AndInner2
from typing import Optional, Set
from typing_extensions import Self

class LogicalOperator2(BaseModel):
    """
    Represents a logical operation (AND, OR, NOT) on filter conditions.  Allows nesting with a defined depth limit.  Also supports shorthand syntax where field names can be passed directly as key-value pairs for equality filtering (e.g., {\"metadata.title\": \"value\"}).
    """ # noqa: E501
    var_and: Optional[List[AndInner2]] = Field(default=None, description="Logical AND operation - all conditions must be true", alias="AND")
    var_or: Optional[List[AndInner2]] = Field(default=None, description="Logical OR operation - at least one condition must be true", alias="OR")
    var_not: Optional[List[AndInner2]] = Field(default=None, description="Logical NOT operation - all conditions must be false", alias="NOT")
    case_sensitive: Optional[StrictBool] = Field(default=False, description="Whether to perform case-sensitive matching")
    additional_properties: Dict[str, Any] = {}
    __properties: ClassVar[List[str]] = ["AND", "OR", "NOT", "case_sensitive"]

    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 LogicalOperator2 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 var_and (list)
        _items = []
        if self.var_and:
            for _item_var_and in self.var_and:
                if _item_var_and:
                    _items.append(_item_var_and.to_dict())
            _dict['AND'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in var_or (list)
        _items = []
        if self.var_or:
            for _item_var_or in self.var_or:
                if _item_var_or:
                    _items.append(_item_var_or.to_dict())
            _dict['OR'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in var_not (list)
        _items = []
        if self.var_not:
            for _item_var_not in self.var_not:
                if _item_var_not:
                    _items.append(_item_var_not.to_dict())
            _dict['NOT'] = _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 LogicalOperator2 from a dict"""
        if obj is None:
            return None

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

        _obj = cls.model_validate({
            "AND": [AndInner2.from_dict(_item) for _item in obj["AND"]] if obj.get("AND") is not None else None,
            "OR": [AndInner2.from_dict(_item) for _item in obj["OR"]] if obj.get("OR") is not None else None,
            "NOT": [AndInner2.from_dict(_item) for _item in obj["NOT"]] if obj.get("NOT") is not None else None,
            "case_sensitive": obj.get("case_sensitive") if obj.get("case_sensitive") is not None else False
        })
        # 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


