/*
 * 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 { WebhookEvent, WebhookEvent$inboundSchema } from "./webhookevent.js";

/**
 * A webhook delivery for a webhook event.
 */
export type WebhookDelivery = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the object.
   */
  id: string;
  /**
   * Whether the delivery was successful.
   */
  succeeded: boolean;
  /**
   * The HTTP code returned by the URL. `null` if the endpoint was unreachable.
   */
  httpCode: number | null;
  /**
   * The response body returned by the URL, or the error message if the endpoint was unreachable.
   */
  response: string | null;
  /**
   * A webhook event.
   *
   * @remarks
   *
   * An event represent something that happened in the system
   * that should be sent to the webhook endpoint.
   *
   * It can be delivered multiple times until it's marked as succeeded,
   * each one creating a new delivery.
   */
  webhookEvent: WebhookEvent;
};

/** @internal */
export const WebhookDelivery$inboundSchema: z.ZodMiniType<
  WebhookDelivery,
  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(),
    succeeded: z.boolean(),
    http_code: z.nullable(z.int()),
    response: z.nullable(z.string()),
    webhook_event: WebhookEvent$inboundSchema,
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "http_code": "httpCode",
      "webhook_event": "webhookEvent",
    });
  }),
);

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