export type VibeNodeType =
  | 'Agent'
  | 'CustomNode'
  | 'Graph'
  | 'Loop'
  | 'LogicSwitch'
  | 'AgentSwitch'
  | string;

export type VibeNodeSpec = {
  name: string;
  type?: VibeNodeType;
  description?: unknown;
  /**
   * Stage-1 `graph_design.json` (v4 schema) uses `label` for human-friendly node text.
   * We keep it optional so the editor can open richer formats without forcing it.
   */
  label?: string;
  /**
   * Stage-1 `graph_design.json` (v4 schema) uses `agent` to reference an existing agent/role.
   * (For now, this only applies to Action/Agent nodes.)
   */
  agent?: string;
  parent?: string;
  max_iterations?: number;
  /**
   * Unified graph_design spec: terminate_condition_prompt is a natural-language
   * description/prompt used by the Loop controller to decide when to stop.
   */
  terminate_condition_prompt?: unknown;
  terminate_condition_expr?: unknown;
  terminate_condition_code?: unknown;
  instructions?: unknown;
  prompt?: unknown;
  prompt_template?: unknown;
  /**
   * Unified graph_design spec: list of allowed tool names for an Action node.
   */
  tools?: unknown;
  pull_keys?: unknown;
  push_keys?: unknown;
  attributes?: unknown;
  condition_bindings?: unknown;
  branches?: unknown;
  forward_body?: unknown;
  code?: unknown;
  [k: string]: unknown;
};

export type VibeEdgeSpec = {
  from: string;
  to: string;
  /**
   * Unified graph_design spec: edge condition (required for Switch outgoing).
   */
  condition?: unknown;
  keys?: unknown;
  [k: string]: unknown;
};

export type VibeGraphDesign = {
  Nodes: VibeNodeSpec[];
  Edges: VibeEdgeSpec[];
  [k: string]: unknown;
};

export type VibeLayout = Record<string, { x: number; y: number }>;

export type VibeLayoutMeta = {
  /**
   * The graphStructureSignature used when the current layout was last
   * auto-generated by the editor.
   */
  autoSig: string | null;
  /**
   * True once the user manually adjusts node positions (drag/drop), meaning we
   * should not auto-relayout on graph changes.
   */
  userTouched: boolean;
};

export type VibeGraphLocator = {
  path: Array<string | number>;
  asString: boolean;
};

export type VibeHistorySnapshot = {
  graph?: VibeGraphDesign;
  layout?: VibeLayout;
  ts: number;
};

export type VibeDocState = {
  uri: string;
  fileName: string;
  sourceText: string;
  graph: VibeGraphDesign | null;
  graphLocator: VibeGraphLocator | null;
  baseGraphSig: string | null;
  parseError: string | null;
  dirtyGraph: VibeGraphDesign | null;
  dirty: boolean;
  saving: boolean;
  saveError: string | null;
};
