/*
 * 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 {
  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 generic payment method.
 */
export type GenericPayment = {
  /**
   * 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: string;
  /**
   * 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;
};

/** @internal */
export const GenericPayment$inboundSchema: z.ZodMiniType<
  GenericPayment,
  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.string(),
    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())),
  }),
  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",
    });
  }),
);

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