/*
 * 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 { RFCDate } from "../../types/rfcdate.js";
import { smartUnion } from "../../types/smartUnion.js";
import {
  ProductBillingType,
  ProductBillingType$outboundSchema,
} from "../components/productbillingtype.js";
import {
  TimeInterval,
  TimeInterval$outboundSchema,
} from "../components/timeinterval.js";

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

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

/**
 * Filter by billing type. `recurring` will filter data corresponding to subscriptions creations or renewals. `one_time` will filter data corresponding to one-time purchases.
 */
export type MetricsExportQueryParamProductBillingTypeFilter =
  | ProductBillingType
  | Array<ProductBillingType>;

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

export type MetricsExportRequest = {
  /**
   * Start date.
   */
  startDate: RFCDate;
  /**
   * End date.
   */
  endDate: RFCDate;
  /**
   * Timezone to use for the timestamps. Default is UTC.
   */
  timezone?: string | undefined;
  /**
   * Interval between two timestamps.
   */
  interval: TimeInterval;
  /**
   * Filter by organization ID.
   */
  organizationId?: string | Array<string> | null | undefined;
  /**
   * Filter by product ID.
   */
  productId?: string | Array<string> | null | undefined;
  /**
   * Filter by billing type. `recurring` will filter data corresponding to subscriptions creations or renewals. `one_time` will filter data corresponding to one-time purchases.
   */
  billingType?:
    | ProductBillingType
    | Array<ProductBillingType>
    | null
    | undefined;
  /**
   * Filter by customer ID.
   */
  customerId?: string | Array<string> | null | undefined;
  /**
   * List of metric slugs to include in the export. If not provided, all metrics are exported.
   */
  metrics?: Array<string> | null | undefined;
};

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

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

export function metricsExportQueryParamOrganizationIDFilterToJSON(
  metricsExportQueryParamOrganizationIDFilter:
    MetricsExportQueryParamOrganizationIDFilter,
): string {
  return JSON.stringify(
    MetricsExportQueryParamOrganizationIDFilter$outboundSchema.parse(
      metricsExportQueryParamOrganizationIDFilter,
    ),
  );
}

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

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

export function metricsExportQueryParamProductIDFilterToJSON(
  metricsExportQueryParamProductIDFilter:
    MetricsExportQueryParamProductIDFilter,
): string {
  return JSON.stringify(
    MetricsExportQueryParamProductIDFilter$outboundSchema.parse(
      metricsExportQueryParamProductIDFilter,
    ),
  );
}

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

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

export function metricsExportQueryParamProductBillingTypeFilterToJSON(
  metricsExportQueryParamProductBillingTypeFilter:
    MetricsExportQueryParamProductBillingTypeFilter,
): string {
  return JSON.stringify(
    MetricsExportQueryParamProductBillingTypeFilter$outboundSchema.parse(
      metricsExportQueryParamProductBillingTypeFilter,
    ),
  );
}

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

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

export function metricsExportQueryParamCustomerIDFilterToJSON(
  metricsExportQueryParamCustomerIDFilter:
    MetricsExportQueryParamCustomerIDFilter,
): string {
  return JSON.stringify(
    MetricsExportQueryParamCustomerIDFilter$outboundSchema.parse(
      metricsExportQueryParamCustomerIDFilter,
    ),
  );
}

/** @internal */
export type MetricsExportRequest$Outbound = {
  start_date: string;
  end_date: string;
  timezone: string;
  interval: string;
  organization_id?: string | Array<string> | null | undefined;
  product_id?: string | Array<string> | null | undefined;
  billing_type?: string | Array<string> | null | undefined;
  customer_id?: string | Array<string> | null | undefined;
  metrics?: Array<string> | null | undefined;
};

/** @internal */
export const MetricsExportRequest$outboundSchema: z.ZodMiniType<
  MetricsExportRequest$Outbound,
  MetricsExportRequest
> = z.pipe(
  z.object({
    startDate: z.pipe(
      z.custom<RFCDate>(x => x instanceof RFCDate),
      z.transform(v => v.toString()),
    ),
    endDate: z.pipe(
      z.custom<RFCDate>(x => x instanceof RFCDate),
      z.transform(v => v.toString()),
    ),
    timezone: z._default(z.string(), "UTC"),
    interval: TimeInterval$outboundSchema,
    organizationId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    productId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    billingType: z.optional(
      z.nullable(
        smartUnion([
          ProductBillingType$outboundSchema,
          z.array(ProductBillingType$outboundSchema),
        ]),
      ),
    ),
    customerId: z.optional(
      z.nullable(smartUnion([z.string(), z.array(z.string())])),
    ),
    metrics: z.optional(z.nullable(z.array(z.string()))),
  }),
  z.transform((v) => {
    return remap$(v, {
      startDate: "start_date",
      endDate: "end_date",
      organizationId: "organization_id",
      productId: "product_id",
      billingType: "billing_type",
      customerId: "customer_id",
    });
  }),
);

export function metricsExportRequestToJSON(
  metricsExportRequest: MetricsExportRequest,
): string {
  return JSON.stringify(
    MetricsExportRequest$outboundSchema.parse(metricsExportRequest),
  );
}
