import { z } from 'zod';

/**
 * Output produced by a single DAG node after execution.
 *
 * Captures the result text, structured fields, and execution metadata
 * so downstream nodes can reference outputs via `$nodeId.output` syntax.
 */
export const nodeOutputSchema = z.object({
  /** The node id that produced this output. */
  nodeId: z.string(),

  /** Current state of the node execution. */
  state: z.enum(['pending', 'running', 'completed', 'failed', 'skipped']),

  /** The raw text output from the node (alias for text for backward compat). */
  output: z.string().default(''),

  /** The raw text output from the node. */
  text: z.string().optional(),

  /** Structured output fields (when output_format is defined). */
  fields: z.record(z.unknown()).optional(),

  /** Error message if the node failed. */
  error: z.string().optional(),

  /** Token usage or cost metadata. */
  costUsd: z.number().optional(),
});

export type NodeOutput = z.infer<typeof nodeOutputSchema>;

/**
 * Result of executing a single node within the DAG.
 *
 * Internal to the engine — not persisted directly but used to build
 * the nodeOutputs map that flows between layers.
 */
export interface NodeExecutionResult {
  state: 'completed' | 'failed' | 'skipped';
  output?: string;
  fields?: Record<string, unknown>;
  error?: string;
  costUsd?: number;
}