"""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 enum import Enum
import pydantic
from pydantic import model_serializer
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class CommissionCreatedEventEvent(str, Enum):
    COMMISSION_CREATED = "commission.created"


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


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


class CommissionCreatedEventPartnerTypedDict(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)."""
    total_clicks: float
    total_leads: float
    total_conversions: float
    total_sales: float
    total_sale_amount: float
    total_commissions: float
    group_id: NotRequired[Nullable[str]]


class CommissionCreatedEventPartner(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)."""

    total_clicks: Annotated[float, pydantic.Field(alias="totalClicks")]

    total_leads: Annotated[float, pydantic.Field(alias="totalLeads")]

    total_conversions: Annotated[float, pydantic.Field(alias="totalConversions")]

    total_sales: Annotated[float, pydantic.Field(alias="totalSales")]

    total_sale_amount: Annotated[float, pydantic.Field(alias="totalSaleAmount")]

    total_commissions: Annotated[float, pydantic.Field(alias="totalCommissions")]

    group_id: Annotated[OptionalNullable[str], pydantic.Field(alias="groupId")] = UNSET

    @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 CommissionCreatedEventCustomerTypedDict(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 CommissionCreatedEventCustomer(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 CommissionCreatedEventLinkTypedDict(TypedDict):
    id: str
    r"""The unique ID of the short link."""
    short_link: str
    r"""The full URL of the short link, including the https protocol (e.g. `https://dub.sh/try`)."""
    domain: str
    r"""The domain of the short link. If not provided, the primary domain for the workspace will be used (or `dub.sh` if the workspace has no domains)."""
    key: str
    r"""The short link slug. If not provided, a random 7-character slug will be generated."""


class CommissionCreatedEventLink(BaseModel):
    id: str
    r"""The unique ID of the short link."""

    short_link: Annotated[str, pydantic.Field(alias="shortLink")]
    r"""The full URL of the short link, including the https protocol (e.g. `https://dub.sh/try`)."""

    domain: str
    r"""The domain of the short link. If not provided, the primary domain for the workspace will be used (or `dub.sh` if the workspace has no domains)."""

    key: str
    r"""The short link slug. If not provided, a random 7-character slug will be generated."""


class CommissionCreatedEventDataTypedDict(TypedDict):
    id: str
    r"""The commission's unique ID on Dub."""
    amount: float
    earnings: float
    currency: str
    status: CommissionCreatedEventStatus
    invoice_id: Nullable[str]
    description: Nullable[str]
    quantity: float
    created_at: str
    updated_at: str
    partner: CommissionCreatedEventPartnerTypedDict
    link: Nullable[CommissionCreatedEventLinkTypedDict]
    type: NotRequired[CommissionCreatedEventType]
    user_id: NotRequired[Nullable[str]]
    r"""The user who created the manual commission."""
    customer: NotRequired[Nullable[CommissionCreatedEventCustomerTypedDict]]


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

    amount: float

    earnings: float

    currency: str

    status: CommissionCreatedEventStatus

    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: CommissionCreatedEventPartner

    link: Nullable[CommissionCreatedEventLink]

    type: Optional[CommissionCreatedEventType] = None

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

    customer: OptionalNullable[CommissionCreatedEventCustomer] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["type", "userId", "customer"])
        nullable_fields = set(
            ["invoiceId", "description", "userId", "customer", "link"]
        )
        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 CommissionCreatedEventTypedDict(TypedDict):
    r"""Triggered when a commission is created for a partner."""

    id: str
    event: CommissionCreatedEventEvent
    created_at: str
    data: CommissionCreatedEventDataTypedDict


class CommissionCreatedEvent(BaseModel):
    r"""Triggered when a commission is created for a partner."""

    id: str

    event: CommissionCreatedEventEvent

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

    data: CommissionCreatedEventData
