"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .creditnote_creditnotelineitem import (
    CreditnoteCreditNoteLineItem,
    CreditnoteCreditNoteLineItemTypedDict,
)
from .creditnotereason import CreditNoteReason
from .creditnotestatus import CreditNoteStatus
from .creditnotetype import CreditNoteType
from .dto_invoiceresponse import DtoInvoiceResponse, DtoInvoiceResponseTypedDict
from .dto_subscriptionresponse import (
    DtoSubscriptionResponse,
    DtoSubscriptionResponseTypedDict,
)
from .github_com_flexprice_flexprice_internal_domain_customer_customer import (
    GithubComFlexpriceFlexpriceInternalDomainCustomerCustomer,
    GithubComFlexpriceFlexpriceInternalDomainCustomerCustomerTypedDict,
)
from .paymentstatus import PaymentStatus
from .status import Status
from flexprice_py.types import BaseModel, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Dict, List, Optional
from typing_extensions import NotRequired, TypedDict


class DtoCreditNoteResponseTypedDict(TypedDict):
    created_at: NotRequired[str]
    created_by: NotRequired[str]
    credit_note_number: NotRequired[str]
    r"""credit_note_number is the unique identifier for credit notes"""
    credit_note_status: NotRequired[CreditNoteStatus]
    credit_note_type: NotRequired[CreditNoteType]
    currency: NotRequired[str]
    r"""currency is the three-letter ISO currency code (e.g., USD, EUR) for the credit note"""
    customer: NotRequired[
        GithubComFlexpriceFlexpriceInternalDomainCustomerCustomerTypedDict
    ]
    customer_id: NotRequired[str]
    r"""customer_id is the unique identifier of the customer who owns this credit note"""
    environment_id: NotRequired[str]
    r"""environment_id is the unique identifier of the environment this credit note belongs to"""
    finalized_at: NotRequired[str]
    r"""finalized_at is the timestamp when the credit note was finalized"""
    id: NotRequired[str]
    r"""id is the unique identifier for the credit note"""
    idempotency_key: NotRequired[str]
    r"""idempotency_key is an optional key used to prevent duplicate credit note creation"""
    invoice: NotRequired[DtoInvoiceResponseTypedDict]
    invoice_id: NotRequired[str]
    r"""invoice_id is the id of the invoice resource that this credit note is applied to"""
    line_items: NotRequired[List[CreditnoteCreditNoteLineItemTypedDict]]
    r"""line_items contains all of the line items associated with this credit note"""
    memo: NotRequired[str]
    r"""memo is an optional memo supplied on the credit note"""
    metadata: NotRequired[Dict[str, str]]
    reason: NotRequired[CreditNoteReason]
    refund_status: NotRequired[PaymentStatus]
    status: NotRequired[Status]
    subscription: NotRequired[DtoSubscriptionResponseTypedDict]
    subscription_id: NotRequired[str]
    r"""subscription_id is the optional unique identifier of the subscription related to this credit note"""
    tenant_id: NotRequired[str]
    total_amount: NotRequired[str]
    r"""total_amount is the total including creditable invoice-level discounts or minimums, and tax"""
    updated_at: NotRequired[str]
    updated_by: NotRequired[str]
    voided_at: NotRequired[str]
    r"""voided_at is the timestamp when the credit note was voided"""


class DtoCreditNoteResponse(BaseModel):
    created_at: Optional[str] = None

    created_by: Optional[str] = None

    credit_note_number: Optional[str] = None
    r"""credit_note_number is the unique identifier for credit notes"""

    credit_note_status: Optional[CreditNoteStatus] = None

    credit_note_type: Optional[CreditNoteType] = None

    currency: Optional[str] = None
    r"""currency is the three-letter ISO currency code (e.g., USD, EUR) for the credit note"""

    customer: Optional[GithubComFlexpriceFlexpriceInternalDomainCustomerCustomer] = None

    customer_id: Optional[str] = None
    r"""customer_id is the unique identifier of the customer who owns this credit note"""

    environment_id: Optional[str] = None
    r"""environment_id is the unique identifier of the environment this credit note belongs to"""

    finalized_at: Optional[str] = None
    r"""finalized_at is the timestamp when the credit note was finalized"""

    id: Optional[str] = None
    r"""id is the unique identifier for the credit note"""

    idempotency_key: Optional[str] = None
    r"""idempotency_key is an optional key used to prevent duplicate credit note creation"""

    invoice: Optional[DtoInvoiceResponse] = None

    invoice_id: Optional[str] = None
    r"""invoice_id is the id of the invoice resource that this credit note is applied to"""

    line_items: Optional[List[CreditnoteCreditNoteLineItem]] = None
    r"""line_items contains all of the line items associated with this credit note"""

    memo: Optional[str] = None
    r"""memo is an optional memo supplied on the credit note"""

    metadata: Optional[Dict[str, str]] = None

    reason: Optional[CreditNoteReason] = None

    refund_status: Optional[PaymentStatus] = None

    status: Optional[Status] = None

    subscription: Optional[DtoSubscriptionResponse] = None

    subscription_id: Optional[str] = None
    r"""subscription_id is the optional unique identifier of the subscription related to this credit note"""

    tenant_id: Optional[str] = None

    total_amount: Optional[str] = None
    r"""total_amount is the total including creditable invoice-level discounts or minimums, and tax"""

    updated_at: Optional[str] = None

    updated_by: Optional[str] = None

    voided_at: Optional[str] = None
    r"""voided_at is the timestamp when the credit note was voided"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "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",
            ]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)

            if val != UNSET_SENTINEL:
                if val is not None or k not in optional_fields:
                    m[k] = val

        return m
