# 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, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from flexprice.models.creditnote_credit_note_line_item import CreditnoteCreditNoteLineItem
from flexprice.models.customer_customer import CustomerCustomer
from flexprice.models.dto_invoice_response import DtoInvoiceResponse
from flexprice.models.dto_subscription_response import DtoSubscriptionResponse
from flexprice.models.types_credit_note_reason import TypesCreditNoteReason
from flexprice.models.types_credit_note_status import TypesCreditNoteStatus
from flexprice.models.types_credit_note_type import TypesCreditNoteType
from flexprice.models.types_payment_status import TypesPaymentStatus
from flexprice.models.types_status import TypesStatus
from typing import Optional, Set
from typing_extensions import Self

class DtoCreditNoteResponse(BaseModel):
    """
    DtoCreditNoteResponse
    """ # noqa: E501
    created_at: Optional[StrictStr] = None
    created_by: Optional[StrictStr] = None
    credit_note_number: Optional[StrictStr] = Field(default=None, description="credit_note_number is the unique identifier for credit notes")
    credit_note_status: Optional[TypesCreditNoteStatus] = None
    credit_note_type: Optional[TypesCreditNoteType] = None
    currency: Optional[StrictStr] = Field(default=None, description="currency is the three-letter ISO currency code (e.g., USD, EUR) for the credit note")
    customer: Optional[CustomerCustomer] = None
    customer_id: Optional[StrictStr] = Field(default=None, description="customer_id is the unique identifier of the customer who owns this credit note")
    environment_id: Optional[StrictStr] = Field(default=None, description="environment_id is the unique identifier of the environment this credit note belongs to")
    finalized_at: Optional[StrictStr] = Field(default=None, description="finalized_at is the timestamp when the credit note was finalized")
    id: Optional[StrictStr] = Field(default=None, description="id is the unique identifier for the credit note")
    idempotency_key: Optional[StrictStr] = Field(default=None, description="idempotency_key is an optional key used to prevent duplicate credit note creation")
    invoice: Optional[DtoInvoiceResponse] = None
    invoice_id: Optional[StrictStr] = Field(default=None, description="invoice_id is the id of the invoice resource that this credit note is applied to")
    line_items: Optional[List[CreditnoteCreditNoteLineItem]] = Field(default=None, description="line_items contains all of the line items associated with this credit note")
    memo: Optional[StrictStr] = Field(default=None, description="memo is an optional memo supplied on the credit note")
    metadata: Optional[Dict[str, StrictStr]] = None
    reason: Optional[TypesCreditNoteReason] = None
    refund_status: Optional[TypesPaymentStatus] = None
    status: Optional[TypesStatus] = None
    subscription: Optional[DtoSubscriptionResponse] = None
    subscription_id: Optional[StrictStr] = Field(default=None, description="subscription_id is the optional unique identifier of the subscription related to this credit note")
    tenant_id: Optional[StrictStr] = None
    total_amount: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="total_amount is the total including creditable invoice-level discounts or minimums, and tax")
    updated_at: Optional[StrictStr] = None
    updated_by: Optional[StrictStr] = None
    voided_at: Optional[StrictStr] = Field(default=None, description="voided_at is the timestamp when the credit note was voided")
    __properties: ClassVar[List[str]] = ["created_at", "created_by", "credit_note_number", "credit_note_status", "credit_note_type", "currency", "customer", "customer_id", "environment_id", "finalized_at", "id", "idempotency_key", "invoice", "invoice_id", "line_items", "memo", "metadata", "reason", "refund_status", "status", "subscription", "subscription_id", "tenant_id", "total_amount", "updated_at", "updated_by", "voided_at"]

    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 DtoCreditNoteResponse 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 customer
        if self.customer:
            _dict['customer'] = self.customer.to_dict()
        # 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 each item in line_items (list)
        _items = []
        if self.line_items:
            for _item_line_items in self.line_items:
                if _item_line_items:
                    _items.append(_item_line_items.to_dict())
            _dict['line_items'] = _items
        # override the default output from pydantic by calling `to_dict()` of subscription
        if self.subscription:
            _dict['subscription'] = self.subscription.to_dict()
        return _dict

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

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

        _obj = cls.model_validate({
            "created_at": obj.get("created_at"),
            "created_by": obj.get("created_by"),
            "credit_note_number": obj.get("credit_note_number"),
            "credit_note_status": obj.get("credit_note_status"),
            "credit_note_type": obj.get("credit_note_type"),
            "currency": obj.get("currency"),
            "customer": CustomerCustomer.from_dict(obj["customer"]) if obj.get("customer") is not None else None,
            "customer_id": obj.get("customer_id"),
            "environment_id": obj.get("environment_id"),
            "finalized_at": obj.get("finalized_at"),
            "id": obj.get("id"),
            "idempotency_key": obj.get("idempotency_key"),
            "invoice": DtoInvoiceResponse.from_dict(obj["invoice"]) if obj.get("invoice") is not None else None,
            "invoice_id": obj.get("invoice_id"),
            "line_items": [CreditnoteCreditNoteLineItem.from_dict(_item) for _item in obj["line_items"]] if obj.get("line_items") is not None else None,
            "memo": obj.get("memo"),
            "metadata": obj.get("metadata"),
            "reason": obj.get("reason"),
            "refund_status": obj.get("refund_status"),
            "status": obj.get("status"),
            "subscription": DtoSubscriptionResponse.from_dict(obj["subscription"]) if obj.get("subscription") is not None else None,
            "subscription_id": obj.get("subscription_id"),
            "tenant_id": obj.get("tenant_id"),
            "total_amount": obj.get("total_amount"),
            "updated_at": obj.get("updated_at"),
            "updated_by": obj.get("updated_by"),
            "voided_at": obj.get("voided_at")
        })
        return _obj


