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

import * as z from "zod/v3";
import * as openEnums from "../../types/enums.js";
import { OpenEnum } from "../../types/enums.js";

/**
 * Defines how to modify the key's remaining credits. Use 'set' to replace current credits with a specific value or unlimited usage, 'increment' to add credits for plan upgrades or credit purchases, and 'decrement' to reduce credits for refunds or policy violations.
 *
 * @remarks
 */
export const Operation = {
  Set: "set",
  Increment: "increment",
  Decrement: "decrement",
} as const;
/**
 * Defines how to modify the key's remaining credits. Use 'set' to replace current credits with a specific value or unlimited usage, 'increment' to add credits for plan upgrades or credit purchases, and 'decrement' to reduce credits for refunds or policy violations.
 *
 * @remarks
 */
export type Operation = OpenEnum<typeof Operation>;

export type V2KeysUpdateCreditsRequestBody = {
  /**
   * The ID of the key to update (begins with `key_`). This is the database reference ID for the key, not the actual API key string that users authenticate with. This ID uniquely identifies which key's credits will be updated.
   */
  keyId: string;
  /**
   * The credit value to use with the specified operation. The meaning depends on the operation: for 'set', this becomes the new remaining credits value; for 'increment', this amount is added to current credits; for 'decrement', this amount is subtracted from current credits.
   *
   * @remarks
   *
   * Set to null when using 'set' operation to make the key unlimited (removes usage restrictions entirely). When decrementing, if the result would be negative, remaining credits are automatically set to zero. Credits are consumed during successful key verification, and when credits reach zero, verification fails with `code=USAGE_EXCEEDED`.
   *
   * Required when using 'increment' or 'decrement' operations. Optional for 'set' operation (null creates unlimited usage).
   */
  value?: number | null | undefined;
  /**
   * Defines how to modify the key's remaining credits. Use 'set' to replace current credits with a specific value or unlimited usage, 'increment' to add credits for plan upgrades or credit purchases, and 'decrement' to reduce credits for refunds or policy violations.
   *
   * @remarks
   */
  operation: Operation;
};

/** @internal */
export const Operation$outboundSchema: z.ZodType<
  string,
  z.ZodTypeDef,
  Operation
> = openEnums.outboundSchema(Operation);

/** @internal */
export type V2KeysUpdateCreditsRequestBody$Outbound = {
  keyId: string;
  value?: number | null | undefined;
  operation: string;
};

/** @internal */
export const V2KeysUpdateCreditsRequestBody$outboundSchema: z.ZodType<
  V2KeysUpdateCreditsRequestBody$Outbound,
  z.ZodTypeDef,
  V2KeysUpdateCreditsRequestBody
> = z.object({
  keyId: z.string(),
  value: z.nullable(z.number().int()).optional(),
  operation: Operation$outboundSchema,
});

export function v2KeysUpdateCreditsRequestBodyToJSON(
  v2KeysUpdateCreditsRequestBody: V2KeysUpdateCreditsRequestBody,
): string {
  return JSON.stringify(
    V2KeysUpdateCreditsRequestBody$outboundSchema.parse(
      v2KeysUpdateCreditsRequestBody,
    ),
  );
}
