/*
 * 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 { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * Pending update to be applied to a subscription at the beginning of the next period.
 */
export type PendingSubscriptionUpdate = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  /**
   * The date and time when the subscription update will be applied.
   */
  appliesAt: Date;
  /**
   * ID of the new product to apply to the subscription. If `null`, the product won't be changed.
   */
  productId: string | null;
  /**
   * Number of seats to apply to the subscription. If `null`, the number of seats won't be changed.
   */
  seats: number | null;
};

/** @internal */
export const PendingSubscriptionUpdate$inboundSchema: z.ZodMiniType<
  PendingSubscriptionUpdate,
  unknown
> = z.pipe(
  z.object({
    created_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    modified_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    id: z.string(),
    applies_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    product_id: z.nullable(z.string()),
    seats: z.nullable(z.int()),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "applies_at": "appliesAt",
      "product_id": "productId",
    });
  }),
);
/** @internal */
export type PendingSubscriptionUpdate$Outbound = {
  created_at: string;
  modified_at: string | null;
  id: string;
  applies_at: string;
  product_id: string | null;
  seats: number | null;
};

/** @internal */
export const PendingSubscriptionUpdate$outboundSchema: z.ZodMiniType<
  PendingSubscriptionUpdate$Outbound,
  PendingSubscriptionUpdate
> = z.pipe(
  z.object({
    createdAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
    modifiedAt: z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    id: z.string(),
    appliesAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
    productId: z.nullable(z.string()),
    seats: z.nullable(z.int()),
  }),
  z.transform((v) => {
    return remap$(v, {
      createdAt: "created_at",
      modifiedAt: "modified_at",
      appliesAt: "applies_at",
      productId: "product_id",
    });
  }),
);

export function pendingSubscriptionUpdateToJSON(
  pendingSubscriptionUpdate: PendingSubscriptionUpdate,
): string {
  return JSON.stringify(
    PendingSubscriptionUpdate$outboundSchema.parse(pendingSubscriptionUpdate),
  );
}
export function pendingSubscriptionUpdateFromJSON(
  jsonString: string,
): SafeParseResult<PendingSubscriptionUpdate, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => PendingSubscriptionUpdate$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'PendingSubscriptionUpdate' from JSON`,
  );
}
