/*
 * 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";
import {
  DiscountDuration,
  DiscountDuration$outboundSchema,
} from "./discountduration.js";
import {
  PresentmentCurrency,
  PresentmentCurrency$outboundSchema,
} from "./presentmentcurrency.js";

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

/**
 * Schema to create a fixed amount discount.
 */
export type DiscountFixedCreate = {
  /**
   * 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;
  /**
   * Name of the discount. Will be displayed to the customer when the discount is applied.
   */
  name: string;
  /**
   * Code customers can use to apply the discount during checkout. Must be between 3 and 256 characters long and contain only alphanumeric characters.If not provided, the discount can only be applied via the API.
   */
  code?: string | null | undefined;
  /**
   * Optional timestamp after which the discount is redeemable.
   */
  startsAt?: Date | null | undefined;
  /**
   * Optional timestamp after which the discount is no longer redeemable.
   */
  endsAt?: Date | null | undefined;
  /**
   * Optional maximum number of times the discount can be redeemed.
   */
  maxRedemptions?: number | null | undefined;
  products?: Array<string> | null | undefined;
  /**
   * The ID of the organization owning the discount. **Required unless you use an organization token.**
   */
  organizationId?: string | null | undefined;
  type: "fixed";
  duration: DiscountDuration;
  /**
   * Number of months the discount should be applied.
   *
   * @remarks
   *
   * Required when `duration` is `repeating`. Must be omitted otherwise.
   *
   * For this to work on yearly pricing, you should multiply this by 12.
   * For example, to apply the discount for 2 years, set this to 24.
   */
  durationInMonths?: number | null | undefined;
  /**
   * @deprecated field: This will be removed in a future release, please migrate away from it as soon as possible.
   */
  amount?: number | null | undefined;
  /**
   * @deprecated field: This will be removed in a future release, please migrate away from it as soon as possible.
   */
  currency?: PresentmentCurrency | null | undefined;
  amounts?: { [k: string]: number } | null | undefined;
};

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

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

export function discountFixedCreateMetadataToJSON(
  discountFixedCreateMetadata: DiscountFixedCreateMetadata,
): string {
  return JSON.stringify(
    DiscountFixedCreateMetadata$outboundSchema.parse(
      discountFixedCreateMetadata,
    ),
  );
}

/** @internal */
export type DiscountFixedCreate$Outbound = {
  metadata?: { [k: string]: string | number | number | boolean } | undefined;
  name: string;
  code?: string | null | undefined;
  starts_at?: string | null | undefined;
  ends_at?: string | null | undefined;
  max_redemptions?: number | null | undefined;
  products?: Array<string> | null | undefined;
  organization_id?: string | null | undefined;
  type: "fixed";
  duration: string;
  duration_in_months?: number | null | undefined;
  amount?: number | null | undefined;
  currency?: string | null | undefined;
  amounts?: { [k: string]: number } | null | undefined;
};

/** @internal */
export const DiscountFixedCreate$outboundSchema: z.ZodMiniType<
  DiscountFixedCreate$Outbound,
  DiscountFixedCreate
> = z.pipe(
  z.object({
    metadata: z.optional(
      z.record(
        z.string(),
        smartUnion([z.string(), z.int(), z.number(), z.boolean()]),
      ),
    ),
    name: z.string(),
    code: z.optional(z.nullable(z.string())),
    startsAt: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    endsAt: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    maxRedemptions: z.optional(z.nullable(z.int())),
    products: z.optional(z.nullable(z.array(z.string()))),
    organizationId: z.optional(z.nullable(z.string())),
    type: z.literal("fixed"),
    duration: DiscountDuration$outboundSchema,
    durationInMonths: z.optional(z.nullable(z.int())),
    amount: z.optional(z.nullable(z.int())),
    currency: z.optional(z.nullable(PresentmentCurrency$outboundSchema)),
    amounts: z.optional(z.nullable(z.record(z.string(), z.int()))),
  }),
  z.transform((v) => {
    return remap$(v, {
      startsAt: "starts_at",
      endsAt: "ends_at",
      maxRedemptions: "max_redemptions",
      organizationId: "organization_id",
      durationInMonths: "duration_in_months",
    });
  }),
);

export function discountFixedCreateToJSON(
  discountFixedCreate: DiscountFixedCreate,
): string {
  return JSON.stringify(
    DiscountFixedCreate$outboundSchema.parse(discountFixedCreate),
  );
}
