/**
 * Command View Types
 *
 * Unified type definitions for the Command View graph visualization.
 * These types are used by both backend serialization and frontend rendering.
 *
 * Command View shows the resource graph: agents, workflows, triggers, integrations,
 * external resources, and human checkpoints with their relationships.
 */

import type {
  ResourceDefinition,
  TriggerDefinition,
  IntegrationDefinition,
  ExternalResourceDefinition,
  HumanCheckpointDefinition
} from './types'

// ============================================================================
// Node Types - Resources that appear in the graph
// ============================================================================

/**
 * Extended agent metadata for Command View
 * Includes model and capability information for graph display
 */
export interface CommandViewAgent extends ResourceDefinition {
  type: 'agent'
  modelProvider: string // e.g., 'anthropic', 'openai'
  modelId: string // e.g., 'claude-sonnet-4-20250514'
  toolCount: number
  hasKnowledgeMap: boolean
  hasMemory: boolean
  sessionCapable: boolean // Explicit session capability declaration
}

/**
 * Extended workflow metadata for Command View
 * Includes step information for graph display
 */
export interface CommandViewWorkflow extends ResourceDefinition {
  type: 'workflow'
  stepCount: number
  entryPoint: string
}

/**
 * Node type categories for Command View
 * Simplified categorization for UI rendering and layout
 */
export type CommandViewNodeType = 'agent' | 'workflow' | 'trigger' | 'integration' | 'external' | 'human'

/**
 * Union type for all node types in Command View
 * Frontend can use this for type-safe node handling
 */
export type CommandViewNode =
  | CommandViewAgent
  | CommandViewWorkflow
  | TriggerDefinition
  | IntegrationDefinition
  | ExternalResourceDefinition
  | HumanCheckpointDefinition

// ============================================================================
// Edge Types - Relationships between resources
// ============================================================================

/**
 * Relationship types between resources
 *
 * - triggers: Resource initiates/starts another resource (orange)
 * - uses: Resource uses an integration (teal)
 * - approval: Resource requires human approval (yellow)
 */
export type RelationshipType = 'triggers' | 'uses' | 'approval'

/**
 * Command View edge (relationship between resources)
 */
export interface CommandViewEdge {
  id: string
  source: string // Source node ID
  target: string // Target node ID
  relationship: RelationshipType
  label?: string // Optional label for the edge
}

// ============================================================================
// Graph Data Structure
// ============================================================================

/**
 * Command View data structure
 * Complete graph data for visualization
 *
 * Backend serializes this once at startup and serves it via /command-view endpoint.
 * Frontend consumes this directly for graph rendering.
 */
export interface CommandViewData {
  workflows: CommandViewWorkflow[]
  agents: CommandViewAgent[]
  triggers: TriggerDefinition[]
  integrations: IntegrationDefinition[]
  externalResources: ExternalResourceDefinition[]
  humanCheckpoints: HumanCheckpointDefinition[]
  edges: CommandViewEdge[]
}

// ============================================================================
// Type Guards
// ============================================================================

/**
 * Check if a node is an agent
 */
export function isAgent(node: CommandViewNode): node is CommandViewAgent {
  return 'type' in node && node.type === 'agent'
}

/**
 * Check if a node is a workflow
 */
export function isWorkflow(node: CommandViewNode): node is CommandViewWorkflow {
  return 'type' in node && node.type === 'workflow'
}

/**
 * Check if a node is a trigger
 */
export function isTrigger(node: CommandViewNode): node is TriggerDefinition {
  return 'type' in node && node.type === 'trigger'
}

/**
 * Check if a node is an integration
 */
export function isIntegration(node: CommandViewNode): node is IntegrationDefinition {
  return 'type' in node && node.type === 'integration'
}

/**
 * Check if a node is an external resource
 */
export function isExternalResource(node: CommandViewNode): node is ExternalResourceDefinition {
  return 'type' in node && node.type === 'external'
}

/**
 * Check if a node is a human checkpoint
 */
export function isHumanCheckpoint(node: CommandViewNode): node is HumanCheckpointDefinition {
  return 'type' in node && node.type === 'human'
}

// ============================================================================
// Helper Functions
// ============================================================================

/**
 * Get node ID - all resources now use unified resourceId field
 */
export function getNodeId(node: CommandViewNode): string {
  return node.resourceId
}

/**
 * Get node type for categorization
 */
export function getNodeType(node: CommandViewNode): CommandViewNodeType {
  if (isAgent(node)) return 'agent'
  if (isWorkflow(node)) return 'workflow'
  if (isTrigger(node)) return 'trigger'
  if (isIntegration(node)) return 'integration'
  if (isExternalResource(node)) return 'external'
  // isHumanCheckpoint is last fallback
  return 'human'
}
