/*
 * 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 {
  CustomerSortProperty,
  CustomerSortProperty$outboundSchema,
} from "../components/customersortproperty.js";
import {
  ListResourceCustomer,
  ListResourceCustomer$inboundSchema,
} from "../components/listresourcecustomer.js";
import {
  MetadataQuery,
  MetadataQuery$Outbound,
  MetadataQuery$outboundSchema,
} from "../components/subscriptionslist.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

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

export type CustomersListRequest = {
  /**
   * Filter by organization ID.
   */
  organizationId?: string | Array<string> | null | undefined;
  /**
   * Filter by exact email.
   */
  email?: string | null | undefined;
  /**
   * Filter by name, email, or external ID.
   */
  query?: string | null | undefined;
  /**
   * Filter by active customers, i.e. customers with at least one trialing, active or past_due subscription.
   */
  active?: boolean | 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<CustomerSortProperty> | 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 CustomersListResponse = {
  result: ListResourceCustomer;
};

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

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

export function customersListQueryParamOrganizationIDFilterToJSON(
  customersListQueryParamOrganizationIDFilter:
    CustomersListQueryParamOrganizationIDFilter,
): string {
  return JSON.stringify(
    CustomersListQueryParamOrganizationIDFilter$outboundSchema.parse(
      customersListQueryParamOrganizationIDFilter,
    ),
  );
}

/** @internal */
export type CustomersListRequest$Outbound = {
  organization_id?: string | Array<string> | null | undefined;
  email?: string | null | undefined;
  query?: string | null | undefined;
  active?: boolean | null | undefined;
  page: number;
  limit: number;
  sorting?: Array<string> | null | undefined;
  metadata?: { [k: string]: MetadataQuery$Outbound } | null | undefined;
};

/** @internal */
export const CustomersListRequest$outboundSchema: z.ZodMiniType<
  CustomersListRequest$Outbound,
  CustomersListRequest
> = z.pipe(
  z.object({
    organizationId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    email: z.optional(z.nullable(z.string())),
    query: z.optional(z.nullable(z.string())),
    active: z.optional(z.nullable(z.boolean())),
    page: z._default(z.int(), 1),
    limit: z._default(z.int(), 10),
    sorting: z.optional(
      z.nullable(z.array(CustomerSortProperty$outboundSchema)),
    ),
    metadata: z.optional(
      z.nullable(z.record(z.string(), MetadataQuery$outboundSchema)),
    ),
  }),
  z.transform((v) => {
    return remap$(v, {
      organizationId: "organization_id",
    });
  }),
);

export function customersListRequestToJSON(
  customersListRequest: CustomersListRequest,
): string {
  return JSON.stringify(
    CustomersListRequest$outboundSchema.parse(customersListRequest),
  );
}

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

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