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

import * as z from "zod/v4-mini";
import { remap as remap$ } from "../../lib/primitives.js";
import { smartUnion } from "../../types/smartUnion.js";

export type OrderCreateCustomFieldData = string | number | boolean | Date;

export type OrderCreateMetadata = string | number | number | boolean;

/**
 * Schema to create a draft order for an off-session charge.
 */
export type OrderCreate = {
  /**
   * Key-value object storing custom field values.
   */
  customFieldData?:
    | { [k: string]: string | number | boolean | Date | null }
    | undefined;
  /**
   * Key-value object allowing you to store additional information.
   *
   * @remarks
   *
   * The key must be a string with a maximum length of **40 characters**.
   * The value must be either:
   *
   * * A string with a maximum length of **500 characters**
   * * An integer
   * * A floating-point number
   * * A boolean
   *
   * You can store up to **50 key-value pairs**.
   */
  metadata?: { [k: string]: string | number | number | boolean } | undefined;
  /**
   * The ID of the organization the order belongs to. **Required unless you use an organization token.** The customer and product must belong to this organization.
   */
  organizationId?: string | null | undefined;
  /**
   * The ID of the customer the order is for. Must belong to the order's organization.
   */
  customerId: string;
  /**
   * The ID of the one-time product to charge for. Must belong to the order's organization. Only fixed-price and free products are supported.
   */
  productId: string;
  /**
   * The currency to charge in (ISO 4217, lowercase, e.g. `usd`). Defaults to the organization's default currency; specify it to force a different one, or when the product isn't priced in the organization's default currency.
   */
  currency?: string | null | undefined;
  /**
   * A custom amount to charge, in the smallest currency unit. Overrides the product's price; defaults to the product's configured price (0 for free products). A positive amount must be at least the currency's minimum.
   */
  amount?: number | null | undefined;
  /**
   * A custom description for the order's line item, shown on the invoice and receipt (e.g. `5,000 tokens`). Defaults to the product name.
   */
  description?: string | null | undefined;
};

/** @internal */
export type OrderCreateCustomFieldData$Outbound =
  | string
  | number
  | boolean
  | string;

/** @internal */
export const OrderCreateCustomFieldData$outboundSchema: z.ZodMiniType<
  OrderCreateCustomFieldData$Outbound,
  OrderCreateCustomFieldData
> = smartUnion([
  z.string(),
  z.int(),
  z.boolean(),
  z.pipe(z.date(), z.transform(v => v.toISOString())),
]);

export function orderCreateCustomFieldDataToJSON(
  orderCreateCustomFieldData: OrderCreateCustomFieldData,
): string {
  return JSON.stringify(
    OrderCreateCustomFieldData$outboundSchema.parse(orderCreateCustomFieldData),
  );
}

/** @internal */
export type OrderCreateMetadata$Outbound = string | number | number | boolean;

/** @internal */
export const OrderCreateMetadata$outboundSchema: z.ZodMiniType<
  OrderCreateMetadata$Outbound,
  OrderCreateMetadata
> = smartUnion([z.string(), z.int(), z.number(), z.boolean()]);

export function orderCreateMetadataToJSON(
  orderCreateMetadata: OrderCreateMetadata,
): string {
  return JSON.stringify(
    OrderCreateMetadata$outboundSchema.parse(orderCreateMetadata),
  );
}

/** @internal */
export type OrderCreate$Outbound = {
  custom_field_data?:
    | { [k: string]: string | number | boolean | string | null }
    | undefined;
  metadata?: { [k: string]: string | number | number | boolean } | undefined;
  organization_id?: string | null | undefined;
  customer_id: string;
  product_id: string;
  currency?: string | null | undefined;
  amount?: number | null | undefined;
  description?: string | null | undefined;
};

/** @internal */
export const OrderCreate$outboundSchema: z.ZodMiniType<
  OrderCreate$Outbound,
  OrderCreate
> = z.pipe(
  z.object({
    customFieldData: z.optional(
      z.record(
        z.string(),
        z.nullable(
          smartUnion([
            z.string(),
            z.int(),
            z.boolean(),
            z.pipe(z.date(), z.transform(v => v.toISOString())),
          ]),
        ),
      ),
    ),
    metadata: z.optional(
      z.record(
        z.string(),
        smartUnion([z.string(), z.int(), z.number(), z.boolean()]),
      ),
    ),
    organizationId: z.optional(z.nullable(z.string())),
    customerId: z.string(),
    productId: z.string(),
    currency: z.optional(z.nullable(z.string())),
    amount: z.optional(z.nullable(z.int())),
    description: z.optional(z.nullable(z.string())),
  }),
  z.transform((v) => {
    return remap$(v, {
      customFieldData: "custom_field_data",
      organizationId: "organization_id",
      customerId: "customer_id",
      productId: "product_id",
    });
  }),
);

export function orderCreateToJSON(orderCreate: OrderCreate): string {
  return JSON.stringify(OrderCreate$outboundSchema.parse(orderCreate));
}
