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

import * as z from "zod/v4";
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 { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  WorkflowExecutionProgressTraceEvent,
  WorkflowExecutionProgressTraceEvent$inboundSchema,
} from "./workflowexecutionprogresstraceevent.js";
import {
  WorkflowExecutionStatus,
  WorkflowExecutionStatus$inboundSchema,
} from "./workflowexecutionstatus.js";
import {
  WorkflowExecutionTraceEvent,
  WorkflowExecutionTraceEvent$inboundSchema,
} from "./workflowexecutiontraceevent.js";

export type WorkflowExecutionTraceEventsResponseEvent =
  | WorkflowExecutionProgressTraceEvent
  | WorkflowExecutionTraceEvent;

export type WorkflowExecutionTraceEventsResponse = {
  /**
   * The name of the workflow
   */
  workflowName: string;
  /**
   * The ID of the workflow execution
   */
  executionId: string;
  /**
   * The parent execution ID of the workflow execution
   */
  parentExecutionId?: string | null | undefined;
  /**
   * The root execution ID of the workflow execution
   */
  rootExecutionId: string;
  /**
   * The unique run identifier (database UUID)
   */
  runId?: string | null | undefined;
  /**
   * The status of the workflow execution
   */
  status: WorkflowExecutionStatus | null;
  /**
   * The start time of the workflow execution
   */
  startTime: Date;
  /**
   * The end time of the workflow execution, if available
   */
  endTime: Date | null;
  /**
   * The total duration of the trace in milliseconds
   */
  totalDurationMs?: number | null | undefined;
  /**
   * The result of the workflow execution, if available
   */
  result: any | null;
  /**
   * The events of the workflow execution
   */
  events?:
    | Array<WorkflowExecutionProgressTraceEvent | WorkflowExecutionTraceEvent>
    | undefined;
};

/** @internal */
export const WorkflowExecutionTraceEventsResponseEvent$inboundSchema: z.ZodType<
  WorkflowExecutionTraceEventsResponseEvent,
  unknown
> = smartUnion([
  WorkflowExecutionProgressTraceEvent$inboundSchema,
  WorkflowExecutionTraceEvent$inboundSchema,
]);

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

/** @internal */
export const WorkflowExecutionTraceEventsResponse$inboundSchema: z.ZodType<
  WorkflowExecutionTraceEventsResponse,
  unknown
> = z.object({
  workflow_name: z.string(),
  execution_id: z.string(),
  parent_execution_id: z.nullable(z.string()).optional(),
  root_execution_id: z.string(),
  run_id: z.nullable(z.string()).optional(),
  status: z.nullable(WorkflowExecutionStatus$inboundSchema),
  start_time: z.iso.datetime({ offset: true }).transform(v => new Date(v)),
  end_time: z.nullable(
    z.iso.datetime({ offset: true }).transform(v => new Date(v)),
  ),
  total_duration_ms: z.nullable(z.int()).optional(),
  result: z.nullable(z.any()),
  events: z.array(
    smartUnion([
      WorkflowExecutionProgressTraceEvent$inboundSchema,
      WorkflowExecutionTraceEvent$inboundSchema,
    ]),
  ).optional(),
}).transform((v) => {
  return remap$(v, {
    "workflow_name": "workflowName",
    "execution_id": "executionId",
    "parent_execution_id": "parentExecutionId",
    "root_execution_id": "rootExecutionId",
    "run_id": "runId",
    "start_time": "startTime",
    "end_time": "endTime",
    "total_duration_ms": "totalDurationMs",
  });
});

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