/*
 * 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";
import {
  CardPaymentMetadata,
  CardPaymentMetadata$inboundSchema,
} from "./cardpaymentmetadata.js";
import {
  PaymentProcessor,
  PaymentProcessor$inboundSchema,
} from "./paymentprocessor.js";
import { PaymentStatus, PaymentStatus$inboundSchema } from "./paymentstatus.js";
import {
  PaymentTrigger,
  PaymentTrigger$inboundSchema,
} from "./paymenttrigger.js";

/**
 * Schema of a payment with a card payment method.
 */
export type CardPayment = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  processor: PaymentProcessor;
  status: PaymentStatus;
  /**
   * The payment amount in cents.
   */
  amount: number;
  /**
   * The payment currency. Currently, only `usd` is supported.
   */
  currency: string;
  /**
   * The payment method used.
   */
  method: "card";
  /**
   * What initiated this payment attempt, e.g. initial purchase, subscription renewal, or an automated dunning retry.
   */
  trigger: PaymentTrigger | null;
  /**
   * Error code, if the payment was declined.
   */
  declineReason: string | null;
  /**
   * Human-readable error message, if the payment was declined.
   */
  declineMessage: string | null;
  /**
   * The ID of the organization that owns the payment.
   */
  organizationId: string;
  /**
   * The ID of the checkout session associated with this payment.
   */
  checkoutId: string | null;
  /**
   * The ID of the order associated with this payment.
   */
  orderId: string | null;
  /**
   * Additional metadata from the payment processor for internal use.
   */
  processorMetadata?: { [k: string]: any } | undefined;
  /**
   * Additional metadata for a card payment method.
   */
  methodMetadata: CardPaymentMetadata;
};

/** @internal */
export const CardPayment$inboundSchema: z.ZodMiniType<CardPayment, 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(),
      processor: PaymentProcessor$inboundSchema,
      status: PaymentStatus$inboundSchema,
      amount: z.int(),
      currency: z.string(),
      method: z.literal("card"),
      trigger: z.nullable(PaymentTrigger$inboundSchema),
      decline_reason: z.nullable(z.string()),
      decline_message: z.nullable(z.string()),
      organization_id: z.string(),
      checkout_id: z.nullable(z.string()),
      order_id: z.nullable(z.string()),
      processor_metadata: z.optional(z.record(z.string(), z.any())),
      method_metadata: CardPaymentMetadata$inboundSchema,
    }),
    z.transform((v) => {
      return remap$(v, {
        "created_at": "createdAt",
        "modified_at": "modifiedAt",
        "decline_reason": "declineReason",
        "decline_message": "declineMessage",
        "organization_id": "organizationId",
        "checkout_id": "checkoutId",
        "order_id": "orderId",
        "processor_metadata": "processorMetadata",
        "method_metadata": "methodMetadata",
      });
    }),
  );

export function cardPaymentFromJSON(
  jsonString: string,
): SafeParseResult<CardPayment, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => CardPayment$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'CardPayment' from JSON`,
  );
}
