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

from __future__ import annotations
from dub.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from dub.utils import FieldMetadata, QueryParamMetadata
from enum import Enum
import pydantic
from pydantic import model_serializer
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class Type(str, Enum):
    CLICK = "click"
    LEAD = "lead"
    SALE = "sale"
    CUSTOM = "custom"


class QueryParamStatus(str, Enum):
    r"""Filter the list of commissions by their corresponding status."""

    PENDING = "pending"
    PROCESSED = "processed"
    PAID = "paid"
    REFUNDED = "refunded"
    DUPLICATE = "duplicate"
    FRAUD = "fraud"
    CANCELED = "canceled"


class ListCommissionsQueryParamSortBy(str, Enum):
    r"""The field to sort the list of commissions by."""

    CREATED_AT = "createdAt"
    AMOUNT = "amount"


class ListCommissionsQueryParamSortOrder(str, Enum):
    r"""The sort order for the list of commissions."""

    ASC = "asc"
    DESC = "desc"


class ListCommissionsQueryParamInterval(str, Enum):
    r"""The interval to retrieve commissions for."""

    TWENTY_FOURH = "24h"
    SEVEND = "7d"
    THIRTYD = "30d"
    NINETYD = "90d"
    ONEY = "1y"
    MTD = "mtd"
    QTD = "qtd"
    YTD = "ytd"
    ALL = "all"


class ListCommissionsRequestTypedDict(TypedDict):
    type: NotRequired[Type]
    customer_id: NotRequired[str]
    r"""Filter the list of commissions by the associated customer."""
    payout_id: NotRequired[str]
    r"""Filter the list of commissions by the associated payout."""
    partner_id: NotRequired[str]
    r"""Filter the list of commissions by the associated partner. When specified, takes precedence over `tenantId`."""
    tenant_id: NotRequired[str]
    r"""Filter the list of commissions by the associated partner's `tenantId` (their unique ID within your database)."""
    group_id: NotRequired[str]
    r"""Filter the list of commissions by the associated partner group."""
    invoice_id: NotRequired[str]
    r"""Filter the list of commissions by the associated invoice. Since invoiceId is unique on a per-program basis, this will only return one commission per invoice."""
    status: NotRequired[QueryParamStatus]
    r"""Filter the list of commissions by their corresponding status."""
    sort_by: NotRequired[ListCommissionsQueryParamSortBy]
    r"""The field to sort the list of commissions by."""
    sort_order: NotRequired[ListCommissionsQueryParamSortOrder]
    r"""The sort order for the list of commissions."""
    interval: NotRequired[ListCommissionsQueryParamInterval]
    r"""The interval to retrieve commissions for."""
    start: NotRequired[str]
    r"""The start date of the date range to filter the commissions by."""
    end: NotRequired[str]
    r"""The end date of the date range to filter the commissions by."""
    timezone: NotRequired[str]
    page: NotRequired[float]
    r"""The page number for pagination."""
    page_size: NotRequired[float]
    r"""The number of items per page."""


class ListCommissionsRequest(BaseModel):
    type: Annotated[
        Optional[Type],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    customer_id: Annotated[
        Optional[str],
        pydantic.Field(alias="customerId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated customer."""

    payout_id: Annotated[
        Optional[str],
        pydantic.Field(alias="payoutId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated payout."""

    partner_id: Annotated[
        Optional[str],
        pydantic.Field(alias="partnerId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated partner. When specified, takes precedence over `tenantId`."""

    tenant_id: Annotated[
        Optional[str],
        pydantic.Field(alias="tenantId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated partner's `tenantId` (their unique ID within your database)."""

    group_id: Annotated[
        Optional[str],
        pydantic.Field(alias="groupId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated partner group."""

    invoice_id: Annotated[
        Optional[str],
        pydantic.Field(alias="invoiceId"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by the associated invoice. Since invoiceId is unique on a per-program basis, this will only return one commission per invoice."""

    status: Annotated[
        Optional[QueryParamStatus],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""Filter the list of commissions by their corresponding status."""

    sort_by: Annotated[
        Optional[ListCommissionsQueryParamSortBy],
        pydantic.Field(alias="sortBy"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = ListCommissionsQueryParamSortBy.CREATED_AT
    r"""The field to sort the list of commissions by."""

    sort_order: Annotated[
        Optional[ListCommissionsQueryParamSortOrder],
        pydantic.Field(alias="sortOrder"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = ListCommissionsQueryParamSortOrder.DESC
    r"""The sort order for the list of commissions."""

    interval: Annotated[
        Optional[ListCommissionsQueryParamInterval],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = ListCommissionsQueryParamInterval.ALL
    r"""The interval to retrieve commissions for."""

    start: Annotated[
        Optional[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""The start date of the date range to filter the commissions by."""

    end: Annotated[
        Optional[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None
    r"""The end date of the date range to filter the commissions by."""

    timezone: Annotated[
        Optional[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = None

    page: Annotated[
        Optional[float],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 1
    r"""The page number for pagination."""

    page_size: Annotated[
        Optional[float],
        pydantic.Field(alias="pageSize"),
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 100
    r"""The number of items per page."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "type",
                "customerId",
                "payoutId",
                "partnerId",
                "tenantId",
                "groupId",
                "invoiceId",
                "status",
                "sortBy",
                "sortOrder",
                "interval",
                "start",
                "end",
                "timezone",
                "page",
                "pageSize",
            ]
        )
        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


class ListCommissionsType(str, Enum):
    CLICK = "click"
    LEAD = "lead"
    SALE = "sale"
    CUSTOM = "custom"


class ListCommissionsStatus(str, Enum):
    PENDING = "pending"
    PROCESSED = "processed"
    PAID = "paid"
    REFUNDED = "refunded"
    DUPLICATE = "duplicate"
    FRAUD = "fraud"
    CANCELED = "canceled"


class ListCommissionsPartnerTypedDict(TypedDict):
    id: str
    r"""The partner's unique ID on Dub."""
    name: str
    r"""The partner's full legal name."""
    email: Nullable[str]
    r"""The partner's email address. Should be a unique value across Dub."""
    image: Nullable[str]
    r"""The partner's avatar image."""
    payouts_enabled_at: Nullable[str]
    r"""The date when the partner enabled payouts."""
    country: Nullable[str]
    r"""The partner's country (required for tax purposes)."""
    group_id: NotRequired[Nullable[str]]
    r"""The partner's group ID on Dub."""


class ListCommissionsPartner(BaseModel):
    id: str
    r"""The partner's unique ID on Dub."""

    name: str
    r"""The partner's full legal name."""

    email: Nullable[str]
    r"""The partner's email address. Should be a unique value across Dub."""

    image: Nullable[str]
    r"""The partner's avatar image."""

    payouts_enabled_at: Annotated[
        Nullable[str], pydantic.Field(alias="payoutsEnabledAt")
    ]
    r"""The date when the partner enabled payouts."""

    country: Nullable[str]
    r"""The partner's country (required for tax purposes)."""

    group_id: Annotated[OptionalNullable[str], pydantic.Field(alias="groupId")] = UNSET
    r"""The partner's group ID on Dub."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["groupId"])
        nullable_fields = set(
            ["email", "image", "payoutsEnabledAt", "country", "groupId"]
        )
        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


class ListCommissionsCustomerTypedDict(TypedDict):
    id: str
    r"""The unique ID of the customer. You may use either the customer's `id` on Dub (obtained via `/customers` endpoint) or their `externalId` (unique ID within your system, prefixed with `ext_`, e.g. `ext_123`)."""
    name: str
    r"""Name of the customer."""
    external_id: str
    r"""Unique identifier for the customer in the client's app."""
    created_at: str
    r"""The date the customer was created (usually the signup date or trial start date)."""
    email: NotRequired[Nullable[str]]
    r"""Email of the customer."""
    avatar: NotRequired[Nullable[str]]
    r"""Avatar URL of the customer."""
    stripe_customer_id: NotRequired[Nullable[str]]
    r"""The customer's Stripe customer ID. This is useful for attributing recurring sale events to the partner who referred the customer."""
    country: NotRequired[Nullable[str]]
    r"""Country of the customer."""
    sales: NotRequired[Nullable[float]]
    r"""Total number of sales for the customer."""
    sale_amount: NotRequired[Nullable[float]]
    r"""Total amount of sales for the customer."""
    first_sale_at: NotRequired[Nullable[str]]
    r"""The date the customer made their first sale. Useful for calculating the time to first sale and LTV."""
    subscription_canceled_at: NotRequired[Nullable[str]]
    r"""The date the customer canceled their subscription. Useful for calculating LTV and churn rate."""


class ListCommissionsCustomer(BaseModel):
    id: str
    r"""The unique ID of the customer. You may use either the customer's `id` on Dub (obtained via `/customers` endpoint) or their `externalId` (unique ID within your system, prefixed with `ext_`, e.g. `ext_123`)."""

    name: str
    r"""Name of the customer."""

    external_id: Annotated[str, pydantic.Field(alias="externalId")]
    r"""Unique identifier for the customer in the client's app."""

    created_at: Annotated[str, pydantic.Field(alias="createdAt")]
    r"""The date the customer was created (usually the signup date or trial start date)."""

    email: OptionalNullable[str] = UNSET
    r"""Email of the customer."""

    avatar: OptionalNullable[str] = UNSET
    r"""Avatar URL of the customer."""

    stripe_customer_id: Annotated[
        OptionalNullable[str], pydantic.Field(alias="stripeCustomerId")
    ] = UNSET
    r"""The customer's Stripe customer ID. This is useful for attributing recurring sale events to the partner who referred the customer."""

    country: OptionalNullable[str] = UNSET
    r"""Country of the customer."""

    sales: OptionalNullable[float] = UNSET
    r"""Total number of sales for the customer."""

    sale_amount: Annotated[
        OptionalNullable[float], pydantic.Field(alias="saleAmount")
    ] = UNSET
    r"""Total amount of sales for the customer."""

    first_sale_at: Annotated[
        OptionalNullable[str], pydantic.Field(alias="firstSaleAt")
    ] = UNSET
    r"""The date the customer made their first sale. Useful for calculating the time to first sale and LTV."""

    subscription_canceled_at: Annotated[
        OptionalNullable[str], pydantic.Field(alias="subscriptionCanceledAt")
    ] = UNSET
    r"""The date the customer canceled their subscription. Useful for calculating LTV and churn rate."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "email",
                "avatar",
                "stripeCustomerId",
                "country",
                "sales",
                "saleAmount",
                "firstSaleAt",
                "subscriptionCanceledAt",
            ]
        )
        nullable_fields = set(
            [
                "email",
                "avatar",
                "stripeCustomerId",
                "country",
                "sales",
                "saleAmount",
                "firstSaleAt",
                "subscriptionCanceledAt",
            ]
        )
        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


class ListCommissionsResponseBodyTypedDict(TypedDict):
    id: str
    r"""The commission's unique ID on Dub."""
    amount: float
    earnings: float
    currency: str
    status: ListCommissionsStatus
    invoice_id: Nullable[str]
    description: Nullable[str]
    quantity: float
    created_at: str
    updated_at: str
    partner: ListCommissionsPartnerTypedDict
    type: NotRequired[ListCommissionsType]
    user_id: NotRequired[Nullable[str]]
    r"""The user who created the manual commission."""
    customer: NotRequired[Nullable[ListCommissionsCustomerTypedDict]]


class ListCommissionsResponseBody(BaseModel):
    id: str
    r"""The commission's unique ID on Dub."""

    amount: float

    earnings: float

    currency: str

    status: ListCommissionsStatus

    invoice_id: Annotated[Nullable[str], pydantic.Field(alias="invoiceId")]

    description: Nullable[str]

    quantity: float

    created_at: Annotated[str, pydantic.Field(alias="createdAt")]

    updated_at: Annotated[str, pydantic.Field(alias="updatedAt")]

    partner: ListCommissionsPartner

    type: Optional[ListCommissionsType] = None

    user_id: Annotated[OptionalNullable[str], pydantic.Field(alias="userId")] = UNSET
    r"""The user who created the manual commission."""

    customer: OptionalNullable[ListCommissionsCustomer] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["type", "userId", "customer"])
        nullable_fields = set(["invoiceId", "description", "userId", "customer"])
        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
