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

from __future__ import annotations
from .creditnotereason import CreditNoteReason
from .dto_createcreditnotelineitemrequest import (
    DtoCreateCreditNoteLineItemRequest,
    DtoCreateCreditNoteLineItemRequestTypedDict,
)
from flexprice.types import BaseModel, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Dict, List, Optional
from typing_extensions import NotRequired, TypedDict


class DtoCreateCreditNoteRequestTypedDict(TypedDict):
    invoice_id: str
    r"""invoice_id is the unique identifier of the invoice this credit note is applied to"""
    reason: CreditNoteReason
    credit_note_number: NotRequired[str]
    r"""credit_note_number is an optional human-readable identifier for the credit note"""
    idempotency_key: NotRequired[str]
    r"""idempotency_key is an optional key used to prevent duplicate credit note creation"""
    line_items: NotRequired[List[DtoCreateCreditNoteLineItemRequestTypedDict]]
    r"""line_items contains the individual line items that make up this credit note (minimum 1 required)"""
    memo: NotRequired[str]
    r"""memo is an optional free-text field for additional notes about the credit note"""
    metadata: NotRequired[Dict[str, str]]
    process_credit_note: NotRequired[bool]
    r"""process_credit_note is a flag to process the credit note after creation"""


class DtoCreateCreditNoteRequest(BaseModel):
    invoice_id: str
    r"""invoice_id is the unique identifier of the invoice this credit note is applied to"""

    reason: CreditNoteReason

    credit_note_number: Optional[str] = None
    r"""credit_note_number is an optional human-readable identifier for the credit note"""

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

    line_items: Optional[List[DtoCreateCreditNoteLineItemRequest]] = None
    r"""line_items contains the individual line items that make up this credit note (minimum 1 required)"""

    memo: Optional[str] = None
    r"""memo is an optional free-text field for additional notes about the credit note"""

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

    process_credit_note: Optional[bool] = True
    r"""process_credit_note is a flag to process the credit note after creation"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "credit_note_number",
                "idempotency_key",
                "line_items",
                "memo",
                "metadata",
                "process_credit_note",
            ]
        )
        serialized = handler(self)
        m = {}

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

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

        return m
