/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import * as types from "../../types/primitives.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * The type of event that triggers the workflow. In this case, the workflow is initiated
 *
 * @remarks
 * by an HTTP request. Future iterations may support additional event types beyond HTTP.
 */
export const TriggerEventType = {
  Http: "HTTP",
} as const;
/**
 * The type of event that triggers the workflow. In this case, the workflow is initiated
 *
 * @remarks
 * by an HTTP request. Future iterations may support additional event types beyond HTTP.
 */
export type TriggerEventType = ClosedEnum<typeof TriggerEventType>;

/**
 * The HTTP method used to trigger the workflow. This defines the type of request
 *
 * @remarks
 * that will initiate the workflow (e.g., GET, POST).
 */
export const MethodResponse = {
  Get: "GET",
  Post: "POST",
  Put: "PUT",
  Patch: "PATCH",
  Delete: "DELETE",
} as const;
/**
 * The HTTP method used to trigger the workflow. This defines the type of request
 *
 * @remarks
 * that will initiate the workflow (e.g., GET, POST).
 */
export type MethodResponse = ClosedEnum<typeof MethodResponse>;

/**
 * Configuration details specific to HTTP-triggered workflows. This object describes the
 *
 * @remarks
 * HTTP method and URL that will trigger the workflow, providing the endpoint and method
 * that should be used to initiate the workflow.
 */
export type TriggerHttpConfig = {
  /**
   * The HTTP method used to trigger the workflow. This defines the type of request
   *
   * @remarks
   * that will initiate the workflow (e.g., GET, POST).
   */
  method?: MethodResponse | undefined;
  /**
   * The URL that is associated with the trigger event. This is the endpoint that must be
   *
   * @remarks
   * called using the specified HTTP method to start the workflow.
   */
  url?: string | null | undefined;
};

/**
 * The default value for the input field if one is provided. This can be a string, number,
 *
 * @remarks
 * boolean, object, or array. If no value is provided during the trigger, the workflow may
 * use this default value.
 */
export type DefaultValue =
  | string
  | number
  | boolean
  | { [k: string]: any }
  | Array<any>;

/**
 * Array of fields required as inputs to trigger the workflow, including field names, data types, and default values.
 */
export type TriggerInputSchema = {
  /**
   * The name of the input field expected by the workflow. This key must match the name
   *
   * @remarks
   * provided in the `trigger_inputs` when triggering the workflow.
   */
  fieldName?: string | undefined;
  /**
   * The data type expected for the input field. This indicates whether the input should
   *
   * @remarks
   * be a string, number, boolean, object, or array, ensuring the data is passed in the
   * correct format.
   */
  fieldDataType?: string | undefined;
  /**
   * The default value for the input field if one is provided. This can be a string, number,
   *
   * @remarks
   * boolean, object, or array. If no value is provided during the trigger, the workflow may
   * use this default value.
   */
  defaultValue?:
    | string
    | number
    | boolean
    | { [k: string]: any }
    | Array<any>
    | undefined;
};

/**
 * Metadata related to the workflow definition, including details about when the workflow was
 *
 * @remarks
 * created, who created it, when it was last modified, and by whom. This information is useful
 * for tracking changes to the workflow over time and identifying the user or system responsible
 * for creating or modifying the workflow.
 */
export type MetadataResponse = {
  /**
   * Timestamp when the agreement document was created.
   */
  createdAt?: Date | null | undefined;
  /**
   * User ID of the person who created the agreement document.
   */
  createdBy?: string | null | undefined;
  /**
   * Timestamp when the agreement document was last modified.
   */
  modifiedAt?: Date | null | undefined;
  /**
   * User ID of the person who last modified the agreement document.
   */
  modifiedBy?: string | null | undefined;
  /**
   * Unique identifier for the request, useful for tracking and debugging.
   */
  requestId?: string | null | undefined;
  /**
   * The timestamp indicating when the response was generated.
   */
  responseTimestamp?: Date | null | undefined;
  /**
   * The duration of time, in milliseconds, that the server took to process and respond
   *
   * @remarks
   * to the request. This is measured from the time the server received the request
   * until the time the response was sent.
   */
  responseDurationMs?: number | null | undefined;
};

/**
 * Control information and metadata for the response.
 */
export type WorkflowTriggerRequirementsSuccess = {
  triggerId: string;
  /**
   * The type of event that triggers the workflow. In this case, the workflow is initiated
   *
   * @remarks
   * by an HTTP request. Future iterations may support additional event types beyond HTTP.
   */
  triggerEventType?: TriggerEventType | undefined;
  /**
   * Configuration details specific to HTTP-triggered workflows. This object describes the
   *
   * @remarks
   * HTTP method and URL that will trigger the workflow, providing the endpoint and method
   * that should be used to initiate the workflow.
   */
  triggerHttpConfig?: TriggerHttpConfig | undefined;
  /**
   * A list of input fields that define the structure of the data required to trigger the workflow.
   *
   * @remarks
   * Each item describes a field that must be included in the request when the workflow is triggered.
   * The schema includes the field name, expected data type, and any default values for the input.
   */
  triggerInputSchema?: Array<TriggerInputSchema> | undefined;
  /**
   * Metadata related to the workflow definition, including details about when the workflow was
   *
   * @remarks
   * created, who created it, when it was last modified, and by whom. This information is useful
   * for tracking changes to the workflow over time and identifying the user or system responsible
   * for creating or modifying the workflow.
   */
  metadata?: MetadataResponse | undefined;
  /**
   * The maximum number of items that can be returned in a single page.
   */
  pageLimit: number | null;
  /**
   * Unique identifier for the request, useful for tracking and debugging.
   */
  requestId: string | null;
  /**
   * The timestamp indicating when the response was generated.
   */
  responseTimestamp: Date | null;
  /**
   * The duration of time, in milliseconds, that the server took to process and respond
   *
   * @remarks
   * to the request. This is measured from the time the server received the request
   * until the time the response was sent.
   */
  responseDurationMs: number | null;
};

/** @internal */
export const TriggerEventType$inboundSchema: z.ZodNativeEnum<
  typeof TriggerEventType
> = z.nativeEnum(TriggerEventType);

/** @internal */
export const MethodResponse$inboundSchema: z.ZodNativeEnum<
  typeof MethodResponse
> = z.nativeEnum(MethodResponse);

/** @internal */
export const TriggerHttpConfig$inboundSchema: z.ZodType<
  TriggerHttpConfig,
  z.ZodTypeDef,
  unknown
> = z.object({
  method: types.optional(MethodResponse$inboundSchema),
  url: z.nullable(types.string()).optional(),
});

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

/** @internal */
export const DefaultValue$inboundSchema: z.ZodType<
  DefaultValue,
  z.ZodTypeDef,
  unknown
> = smartUnion([
  types.string(),
  types.number(),
  types.boolean(),
  z.record(z.any()),
  z.array(z.any()),
]);

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

/** @internal */
export const TriggerInputSchema$inboundSchema: z.ZodType<
  TriggerInputSchema,
  z.ZodTypeDef,
  unknown
> = z.object({
  field_name: types.optional(types.string()),
  field_data_type: types.optional(types.string()),
  default_value: types.optional(
    smartUnion([
      types.string(),
      types.number(),
      types.boolean(),
      z.record(z.any()),
      z.array(z.any()),
    ]),
  ),
}).transform((v) => {
  return remap$(v, {
    "field_name": "fieldName",
    "field_data_type": "fieldDataType",
    "default_value": "defaultValue",
  });
});

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

/** @internal */
export const MetadataResponse$inboundSchema: z.ZodType<
  MetadataResponse,
  z.ZodTypeDef,
  unknown
> = z.object({
  created_at: z.nullable(types.date()).optional(),
  created_by: z.nullable(types.string()).optional(),
  modified_at: z.nullable(types.date()).optional(),
  modified_by: z.nullable(types.string()).optional(),
  request_id: z.nullable(types.string()).optional(),
  response_timestamp: z.nullable(types.date()).optional(),
  response_duration_ms: z.nullable(types.number()).optional(),
}).transform((v) => {
  return remap$(v, {
    "created_at": "createdAt",
    "created_by": "createdBy",
    "modified_at": "modifiedAt",
    "modified_by": "modifiedBy",
    "request_id": "requestId",
    "response_timestamp": "responseTimestamp",
    "response_duration_ms": "responseDurationMs",
  });
});

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

/** @internal */
export const WorkflowTriggerRequirementsSuccess$inboundSchema: z.ZodType<
  WorkflowTriggerRequirementsSuccess,
  z.ZodTypeDef,
  unknown
> = z.object({
  trigger_id: types.string().default("00000000-0000-0000-0000-000000000000"),
  trigger_event_type: types.optional(TriggerEventType$inboundSchema),
  trigger_http_config: types.optional(
    z.lazy(() => TriggerHttpConfig$inboundSchema),
  ),
  trigger_input_schema: types.optional(
    z.array(z.lazy(() => TriggerInputSchema$inboundSchema)),
  ),
  metadata: types.optional(z.lazy(() => MetadataResponse$inboundSchema)),
  page_limit: z.nullable(types.number().default(25)),
  request_id: types.nullable(types.string()),
  response_timestamp: types.nullable(types.date()),
  response_duration_ms: types.nullable(types.number()),
}).transform((v) => {
  return remap$(v, {
    "trigger_id": "triggerId",
    "trigger_event_type": "triggerEventType",
    "trigger_http_config": "triggerHttpConfig",
    "trigger_input_schema": "triggerInputSchema",
    "page_limit": "pageLimit",
    "request_id": "requestId",
    "response_timestamp": "responseTimestamp",
    "response_duration_ms": "responseDurationMs",
  });
});

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