/*
 * 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 { Result as SafeParseResult } from "../../types/fp.js";
import * as types from "../../types/primitives.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import { TriggerInputs, TriggerInputs$inboundSchema } from "./triggerinputs.js";

/**
 * Additional metadata related to this workflow instance
 */
export type WorkflowInstanceMetadata = {
  /**
   * Identifier of the user who originally created the workflow definition
   */
  workflowCreatedBy?: string | undefined;
  /**
   * Version string of the deployed workflow
   */
  workflowVersion?: string | undefined;
  /**
   * Identifier for workflow definition metadata in the system
   */
  workflowMetadataId?: string | undefined;
};

export type WorkflowInstance = {
  /**
   * Unique identifier for the workflow instance
   */
  id?: string | undefined;
  /**
   * Human-readable name for the workflow instance
   */
  name?: string | undefined;
  /**
   * Current status of the workflow (e.g. In Progress, Completed, Canceled)
   */
  workflowStatus?: string | undefined;
  /**
   * Identifier linking this instance to a workflow template
   */
  templateId?: string | undefined;
  /**
   * Account under which this workflow instance was initiated
   */
  accountId?: string | undefined;
  /**
   * Date and time when the workflow was started
   */
  startedAt?: Date | undefined;
  /**
   * User or system identifier that started this workflow
   */
  startedBy?: string | undefined;
  /**
   * Display name of the user who started this workflow
   */
  startedByName?: string | undefined;
  /**
   * Role of the user who started this workflow (e.g. Preparer)
   */
  startedByRole?: string | undefined;
  /**
   * Date and time when the workflow completed
   */
  endedAt?: Date | null | undefined;
  /**
   * Date and time after which the workflow expires
   */
  expiresAt?: Date | null | undefined;
  /**
   * Date and time when the workflow instance was last modified
   */
  lastModifiedAt?: Date | undefined;
  /**
   * Date and time when the workflow was canceled (if applicable)
   */
  canceledAt?: Date | null | undefined;
  /**
   * User or system identifier that canceled this workflow (if applicable)
   */
  canceledBy?: string | null | undefined;
  /**
   * Key-value pairs representing the input data required to trigger the workflow.
   *
   * @remarks
   * The keys correspond to the `field_name` values defined in the `trigger_input_schema` of the workflow definition.
   * The values should match the specified `field_data_type` (e.g., string, number, boolean).
   * Example: {"name": "John Doe", "email": "johndoe@example.com"}
   */
  triggerInputs?: { [k: string]: TriggerInputs } | undefined;
  /**
   * Total number of steps configured in the workflow
   */
  totalSteps?: number | undefined;
  /**
   * The index of the most recently completed step
   */
  lastCompletedStep?: number | undefined;
  /**
   * The name of the most recently completed step
   */
  lastCompletedStepName?: string | null | undefined;
  /**
   * Custom tags for organization or filtering
   */
  tags?: Array<string> | undefined;
  /**
   * Additional metadata related to this workflow instance
   */
  metadata?: WorkflowInstanceMetadata | undefined;
};

/** @internal */
export const WorkflowInstanceMetadata$inboundSchema: z.ZodType<
  WorkflowInstanceMetadata,
  z.ZodTypeDef,
  unknown
> = z.object({
  workflow_created_by: types.optional(types.string()),
  workflow_version: types.optional(types.string()),
  workflow_metadata_id: types.optional(types.string()),
}).transform((v) => {
  return remap$(v, {
    "workflow_created_by": "workflowCreatedBy",
    "workflow_version": "workflowVersion",
    "workflow_metadata_id": "workflowMetadataId",
  });
});

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

/** @internal */
export const WorkflowInstance$inboundSchema: z.ZodType<
  WorkflowInstance,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.optional(types.string()),
  name: types.optional(types.string()),
  workflow_status: types.optional(types.string()),
  template_id: types.optional(types.string()),
  account_id: types.optional(types.string()),
  started_at: types.optional(types.date()),
  started_by: types.optional(types.string()),
  started_by_name: types.optional(types.string()),
  started_by_role: types.optional(types.string()),
  ended_at: z.nullable(types.date()).optional(),
  expires_at: z.nullable(types.date()).optional(),
  last_modified_at: types.optional(types.date()),
  canceled_at: z.nullable(types.date()).optional(),
  canceled_by: z.nullable(types.string()).optional(),
  trigger_inputs: types.optional(z.record(TriggerInputs$inboundSchema)),
  total_steps: types.optional(types.number()),
  last_completed_step: types.optional(types.number()),
  last_completed_step_name: z.nullable(types.string()).optional(),
  tags: types.optional(z.array(types.string())),
  metadata: types.optional(
    z.lazy(() => WorkflowInstanceMetadata$inboundSchema),
  ),
}).transform((v) => {
  return remap$(v, {
    "workflow_status": "workflowStatus",
    "template_id": "templateId",
    "account_id": "accountId",
    "started_at": "startedAt",
    "started_by": "startedBy",
    "started_by_name": "startedByName",
    "started_by_role": "startedByRole",
    "ended_at": "endedAt",
    "expires_at": "expiresAt",
    "last_modified_at": "lastModifiedAt",
    "canceled_at": "canceledAt",
    "canceled_by": "canceledBy",
    "trigger_inputs": "triggerInputs",
    "total_steps": "totalSteps",
    "last_completed_step": "lastCompletedStep",
    "last_completed_step_name": "lastCompletedStepName",
  });
});

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