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

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 { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  WorkflowExecutionFailedAttributes,
  WorkflowExecutionFailedAttributes$inboundSchema,
} from "./workflowexecutionfailedattributes.js";

/**
 * Emitted when a workflow execution fails due to an unhandled exception.
 *
 * @remarks
 *
 * This is a terminal event indicating the workflow ended with an error.
 */
export type WorkflowExecutionFailedResponse = {
  /**
   * Unique identifier for this event instance.
   */
  eventId: string;
  /**
   * Unix timestamp in nanoseconds when the event was created.
   */
  eventTimestamp: number;
  /**
   * Execution ID of the root workflow that initiated this execution chain.
   */
  rootWorkflowExecId: string;
  /**
   * Execution ID of the parent workflow that initiated this execution. If this is a root workflow, this field is not set.
   */
  parentWorkflowExecId: string | null;
  /**
   * Execution ID of the workflow that emitted this event.
   */
  workflowExecId: string;
  /**
   * Run ID of the workflow execution. Changes on continue-as-new while workflow_exec_id stays the same.
   */
  workflowRunId: string;
  /**
   * The registered name of the workflow that emitted this event.
   */
  workflowName: string;
  /**
   * Event type discriminator.
   */
  eventType: "WORKFLOW_EXECUTION_FAILED";
  /**
   * Attributes for workflow execution failed events.
   */
  attributes: WorkflowExecutionFailedAttributes;
};

/** @internal */
export const WorkflowExecutionFailedResponse$inboundSchema: z.ZodType<
  WorkflowExecutionFailedResponse,
  unknown
> = z.object({
  event_id: z.string(),
  event_timestamp: z.int(),
  root_workflow_exec_id: z.string(),
  parent_workflow_exec_id: z.nullable(z.string()),
  workflow_exec_id: z.string(),
  workflow_run_id: z.string(),
  workflow_name: z.string(),
  event_type: z.literal("WORKFLOW_EXECUTION_FAILED").default(
    "WORKFLOW_EXECUTION_FAILED",
  ),
  attributes: WorkflowExecutionFailedAttributes$inboundSchema,
}).transform((v) => {
  return remap$(v, {
    "event_id": "eventId",
    "event_timestamp": "eventTimestamp",
    "root_workflow_exec_id": "rootWorkflowExecId",
    "parent_workflow_exec_id": "parentWorkflowExecId",
    "workflow_exec_id": "workflowExecId",
    "workflow_run_id": "workflowRunId",
    "workflow_name": "workflowName",
    "event_type": "eventType",
  });
});

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