# coding: utf-8

"""
    FlexPrice API

    FlexPrice API Service

    The version of the OpenAPI document: 1.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, Field, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from flexprice.models.dto_credit_grant_response import DtoCreditGrantResponse
from flexprice.models.dto_invoice_response import DtoInvoiceResponse
from flexprice.models.dto_proration_details import DtoProrationDetails
from flexprice.models.dto_subscription_summary import DtoSubscriptionSummary
from flexprice.models.types_subscription_change_type import TypesSubscriptionChangeType
from typing import Optional, Set
from typing_extensions import Self

class DtoSubscriptionChangeExecuteResponse(BaseModel):
    """
    Response after successfully executing a subscription plan change
    """ # noqa: E501
    change_type: Optional[TypesSubscriptionChangeType] = None
    credit_grants: Optional[List[DtoCreditGrantResponse]] = Field(default=None, description="credit_grants contains any credit grants created for proration credits")
    effective_date: Optional[StrictStr] = Field(default=None, description="effective_date is when the change took effect")
    invoice: Optional[DtoInvoiceResponse] = None
    metadata: Optional[Dict[str, StrictStr]] = Field(default=None, description="metadata from the request")
    new_subscription: Optional[DtoSubscriptionSummary] = None
    old_subscription: Optional[DtoSubscriptionSummary] = None
    proration_applied: Optional[DtoProrationDetails] = None
    __properties: ClassVar[List[str]] = ["change_type", "credit_grants", "effective_date", "invoice", "metadata", "new_subscription", "old_subscription", "proration_applied"]

    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 DtoSubscriptionChangeExecuteResponse 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 credit_grants (list)
        _items = []
        if self.credit_grants:
            for _item_credit_grants in self.credit_grants:
                if _item_credit_grants:
                    _items.append(_item_credit_grants.to_dict())
            _dict['credit_grants'] = _items
        # override the default output from pydantic by calling `to_dict()` of invoice
        if self.invoice:
            _dict['invoice'] = self.invoice.to_dict()
        # override the default output from pydantic by calling `to_dict()` of new_subscription
        if self.new_subscription:
            _dict['new_subscription'] = self.new_subscription.to_dict()
        # override the default output from pydantic by calling `to_dict()` of old_subscription
        if self.old_subscription:
            _dict['old_subscription'] = self.old_subscription.to_dict()
        # override the default output from pydantic by calling `to_dict()` of proration_applied
        if self.proration_applied:
            _dict['proration_applied'] = self.proration_applied.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "change_type": obj.get("change_type"),
            "credit_grants": [DtoCreditGrantResponse.from_dict(_item) for _item in obj["credit_grants"]] if obj.get("credit_grants") is not None else None,
            "effective_date": obj.get("effective_date"),
            "invoice": DtoInvoiceResponse.from_dict(obj["invoice"]) if obj.get("invoice") is not None else None,
            "metadata": obj.get("metadata"),
            "new_subscription": DtoSubscriptionSummary.from_dict(obj["new_subscription"]) if obj.get("new_subscription") is not None else None,
            "old_subscription": DtoSubscriptionSummary.from_dict(obj["old_subscription"]) if obj.get("old_subscription") is not None else None,
            "proration_applied": DtoProrationDetails.from_dict(obj["proration_applied"]) if obj.get("proration_applied") is not None else None
        })
        return _obj


