# -*- coding: utf-8 -*-
# File generated from our OpenAPI spec
from stripe._expandable_field import ExpandableField
from stripe._list_object import ListObject
from stripe._listable_api_resource import ListableAPIResource
from stripe._stripe_object import StripeObject, UntypedStripeObject
from typing import ClassVar, Optional
from typing_extensions import Literal, Unpack, TYPE_CHECKING

if TYPE_CHECKING:
    from stripe._dispute import Dispute
    from stripe._refund import Refund
    from stripe.params.reserve._release_list_params import ReleaseListParams
    from stripe.params.reserve._release_retrieve_params import (
        ReleaseRetrieveParams,
    )
    from stripe.reserve._hold import Hold
    from stripe.reserve._plan import Plan


class Release(ListableAPIResource["Release"]):
    """
    ReserveReleases represent the release of funds from a ReserveHold.
    """

    OBJECT_NAME: ClassVar[Literal["reserve.release"]] = "reserve.release"

    class SourceTransaction(StripeObject):
        dispute: Optional[ExpandableField["Dispute"]]
        """
        The ID of the dispute.
        """
        refund: Optional[ExpandableField["Refund"]]
        """
        The ID of the refund.
        """
        type: Literal["dispute", "refund"]
        """
        The type of source transaction.
        """

    amount: int
    """
    Amount released. A positive integer representing how much is released in the [smallest currency unit](https://docs.stripe.com/currencies#zero-decimal).
    """
    created: int
    """
    Time at which the object was created. Measured in seconds since the Unix epoch.
    """
    created_by: Literal["application", "stripe"]
    """
    Indicates which party created this ReserveRelease.
    """
    currency: str
    """
    Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase. Must be a [supported currency](https://stripe.com/docs/currencies).
    """
    id: str
    """
    Unique identifier for the object.
    """
    livemode: bool
    """
    If the object exists in live mode, the value is `true`. If the object exists in test mode, the value is `false`.
    """
    metadata: Optional[UntypedStripeObject[str]]
    """
    Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format.
    """
    object: Literal["reserve.release"]
    """
    String representing the object's type. Objects of the same type share the same value.
    """
    reason: Literal[
        "bulk_hold_expiry",
        "hold_released_early",
        "hold_reversed",
        "plan_disabled",
    ]
    """
    The reason for the ReserveRelease, indicating why the funds were released.
    """
    released_at: int
    """
    The release timestamp of the funds.
    """
    reserve_hold: Optional[ExpandableField["Hold"]]
    """
    The ReserveHold this ReserveRelease is associated with.
    """
    reserve_plan: Optional[ExpandableField["Plan"]]
    """
    The ReservePlan ID this ReserveRelease is associated with. This field is only populated if a ReserveRelease is created by a ReservePlan disable operation, or from a scheduled ReservedHold expiry.
    """
    source_transaction: Optional[SourceTransaction]

    @classmethod
    def list(
        cls, **params: Unpack["ReleaseListParams"]
    ) -> ListObject["Release"]:
        """
        Returns a list of ReserveReleases previously created. The ReserveReleases are returned in sorted order, with the most recent ReserveReleases appearing first.
        """
        result = cls._static_request(
            "get",
            cls.class_url(),
            params=params,
        )
        if not isinstance(result, ListObject):
            raise TypeError(
                "Expected list object from API, got %s"
                % (type(result).__name__)
            )

        return result

    @classmethod
    async def list_async(
        cls, **params: Unpack["ReleaseListParams"]
    ) -> ListObject["Release"]:
        """
        Returns a list of ReserveReleases previously created. The ReserveReleases are returned in sorted order, with the most recent ReserveReleases appearing first.
        """
        result = await cls._static_request_async(
            "get",
            cls.class_url(),
            params=params,
        )
        if not isinstance(result, ListObject):
            raise TypeError(
                "Expected list object from API, got %s"
                % (type(result).__name__)
            )

        return result

    @classmethod
    def retrieve(
        cls, id: str, **params: Unpack["ReleaseRetrieveParams"]
    ) -> "Release":
        """
        Retrieve a ReserveRelease.
        """
        instance = cls(id, **params)
        instance.refresh()
        return instance

    @classmethod
    async def retrieve_async(
        cls, id: str, **params: Unpack["ReleaseRetrieveParams"]
    ) -> "Release":
        """
        Retrieve a ReserveRelease.
        """
        instance = cls(id, **params)
        await instance.refresh_async()
        return instance

    _inner_class_types = {"source_transaction": SourceTransaction}
