import type { DiagramNode, DiagramEdge, DiagramLayout } from '../../api/queries/diagrams';
import type { NodeExecutionState, IterationInfo } from '../ExecutionDiagram/types';

export type NodeAction = 'inspect' | 'toggle-trace' | 'configure-tap' | 'copy-id';

export interface NodeConfig {
  traceEnabled?: boolean;
  tapExpression?: string;
}

export interface DiagramSection {
  label: string;
  nodes: DiagramNode[];
  edges: DiagramEdge[];
  offsetY: number;
  variant?: 'error' | 'completion';
}

export interface LatencyHeatmapEntry {
  avgDurationMs: number;
  p99DurationMs: number;
  /** Percentage of total route time this processor consumes (0-100) */
  pctOfRoute: number;
  /** Additional fields for diagram tooltip (optional — populated by dashboard) */
  processorType?: string;
  totalCount?: number;
  errorRate?: number;
}

export interface ProcessDiagramProps {
  application: string;
  routeId: string;
  direction?: 'LR' | 'TB';
  selectedNodeId?: string;
  onNodeSelect?: (nodeId: string) => void;
  onNodeAction?: (nodeId: string, action: NodeAction) => void;
  nodeConfigs?: Map<string, NodeConfig>;
  /** Known route IDs for this application (enables drill-down resolution) */
  knownRouteIds?: Set<string>;
  /** Maps from() endpoint URI → routeId for cross-route drill-down */
  endpointRouteMap?: Map<string, string>;
  className?: string;
  /** Pre-fetched diagram layout (bypasses internal fetch by application/routeId) */
  diagramLayout?: DiagramLayout;
  /** Execution overlay: maps diagram node ID → execution state */
  executionOverlay?: Map<string, NodeExecutionState>;
  /** Per-compound iteration info: maps compound node ID → iteration info */
  iterationState?: Map<string, IterationInfo>;
  /** Called when user changes iteration on a compound stepper */
  onIterationChange?: (compoundNodeId: string, iterationIndex: number) => void;
  /** When set, the diagram pans to center this node in the viewport */
  centerOnNodeId?: string;
  /** Latency heatmap: maps processor ID → aggregate performance data.
   *  When provided, nodes are colored green→yellow→red by relative latency. */
  latencyHeatmap?: Map<string, LatencyHeatmapEntry>;
}
