/*
 * 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 {
  BenefitGrantError,
  BenefitGrantError$inboundSchema,
  BenefitGrantError$Outbound,
  BenefitGrantError$outboundSchema,
} from "./benefitgranterror.js";
import {
  BenefitGrantSlackSharedChannelProperties,
  BenefitGrantSlackSharedChannelProperties$inboundSchema,
  BenefitGrantSlackSharedChannelProperties$Outbound,
  BenefitGrantSlackSharedChannelProperties$outboundSchema,
} from "./benefitgrantslacksharedchannelproperties.js";
import {
  BenefitSlackSharedChannel,
  BenefitSlackSharedChannel$inboundSchema,
  BenefitSlackSharedChannel$Outbound,
  BenefitSlackSharedChannel$outboundSchema,
} from "./benefitslacksharedchannel.js";
import {
  Customer,
  Customer$inboundSchema,
  Customer$Outbound,
  Customer$outboundSchema,
} from "./customer.js";
import {
  Member,
  Member$inboundSchema,
  Member$Outbound,
  Member$outboundSchema,
} from "./member.js";

export type BenefitGrantSlackSharedChannelWebhook = {
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the grant.
   */
  id: string;
  /**
   * The timestamp when the benefit was granted. If `None`, the benefit is not granted.
   */
  grantedAt?: Date | null | undefined;
  /**
   * Whether the benefit is granted.
   */
  isGranted: boolean;
  /**
   * The timestamp when the benefit was revoked. If `None`, the benefit is not revoked.
   */
  revokedAt?: Date | null | undefined;
  /**
   * Whether the benefit is revoked.
   */
  isRevoked: boolean;
  /**
   * The ID of the subscription that granted this benefit.
   */
  subscriptionId: string | null;
  /**
   * The ID of the order that granted this benefit.
   */
  orderId: string | null;
  /**
   * The ID of the customer concerned by this grant.
   */
  customerId: string;
  /**
   * The ID of the member concerned by this grant.
   */
  memberId?: string | null | undefined;
  /**
   * The ID of the benefit concerned by this grant.
   */
  benefitId: string;
  /**
   * The error information if the benefit grant failed with an unrecoverable error.
   */
  error?: BenefitGrantError | null | undefined;
  customer: Customer;
  member?: Member | null | undefined;
  benefit: BenefitSlackSharedChannel;
  properties: BenefitGrantSlackSharedChannelProperties;
  previousProperties?:
    | BenefitGrantSlackSharedChannelProperties
    | null
    | undefined;
};

/** @internal */
export const BenefitGrantSlackSharedChannelWebhook$inboundSchema: z.ZodMiniType<
  BenefitGrantSlackSharedChannelWebhook,
  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(),
    granted_at: z.optional(
      z.nullable(z.pipe(
        z.iso.datetime({ offset: true }),
        z.transform(v => new Date(v)),
      )),
    ),
    is_granted: z.boolean(),
    revoked_at: z.optional(
      z.nullable(z.pipe(
        z.iso.datetime({ offset: true }),
        z.transform(v => new Date(v)),
      )),
    ),
    is_revoked: z.boolean(),
    subscription_id: z.nullable(z.string()),
    order_id: z.nullable(z.string()),
    customer_id: z.string(),
    member_id: z.optional(z.nullable(z.string())),
    benefit_id: z.string(),
    error: z.optional(z.nullable(BenefitGrantError$inboundSchema)),
    customer: Customer$inboundSchema,
    member: z.optional(z.nullable(Member$inboundSchema)),
    benefit: BenefitSlackSharedChannel$inboundSchema,
    properties: BenefitGrantSlackSharedChannelProperties$inboundSchema,
    previous_properties: z.optional(
      z.nullable(BenefitGrantSlackSharedChannelProperties$inboundSchema),
    ),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "granted_at": "grantedAt",
      "is_granted": "isGranted",
      "revoked_at": "revokedAt",
      "is_revoked": "isRevoked",
      "subscription_id": "subscriptionId",
      "order_id": "orderId",
      "customer_id": "customerId",
      "member_id": "memberId",
      "benefit_id": "benefitId",
      "previous_properties": "previousProperties",
    });
  }),
);
/** @internal */
export type BenefitGrantSlackSharedChannelWebhook$Outbound = {
  created_at: string;
  modified_at: string | null;
  id: string;
  granted_at?: string | null | undefined;
  is_granted: boolean;
  revoked_at?: string | null | undefined;
  is_revoked: boolean;
  subscription_id: string | null;
  order_id: string | null;
  customer_id: string;
  member_id?: string | null | undefined;
  benefit_id: string;
  error?: BenefitGrantError$Outbound | null | undefined;
  customer: Customer$Outbound;
  member?: Member$Outbound | null | undefined;
  benefit: BenefitSlackSharedChannel$Outbound;
  properties: BenefitGrantSlackSharedChannelProperties$Outbound;
  previous_properties?:
    | BenefitGrantSlackSharedChannelProperties$Outbound
    | null
    | undefined;
};

/** @internal */
export const BenefitGrantSlackSharedChannelWebhook$outboundSchema:
  z.ZodMiniType<
    BenefitGrantSlackSharedChannelWebhook$Outbound,
    BenefitGrantSlackSharedChannelWebhook
  > = 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(),
      grantedAt: z.optional(
        z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
      ),
      isGranted: z.boolean(),
      revokedAt: z.optional(
        z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
      ),
      isRevoked: z.boolean(),
      subscriptionId: z.nullable(z.string()),
      orderId: z.nullable(z.string()),
      customerId: z.string(),
      memberId: z.optional(z.nullable(z.string())),
      benefitId: z.string(),
      error: z.optional(z.nullable(BenefitGrantError$outboundSchema)),
      customer: Customer$outboundSchema,
      member: z.optional(z.nullable(Member$outboundSchema)),
      benefit: BenefitSlackSharedChannel$outboundSchema,
      properties: BenefitGrantSlackSharedChannelProperties$outboundSchema,
      previousProperties: z.optional(
        z.nullable(BenefitGrantSlackSharedChannelProperties$outboundSchema),
      ),
    }),
    z.transform((v) => {
      return remap$(v, {
        createdAt: "created_at",
        modifiedAt: "modified_at",
        grantedAt: "granted_at",
        isGranted: "is_granted",
        revokedAt: "revoked_at",
        isRevoked: "is_revoked",
        subscriptionId: "subscription_id",
        orderId: "order_id",
        customerId: "customer_id",
        memberId: "member_id",
        benefitId: "benefit_id",
        previousProperties: "previous_properties",
      });
    }),
  );

export function benefitGrantSlackSharedChannelWebhookToJSON(
  benefitGrantSlackSharedChannelWebhook: BenefitGrantSlackSharedChannelWebhook,
): string {
  return JSON.stringify(
    BenefitGrantSlackSharedChannelWebhook$outboundSchema.parse(
      benefitGrantSlackSharedChannelWebhook,
    ),
  );
}
export function benefitGrantSlackSharedChannelWebhookFromJSON(
  jsonString: string,
): SafeParseResult<BenefitGrantSlackSharedChannelWebhook, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) =>
      BenefitGrantSlackSharedChannelWebhook$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'BenefitGrantSlackSharedChannelWebhook' from JSON`,
  );
}
