/*
 * 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 {
  EventSortProperty,
  EventSortProperty$outboundSchema,
} from "../components/eventsortproperty.js";
import {
  EventSource,
  EventSource$outboundSchema,
} from "../components/eventsource.js";
import {
  ListResourceEvent,
  ListResourceEvent$inboundSchema,
} from "../components/listresourceevent.js";
import {
  ListResourceWithCursorPaginationEvent,
  ListResourceWithCursorPaginationEvent$inboundSchema,
} from "../components/listresourcewithcursorpaginationevent.js";
import {
  MetadataQuery,
  MetadataQuery$Outbound,
  MetadataQuery$outboundSchema,
} from "../components/subscriptionslist.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

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

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

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

/**
 * Filter by event name.
 */
export type NameFilter = string | Array<string>;

/**
 * Filter by event source.
 */
export type SourceFilter = EventSource | Array<EventSource>;

export type EventsListRequest = {
  /**
   * Filter events following filter clauses. JSON string following the same schema a meter filter clause.
   */
  filter?: string | null | undefined;
  /**
   * Filter events after this timestamp.
   */
  startTimestamp?: Date | null | undefined;
  /**
   * Filter events before this timestamp.
   */
  endTimestamp?: Date | null | undefined;
  /**
   * Filter by organization ID.
   */
  organizationId?: string | Array<string> | null | undefined;
  /**
   * Filter by customer ID.
   */
  customerId?: string | Array<string> | null | undefined;
  /**
   * Filter by external customer ID.
   */
  externalCustomerId?: string | Array<string> | null | undefined;
  /**
   * Filter by a meter filter clause.
   */
  meterId?: string | null | undefined;
  /**
   * Filter by event name.
   */
  name?: string | Array<string> | null | undefined;
  /**
   * Filter by event source.
   */
  source?: EventSource | Array<EventSource> | null | undefined;
  /**
   * Query to filter events.
   */
  query?: string | null | undefined;
  /**
   * When combined with depth, use this event as the anchor instead of root events.
   */
  parentId?: string | null | undefined;
  /**
   * Fetch descendants up to this depth. When set: 0=root events only, 1=roots+children, etc. Max 5. When not set, returns all events.
   */
  depth?: number | 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<EventSortProperty> | null | undefined;
  /**
   * Filter by metadata key-value pairs. It uses the `deepObject` style, e.g. `?metadata[key]=value`.
   */
  metadata?: { [k: string]: MetadataQuery } | null | undefined;
};

/**
 * Successful Response
 */
export type EventsListResponseEventsList =
  | ListResourceEvent
  | ListResourceWithCursorPaginationEvent;

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

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

export function eventsListQueryParamOrganizationIDFilterToJSON(
  eventsListQueryParamOrganizationIDFilter:
    EventsListQueryParamOrganizationIDFilter,
): string {
  return JSON.stringify(
    EventsListQueryParamOrganizationIDFilter$outboundSchema.parse(
      eventsListQueryParamOrganizationIDFilter,
    ),
  );
}

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

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

export function eventsListQueryParamCustomerIDFilterToJSON(
  eventsListQueryParamCustomerIDFilter: EventsListQueryParamCustomerIDFilter,
): string {
  return JSON.stringify(
    EventsListQueryParamCustomerIDFilter$outboundSchema.parse(
      eventsListQueryParamCustomerIDFilter,
    ),
  );
}

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

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

export function eventsListQueryParamExternalCustomerIDFilterToJSON(
  eventsListQueryParamExternalCustomerIDFilter:
    EventsListQueryParamExternalCustomerIDFilter,
): string {
  return JSON.stringify(
    EventsListQueryParamExternalCustomerIDFilter$outboundSchema.parse(
      eventsListQueryParamExternalCustomerIDFilter,
    ),
  );
}

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

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

export function nameFilterToJSON(nameFilter: NameFilter): string {
  return JSON.stringify(NameFilter$outboundSchema.parse(nameFilter));
}

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

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

export function sourceFilterToJSON(sourceFilter: SourceFilter): string {
  return JSON.stringify(SourceFilter$outboundSchema.parse(sourceFilter));
}

/** @internal */
export type EventsListRequest$Outbound = {
  filter?: string | null | undefined;
  start_timestamp?: string | null | undefined;
  end_timestamp?: string | null | undefined;
  organization_id?: string | Array<string> | null | undefined;
  customer_id?: string | Array<string> | null | undefined;
  external_customer_id?: string | Array<string> | null | undefined;
  meter_id?: string | null | undefined;
  name?: string | Array<string> | null | undefined;
  source?: string | Array<string> | null | undefined;
  query?: string | null | undefined;
  parent_id?: string | null | undefined;
  depth?: number | null | undefined;
  page: number;
  limit: number;
  sorting?: Array<string> | null | undefined;
  metadata?: { [k: string]: MetadataQuery$Outbound } | null | undefined;
};

/** @internal */
export const EventsListRequest$outboundSchema: z.ZodMiniType<
  EventsListRequest$Outbound,
  EventsListRequest
> = z.pipe(
  z.object({
    filter: z.optional(z.nullable(z.string())),
    startTimestamp: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    endTimestamp: z.optional(
      z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    ),
    organizationId: 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())])),
    ),
    meterId: z.optional(z.nullable(z.string())),
    name: z.optional(z.nullable(smartUnion([z.string(), z.array(z.string())]))),
    source: z.optional(
      z.nullable(
        smartUnion([
          EventSource$outboundSchema,
          z.array(EventSource$outboundSchema),
        ]),
      ),
    ),
    query: z.optional(z.nullable(z.string())),
    parentId: z.optional(z.nullable(z.string())),
    depth: z.optional(z.nullable(z.int())),
    page: z._default(z.int(), 1),
    limit: z._default(z.int(), 10),
    sorting: z.optional(z.nullable(z.array(EventSortProperty$outboundSchema))),
    metadata: z.optional(
      z.nullable(z.record(z.string(), MetadataQuery$outboundSchema)),
    ),
  }),
  z.transform((v) => {
    return remap$(v, {
      startTimestamp: "start_timestamp",
      endTimestamp: "end_timestamp",
      organizationId: "organization_id",
      customerId: "customer_id",
      externalCustomerId: "external_customer_id",
      meterId: "meter_id",
      parentId: "parent_id",
    });
  }),
);

export function eventsListRequestToJSON(
  eventsListRequest: EventsListRequest,
): string {
  return JSON.stringify(
    EventsListRequest$outboundSchema.parse(eventsListRequest),
  );
}

/** @internal */
export const EventsListResponseEventsList$inboundSchema: z.ZodMiniType<
  EventsListResponseEventsList,
  unknown
> = smartUnion([
  ListResourceEvent$inboundSchema,
  ListResourceWithCursorPaginationEvent$inboundSchema,
]);

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