/**
 * Action Type Definitions
 * All action-related types and validation schemas
 */

import { z } from 'zod'

/**
 * Agent action types
 * Discriminated union for type-safe action handling
 */
export type AgentAction =
  | ToolCallAction
  | CompleteAction
  | MessageAction
  | NavigateKnowledgeAction

export interface ToolCallAction {
  type: 'tool-call'
  id: string // Unique ID for tracking tool call/result pairs
  name: string // Tool name
  input: Record<string, unknown> // Tool input (validated against tool's inputSchema)
}

export interface CompleteAction {
  type: 'complete'
}

export interface MessageAction {
  type: 'message'
  text: string // User-facing message text
}

export interface NavigateKnowledgeAction {
  type: 'navigate-knowledge'
  id: string // Unique ID for tracking action/result pairs
  nodeId: string // Knowledge node identifier
}

/**
 * Zod schemas for runtime validation
 */
export const ToolCallActionSchema = z.object({
  type: z.literal('tool-call'),
  id: z.string(),
  name: z.string(),
  input: z.any() // Use z.any() instead of z.unknown() for JSON Schema compatibility
})

export const CompleteActionSchema = z.object({
  type: z.literal('complete')
})

export const MessageActionSchema = z.object({
  type: z.literal('message'),
  text: z.string()
})

export const NavigateKnowledgeActionSchema = z.object({
  type: z.literal('navigate-knowledge'),
  id: z.string(),
  nodeId: z.string()
})

export const AgentActionSchema = z.discriminatedUnion('type', [
  ToolCallActionSchema,
  CompleteActionSchema,
  MessageActionSchema,
  NavigateKnowledgeActionSchema
])
