# coding: utf-8

"""
    Mender API

    Combined API specification for the features of the different Mender backend services, suitable for code generation applications 

    The version of the OpenAPI document: 1
    Contact: support@mender.io
    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, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from mender_client.models.filter_predicate import FilterPredicate
from mender_client.models.select_attribute import SelectAttribute
from mender_client.models.sort_criteria import SortCriteria
from typing import Optional, Set
from typing_extensions import Self

class SearchParams(BaseModel):
    """
    SearchParams
    """ # noqa: E501
    page: Optional[StrictInt] = Field(default=None, description="Starting page.")
    per_page: Optional[StrictInt] = Field(default=None, description="Number of results per page.")
    device_ids: Optional[List[StrictStr]] = Field(default=None, description="List of device IDs")
    text: Optional[StrictStr] = Field(default=None, description="Free-text search query")
    filters: Optional[List[FilterPredicate]] = Field(default=None, description="List of filter predicates, chained with boolean AND operators to build the search condition definition.")
    sort: Optional[List[SortCriteria]] = Field(default=None, description="List of ordered sort criterias")
    attributes: Optional[List[SelectAttribute]] = Field(default=None, description="List of attributes to select and return")
    __properties: ClassVar[List[str]] = ["page", "per_page", "device_ids", "text", "filters", "sort", "attributes"]

    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 SearchParams 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 filters (list)
        _items = []
        if self.filters:
            for _item_filters in self.filters:
                if _item_filters:
                    _items.append(_item_filters.to_dict())
            _dict['filters'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in sort (list)
        _items = []
        if self.sort:
            for _item_sort in self.sort:
                if _item_sort:
                    _items.append(_item_sort.to_dict())
            _dict['sort'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in attributes (list)
        _items = []
        if self.attributes:
            for _item_attributes in self.attributes:
                if _item_attributes:
                    _items.append(_item_attributes.to_dict())
            _dict['attributes'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "page": obj.get("page"),
            "per_page": obj.get("per_page"),
            "device_ids": obj.get("device_ids"),
            "text": obj.get("text"),
            "filters": [FilterPredicate.from_dict(_item) for _item in obj["filters"]] if obj.get("filters") is not None else None,
            "sort": [SortCriteria.from_dict(_item) for _item in obj["sort"]] if obj.get("sort") is not None else None,
            "attributes": [SelectAttribute.from_dict(_item) for _item in obj["attributes"]] if obj.get("attributes") is not None else None
        })
        return _obj


