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

from __future__ import annotations
from datetime import datetime
from paygentic_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
import pydantic
from pydantic import model_serializer
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class CreateManualLineItemRequestTypedDict(TypedDict):
    display_name: str
    r"""Human-readable label shown on the invoice."""
    currency: str
    r"""ISO 4217 currency code (e.g., USD). Must match the subscription or invoice currency."""
    quantity: float
    r"""Number of units."""
    unit_price: float
    r"""Price per unit as a decimal amount (e.g., 29.99 for $29.99). Can be negative for credits or adjustments."""
    subscription_id: NotRequired[str]
    r"""The subscription ID. Exactly one of subscriptionId or invoiceId must be provided."""
    invoice_id: NotRequired[str]
    r"""The invoice ID to attach this item directly to. Exactly one of subscriptionId or invoiceId must be provided. The invoice must be in ACTIVE or CLOSING state."""
    description: NotRequired[Nullable[str]]
    r"""Optional longer description shown on the invoice."""
    invoice_at: NotRequired[Nullable[datetime]]
    r"""When to collect this item into an invoice. Defaults to now. Ignored when invoiceId is provided."""
    period_start: NotRequired[Nullable[datetime]]
    r"""Start of the billing period for display purposes. Defaults to now."""
    period_end: NotRequired[Nullable[datetime]]
    r"""End of the billing period for display purposes. Defaults to now."""
    idempotency_key: NotRequired[Nullable[str]]
    r"""Optional caller-provided idempotency key. Auto-generated if not provided."""


class CreateManualLineItemRequest(BaseModel):
    display_name: Annotated[str, pydantic.Field(alias="displayName")]
    r"""Human-readable label shown on the invoice."""

    currency: str
    r"""ISO 4217 currency code (e.g., USD). Must match the subscription or invoice currency."""

    quantity: float
    r"""Number of units."""

    unit_price: Annotated[float, pydantic.Field(alias="unitPrice")]
    r"""Price per unit as a decimal amount (e.g., 29.99 for $29.99). Can be negative for credits or adjustments."""

    subscription_id: Annotated[
        Optional[str], pydantic.Field(alias="subscriptionId")
    ] = None
    r"""The subscription ID. Exactly one of subscriptionId or invoiceId must be provided."""

    invoice_id: Annotated[Optional[str], pydantic.Field(alias="invoiceId")] = None
    r"""The invoice ID to attach this item directly to. Exactly one of subscriptionId or invoiceId must be provided. The invoice must be in ACTIVE or CLOSING state."""

    description: OptionalNullable[str] = UNSET
    r"""Optional longer description shown on the invoice."""

    invoice_at: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="invoiceAt")
    ] = UNSET
    r"""When to collect this item into an invoice. Defaults to now. Ignored when invoiceId is provided."""

    period_start: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="periodStart")
    ] = UNSET
    r"""Start of the billing period for display purposes. Defaults to now."""

    period_end: Annotated[
        OptionalNullable[datetime], pydantic.Field(alias="periodEnd")
    ] = UNSET
    r"""End of the billing period for display purposes. Defaults to now."""

    idempotency_key: Annotated[
        OptionalNullable[str], pydantic.Field(alias="idempotencyKey")
    ] = UNSET
    r"""Optional caller-provided idempotency key. Auto-generated if not provided."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "subscriptionId",
                "invoiceId",
                "description",
                "invoiceAt",
                "periodStart",
                "periodEnd",
                "idempotencyKey",
            ]
        )
        nullable_fields = set(
            ["description", "invoiceAt", "periodStart", "periodEnd", "idempotencyKey"]
        )
        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))
            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


try:
    CreateManualLineItemRequest.model_rebuild()
except NameError:
    pass
