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

from __future__ import annotations
from .refundtargettype import RefundTargetType
from gr4vy.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class TransactionRefundCreateTypedDict(TypedDict):
    amount: NotRequired[Nullable[int]]
    r"""The amount to refund, in the smallest currency unit (e.g., cents). If omitted, a full refund will be requested."""
    target_type: NotRequired[RefundTargetType]
    target_id: NotRequired[Nullable[str]]
    r"""The optional ID of the instrument to refund for. This is only required when the `target_type` is set to `gift-card-redemption`."""
    reason: NotRequired[Nullable[str]]
    r"""An optional reason to attach extra context to the refund request."""
    external_identifier: NotRequired[Nullable[str]]
    r"""An external identifier that can be used to match the refund against your own records."""


class TransactionRefundCreate(BaseModel):
    amount: OptionalNullable[int] = UNSET
    r"""The amount to refund, in the smallest currency unit (e.g., cents). If omitted, a full refund will be requested."""

    target_type: Optional[RefundTargetType] = None

    target_id: OptionalNullable[str] = UNSET
    r"""The optional ID of the instrument to refund for. This is only required when the `target_type` is set to `gift-card-redemption`."""

    reason: OptionalNullable[str] = UNSET
    r"""An optional reason to attach extra context to the refund request."""

    external_identifier: OptionalNullable[str] = UNSET
    r"""An external identifier that can be used to match the refund against your own records."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["amount", "target_type", "target_id", "reason", "external_identifier"]
        )
        nullable_fields = set(["amount", "target_id", "reason", "external_identifier"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

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

        return m
