# coding: utf-8

"""
    bxb API

    A comprehensive usage-based billing platform API. Manage customers, plans, subscriptions, invoices, payments, wallets, coupons, credit notes, taxes, webhooks, and more.

    The version of the OpenAPI document: 0.5.0
    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, StrictInt
from typing import Any, ClassVar, Dict, List, Optional
from bxb.models.subscription_plan_breakdown import SubscriptionPlanBreakdown
from bxb.models.trend_indicator import TrendIndicator
from typing import Optional, Set
from typing_extensions import Self

class SubscriptionMetricsResponse(BaseModel):
    """
    SubscriptionMetricsResponse
    """ # noqa: E501
    active: StrictInt
    new_this_month: StrictInt
    canceled_this_month: StrictInt
    by_plan: List[SubscriptionPlanBreakdown]
    new_trend: Optional[TrendIndicator] = None
    canceled_trend: Optional[TrendIndicator] = None
    __properties: ClassVar[List[str]] = ["active", "new_this_month", "canceled_this_month", "by_plan", "new_trend", "canceled_trend"]

    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 SubscriptionMetricsResponse 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 by_plan (list)
        _items = []
        if self.by_plan:
            for _item_by_plan in self.by_plan:
                if _item_by_plan:
                    _items.append(_item_by_plan.to_dict())
            _dict['by_plan'] = _items
        # override the default output from pydantic by calling `to_dict()` of new_trend
        if self.new_trend:
            _dict['new_trend'] = self.new_trend.to_dict()
        # override the default output from pydantic by calling `to_dict()` of canceled_trend
        if self.canceled_trend:
            _dict['canceled_trend'] = self.canceled_trend.to_dict()
        # set to None if new_trend (nullable) is None
        # and model_fields_set contains the field
        if self.new_trend is None and "new_trend" in self.model_fields_set:
            _dict['new_trend'] = None

        # set to None if canceled_trend (nullable) is None
        # and model_fields_set contains the field
        if self.canceled_trend is None and "canceled_trend" in self.model_fields_set:
            _dict['canceled_trend'] = None

        return _dict

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

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

        _obj = cls.model_validate({
            "active": obj.get("active"),
            "new_this_month": obj.get("new_this_month"),
            "canceled_this_month": obj.get("canceled_this_month"),
            "by_plan": [SubscriptionPlanBreakdown.from_dict(_item) for _item in obj["by_plan"]] if obj.get("by_plan") is not None else None,
            "new_trend": TrendIndicator.from_dict(obj["new_trend"]) if obj.get("new_trend") is not None else None,
            "canceled_trend": TrendIndicator.from_dict(obj["canceled_trend"]) if obj.get("canceled_trend") is not None else None
        })
        return _obj


