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

import * as z from "zod/v4-mini";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  Subscription,
  Subscription$inboundSchema,
  Subscription$Outbound,
  Subscription$outboundSchema,
} from "./subscription.js";

/**
 * Sent when a subscription is revoked and the user loses access immediately.
 *
 * @remarks
 * Happens when the subscription is canceled or payment retries are exhausted (status becomes `unpaid`).
 *
 * For payment failures that can still be recovered, see `subscription.past_due`.
 *
 * **Discord & Slack support:** Full
 */
export type WebhookSubscriptionRevokedPayload = {
  type: "subscription.revoked";
  timestamp: Date;
  data: Subscription;
};

/** @internal */
export const WebhookSubscriptionRevokedPayload$inboundSchema: z.ZodMiniType<
  WebhookSubscriptionRevokedPayload,
  unknown
> = z.object({
  type: z.literal("subscription.revoked"),
  timestamp: z.pipe(
    z.iso.datetime({ offset: true }),
    z.transform(v => new Date(v)),
  ),
  data: Subscription$inboundSchema,
});
/** @internal */
export type WebhookSubscriptionRevokedPayload$Outbound = {
  type: "subscription.revoked";
  timestamp: string;
  data: Subscription$Outbound;
};

/** @internal */
export const WebhookSubscriptionRevokedPayload$outboundSchema: z.ZodMiniType<
  WebhookSubscriptionRevokedPayload$Outbound,
  WebhookSubscriptionRevokedPayload
> = z.object({
  type: z.literal("subscription.revoked"),
  timestamp: z.pipe(z.date(), z.transform(v => v.toISOString())),
  data: Subscription$outboundSchema,
});

export function webhookSubscriptionRevokedPayloadToJSON(
  webhookSubscriptionRevokedPayload: WebhookSubscriptionRevokedPayload,
): string {
  return JSON.stringify(
    WebhookSubscriptionRevokedPayload$outboundSchema.parse(
      webhookSubscriptionRevokedPayload,
    ),
  );
}
export function webhookSubscriptionRevokedPayloadFromJSON(
  jsonString: string,
): SafeParseResult<WebhookSubscriptionRevokedPayload, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => WebhookSubscriptionRevokedPayload$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'WebhookSubscriptionRevokedPayload' from JSON`,
  );
}
