# coding: utf-8

"""
    Skyledge API

    Sky Ledge is an operational insights platform that gives you the visibility and control you need to minimise inefficiencies, oversight, theft, and ultimately, unnecessary costs associated with your operations.

    The version of the OpenAPI document: 1.0.3
    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
from typing import Any, ClassVar, Dict, List, Optional
from skyledge_sdk.models.aggregated_response import AggregatedResponse
from skyledge_sdk.models.asset_response import AssetResponse
from skyledge_sdk.models.date_grouping import DateGrouping
from skyledge_sdk.models.date_range import DateRange
from typing import Optional, Set
from typing_extensions import Self

class AssetsAggregationResponse(BaseModel):
    """
    AssetsAggregationResponse
    """ # noqa: E501
    results: Optional[List[AggregatedResponse]] = None
    grouped_results: Optional[Dict[str, List[AggregatedResponse]]] = Field(default=None, alias="groupedResults")
    assets: Optional[List[AssetResponse]] = None
    date_grouping: Optional[DateGrouping] = Field(default=None, alias="dateGrouping")
    date_range: Optional[DateRange] = Field(default=None, alias="dateRange")
    total_count: Optional[StrictInt] = Field(default=None, alias="totalCount")
    __properties: ClassVar[List[str]] = ["results", "groupedResults", "assets", "dateGrouping", "dateRange", "totalCount"]

    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 AssetsAggregationResponse 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 results (list)
        _items = []
        if self.results:
            for _item_results in self.results:
                if _item_results:
                    _items.append(_item_results.to_dict())
            _dict['results'] = _items
        # override the default output from pydantic by calling `to_dict()` of each value in grouped_results (dict of array)
        _field_dict_of_array = {}
        if self.grouped_results:
            for _key_grouped_results in self.grouped_results:
                if self.grouped_results[_key_grouped_results] is not None:
                    _field_dict_of_array[_key_grouped_results] = [
                        _item.to_dict() for _item in self.grouped_results[_key_grouped_results]
                    ]
            _dict['groupedResults'] = _field_dict_of_array
        # override the default output from pydantic by calling `to_dict()` of each item in assets (list)
        _items = []
        if self.assets:
            for _item_assets in self.assets:
                if _item_assets:
                    _items.append(_item_assets.to_dict())
            _dict['assets'] = _items
        # override the default output from pydantic by calling `to_dict()` of date_grouping
        if self.date_grouping:
            _dict['dateGrouping'] = self.date_grouping.to_dict()
        # override the default output from pydantic by calling `to_dict()` of date_range
        if self.date_range:
            _dict['dateRange'] = self.date_range.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "results": [AggregatedResponse.from_dict(_item) for _item in obj["results"]] if obj.get("results") is not None else None,
            "groupedResults": dict(
                (_k,
                        [AggregatedResponse.from_dict(_item) for _item in _v]
                        if _v is not None
                        else None
                )
                for _k, _v in obj.get("groupedResults", {}).items()
            ),
            "assets": [AssetResponse.from_dict(_item) for _item in obj["assets"]] if obj.get("assets") is not None else None,
            "dateGrouping": DateGrouping.from_dict(obj["dateGrouping"]) if obj.get("dateGrouping") is not None else None,
            "dateRange": DateRange.from_dict(obj["dateRange"]) if obj.get("dateRange") is not None else None,
            "totalCount": obj.get("totalCount")
        })
        return _obj


