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

import * as z from "zod";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  TriggerInputs,
  TriggerInputs$inboundSchema,
  TriggerInputs$Outbound,
  TriggerInputs$outboundSchema,
} from "./triggerinputs.js";

/**
 * Additional metadata related to this workflow instance
 */
export type Metadata = {
  /**
   * 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?: Metadata | undefined;
};

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

/** @internal */
export type Metadata$Outbound = {
  workflow_created_by?: string | undefined;
  workflow_version?: string | undefined;
  workflow_metadata_id?: string | undefined;
};

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

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace Metadata$ {
  /** @deprecated use `Metadata$inboundSchema` instead. */
  export const inboundSchema = Metadata$inboundSchema;
  /** @deprecated use `Metadata$outboundSchema` instead. */
  export const outboundSchema = Metadata$outboundSchema;
  /** @deprecated use `Metadata$Outbound` instead. */
  export type Outbound = Metadata$Outbound;
}

export function metadataToJSON(metadata: Metadata): string {
  return JSON.stringify(Metadata$outboundSchema.parse(metadata));
}

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

/** @internal */
export const WorkflowInstance$inboundSchema: z.ZodType<
  WorkflowInstance,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: z.string().optional(),
  name: z.string().optional(),
  workflow_status: z.string().optional(),
  template_id: z.string().optional(),
  account_id: z.string().optional(),
  started_at: z.string().datetime({ offset: true }).transform(v => new Date(v))
    .optional(),
  started_by: z.string().optional(),
  started_by_name: z.string().optional(),
  started_by_role: z.string().optional(),
  ended_at: z.nullable(
    z.string().datetime({ offset: true }).transform(v => new Date(v)),
  ).optional(),
  expires_at: z.nullable(
    z.string().datetime({ offset: true }).transform(v => new Date(v)),
  ).optional(),
  last_modified_at: z.string().datetime({ offset: true }).transform(v =>
    new Date(v)
  ).optional(),
  canceled_at: z.nullable(
    z.string().datetime({ offset: true }).transform(v => new Date(v)),
  ).optional(),
  canceled_by: z.nullable(z.string()).optional(),
  trigger_inputs: z.record(TriggerInputs$inboundSchema).optional(),
  total_steps: z.number().int().optional(),
  last_completed_step: z.number().int().optional(),
  last_completed_step_name: z.nullable(z.string()).optional(),
  tags: z.array(z.string()).optional(),
  metadata: z.lazy(() => Metadata$inboundSchema).optional(),
}).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",
  });
});

/** @internal */
export type WorkflowInstance$Outbound = {
  id?: string | undefined;
  name?: string | undefined;
  workflow_status?: string | undefined;
  template_id?: string | undefined;
  account_id?: string | undefined;
  started_at?: string | undefined;
  started_by?: string | undefined;
  started_by_name?: string | undefined;
  started_by_role?: string | undefined;
  ended_at?: string | null | undefined;
  expires_at?: string | null | undefined;
  last_modified_at?: string | undefined;
  canceled_at?: string | null | undefined;
  canceled_by?: string | null | undefined;
  trigger_inputs?: { [k: string]: TriggerInputs$Outbound } | undefined;
  total_steps?: number | undefined;
  last_completed_step?: number | undefined;
  last_completed_step_name?: string | null | undefined;
  tags?: Array<string> | undefined;
  metadata?: Metadata$Outbound | undefined;
};

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

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace WorkflowInstance$ {
  /** @deprecated use `WorkflowInstance$inboundSchema` instead. */
  export const inboundSchema = WorkflowInstance$inboundSchema;
  /** @deprecated use `WorkflowInstance$outboundSchema` instead. */
  export const outboundSchema = WorkflowInstance$outboundSchema;
  /** @deprecated use `WorkflowInstance$Outbound` instead. */
  export type Outbound = WorkflowInstance$Outbound;
}

export function workflowInstanceToJSON(
  workflowInstance: WorkflowInstance,
): string {
  return JSON.stringify(
    WorkflowInstance$outboundSchema.parse(workflowInstance),
  );
}

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