/*
 * 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 { smartUnion } from "../../types/smartUnion.js";
import {
  CustomerCancellationReason,
  CustomerCancellationReason$outboundSchema,
} from "../components/customercancellationreason.js";
import {
  ListResourceSubscription,
  ListResourceSubscription$inboundSchema,
} from "../components/listresourcesubscription.js";
import {
  MetadataQuery,
  MetadataQuery$Outbound,
  MetadataQuery$outboundSchema,
} from "../components/subscriptionslist.js";
import {
  SubscriptionSortProperty,
  SubscriptionSortProperty$outboundSchema,
} from "../components/subscriptionsortproperty.js";
import {
  SubscriptionStatus,
  SubscriptionStatus$outboundSchema,
} from "../components/subscriptionstatus.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * Filter by organization ID.
 */
export type OrganizationIDFilter = string | Array<string>;

/**
 * Filter by product ID.
 */
export type ProductIDFilter = string | Array<string>;

/**
 * Filter by customer ID.
 */
export type CustomerIDFilter = string | Array<string>;

/**
 * Filter by customer external ID.
 */
export type ExternalCustomerIDFilter = string | Array<string>;

/**
 * Filter by discount ID.
 */
export type DiscountIDFilter = string | Array<string>;

/**
 * Filter by subscription status.
 */
export type StatusFilter = SubscriptionStatus | Array<SubscriptionStatus>;

/**
 * Filter by customer cancellation reason.
 */
export type CustomerCancellationReasonFilter =
  | CustomerCancellationReason
  | Array<CustomerCancellationReason>;

export type SubscriptionsListRequest = {
  /**
   * Filter by organization ID.
   */
  organizationId?: string | Array<string> | null | undefined;
  /**
   * Filter by product ID.
   */
  productId?: string | Array<string> | null | undefined;
  /**
   * Filter by customer ID.
   */
  customerId?: string | Array<string> | null | undefined;
  /**
   * Filter by customer external ID.
   */
  externalCustomerId?: string | Array<string> | null | undefined;
  /**
   * Filter by discount ID.
   */
  discountId?: string | Array<string> | null | undefined;
  /**
   * Filter by active or inactive subscription.
   *
   * @deprecated field: This will be removed in a future release, please migrate away from it as soon as possible.
   */
  active?: boolean | null | undefined;
  /**
   * Filter by subscription status.
   */
  status?: SubscriptionStatus | Array<SubscriptionStatus> | null | undefined;
  /**
   * Filter by subscriptions that are set to cancel at period end.
   */
  cancelAtPeriodEnd?: boolean | null | undefined;
  /**
   * Filter by customer cancellation reason.
   */
  customerCancellationReason?:
    | CustomerCancellationReason
    | Array<CustomerCancellationReason>
    | null
    | undefined;
  /**
   * Filter by cancellation date (after or equal to).
   */
  canceledAtAfter?: Date | null | undefined;
  /**
   * Filter by cancellation date (before or equal to).
   */
  canceledAtBefore?: Date | null | undefined;
  /**
   * Page number, defaults to 1.
   */
  page?: number | undefined;
  /**
   * Size of a page, defaults to 10. Maximum is 100.
   */
  limit?: number | undefined;
  /**
   * Sorting criterion. Several criteria can be used simultaneously and will be applied in order. Add a minus sign `-` before the criteria name to sort by descending order.
   */
  sorting?: Array<SubscriptionSortProperty> | null | undefined;
  /**
   * Filter by metadata key-value pairs. It uses the `deepObject` style, e.g. `?metadata[key]=value`.
   */
  metadata?: { [k: string]: MetadataQuery } | null | undefined;
};

export type SubscriptionsListResponse = {
  result: ListResourceSubscription;
};

/** @internal */
export type OrganizationIDFilter$Outbound = string | Array<string>;

/** @internal */
export const OrganizationIDFilter$outboundSchema: z.ZodMiniType<
  OrganizationIDFilter$Outbound,
  OrganizationIDFilter
> = smartUnion([z.string(), z.array(z.string())]);

export function organizationIDFilterToJSON(
  organizationIDFilter: OrganizationIDFilter,
): string {
  return JSON.stringify(
    OrganizationIDFilter$outboundSchema.parse(organizationIDFilter),
  );
}

/** @internal */
export type ProductIDFilter$Outbound = string | Array<string>;

/** @internal */
export const ProductIDFilter$outboundSchema: z.ZodMiniType<
  ProductIDFilter$Outbound,
  ProductIDFilter
> = smartUnion([z.string(), z.array(z.string())]);

export function productIDFilterToJSON(
  productIDFilter: ProductIDFilter,
): string {
  return JSON.stringify(ProductIDFilter$outboundSchema.parse(productIDFilter));
}

/** @internal */
export type CustomerIDFilter$Outbound = string | Array<string>;

/** @internal */
export const CustomerIDFilter$outboundSchema: z.ZodMiniType<
  CustomerIDFilter$Outbound,
  CustomerIDFilter
> = smartUnion([z.string(), z.array(z.string())]);

export function customerIDFilterToJSON(
  customerIDFilter: CustomerIDFilter,
): string {
  return JSON.stringify(
    CustomerIDFilter$outboundSchema.parse(customerIDFilter),
  );
}

/** @internal */
export type ExternalCustomerIDFilter$Outbound = string | Array<string>;

/** @internal */
export const ExternalCustomerIDFilter$outboundSchema: z.ZodMiniType<
  ExternalCustomerIDFilter$Outbound,
  ExternalCustomerIDFilter
> = smartUnion([z.string(), z.array(z.string())]);

export function externalCustomerIDFilterToJSON(
  externalCustomerIDFilter: ExternalCustomerIDFilter,
): string {
  return JSON.stringify(
    ExternalCustomerIDFilter$outboundSchema.parse(externalCustomerIDFilter),
  );
}

/** @internal */
export type DiscountIDFilter$Outbound = string | Array<string>;

/** @internal */
export const DiscountIDFilter$outboundSchema: z.ZodMiniType<
  DiscountIDFilter$Outbound,
  DiscountIDFilter
> = smartUnion([z.string(), z.array(z.string())]);

export function discountIDFilterToJSON(
  discountIDFilter: DiscountIDFilter,
): string {
  return JSON.stringify(
    DiscountIDFilter$outboundSchema.parse(discountIDFilter),
  );
}

/** @internal */
export type StatusFilter$Outbound = string | Array<string>;

/** @internal */
export const StatusFilter$outboundSchema: z.ZodMiniType<
  StatusFilter$Outbound,
  StatusFilter
> = smartUnion([
  SubscriptionStatus$outboundSchema,
  z.array(SubscriptionStatus$outboundSchema),
]);

export function statusFilterToJSON(statusFilter: StatusFilter): string {
  return JSON.stringify(StatusFilter$outboundSchema.parse(statusFilter));
}

/** @internal */
export type CustomerCancellationReasonFilter$Outbound = string | Array<string>;

/** @internal */
export const CustomerCancellationReasonFilter$outboundSchema: z.ZodMiniType<
  CustomerCancellationReasonFilter$Outbound,
  CustomerCancellationReasonFilter
> = smartUnion([
  CustomerCancellationReason$outboundSchema,
  z.array(CustomerCancellationReason$outboundSchema),
]);

export function customerCancellationReasonFilterToJSON(
  customerCancellationReasonFilter: CustomerCancellationReasonFilter,
): string {
  return JSON.stringify(
    CustomerCancellationReasonFilter$outboundSchema.parse(
      customerCancellationReasonFilter,
    ),
  );
}

/** @internal */
export type SubscriptionsListRequest$Outbound = {
  organization_id?: string | Array<string> | null | undefined;
  product_id?: string | Array<string> | null | undefined;
  customer_id?: string | Array<string> | null | undefined;
  external_customer_id?: string | Array<string> | null | undefined;
  discount_id?: string | Array<string> | null | undefined;
  active?: boolean | null | undefined;
  status?: string | Array<string> | null | undefined;
  cancel_at_period_end?: boolean | null | undefined;
  customer_cancellation_reason?: string | Array<string> | null | undefined;
  canceled_at_after?: string | null | undefined;
  canceled_at_before?: string | null | undefined;
  page: number;
  limit: number;
  sorting?: Array<string> | null | undefined;
  metadata?: { [k: string]: MetadataQuery$Outbound } | null | undefined;
};

/** @internal */
export const SubscriptionsListRequest$outboundSchema: z.ZodMiniType<
  SubscriptionsListRequest$Outbound,
  SubscriptionsListRequest
> = z.pipe(
  z.object({
    organizationId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    productId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    customerId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    externalCustomerId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    discountId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    active: z.optional(z.nullable(z.boolean())),
    status: z.optional(
      z.nullable(
        smartUnion([
          SubscriptionStatus$outboundSchema,
          z.array(SubscriptionStatus$outboundSchema),
        ]),
      ),
    ),
    cancelAtPeriodEnd: z.optional(z.nullable(z.boolean())),
    customerCancellationReason: z.optional(
      z.nullable(
        smartUnion([
          CustomerCancellationReason$outboundSchema,
          z.array(CustomerCancellationReason$outboundSchema),
        ]),
      ),
    ),
    canceledAtAfter: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    canceledAtBefore: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    page: z._default(z.int(), 1),
    limit: z._default(z.int(), 10),
    sorting: z.optional(
      z.nullable(z.array(SubscriptionSortProperty$outboundSchema)),
    ),
    metadata: z.optional(z.nullable(z.record(
      z.string(),
      z.lazy(() => MetadataQuery$outboundSchema),
    ))),
  }),
  z.transform((v) => {
    return remap$(v, {
      organizationId: "organization_id",
      productId: "product_id",
      customerId: "customer_id",
      externalCustomerId: "external_customer_id",
      discountId: "discount_id",
      cancelAtPeriodEnd: "cancel_at_period_end",
      customerCancellationReason: "customer_cancellation_reason",
      canceledAtAfter: "canceled_at_after",
      canceledAtBefore: "canceled_at_before",
    });
  }),
);

export function subscriptionsListRequestToJSON(
  subscriptionsListRequest: SubscriptionsListRequest,
): string {
  return JSON.stringify(
    SubscriptionsListRequest$outboundSchema.parse(subscriptionsListRequest),
  );
}

/** @internal */
export const SubscriptionsListResponse$inboundSchema: z.ZodMiniType<
  SubscriptionsListResponse,
  unknown
> = z.pipe(
  z.object({
    Result: ListResourceSubscription$inboundSchema,
  }),
  z.transform((v) => {
    return remap$(v, {
      "Result": "result",
    });
  }),
);

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