
import { Node, Edge } from 'reactflow';
import { FC, ReactNode } from 'react';
import type { PrefixLengthStep } from './prefixRestructure';
import type { EditorAiCapability } from './aiCapability';

export interface Attribute {
  role: string;
  value: string;
}

export interface Member {
  id: string;
  name: string;
  email: string;
  role: string;
  status: 'Active' | 'Inactive' | 'Pending';
  avatarUrl?: string;
  sourceGroup?: string;
}

export type MemberFetcher = (
  nodeId: string,
  nodeName: string,
  page: number,
  query: string,
  inclusive: boolean
) => Promise<{ data: Member[], total: number }>;

export interface NodeData {
  name: string;
  description: string;
  iconPath: string;
  color: string;
  isRootNode: boolean;
  inCycle: boolean;
  
  // Status Flags
  isActive: boolean;
  isDeleted: boolean;
  isSuspended: boolean;

  // Lifecycle Details
  deletionReason?: string | null;
  deletionMessage?: string | null;
  suspensionReason?: string | null;
  suspensionMessage?: string | null;

  attributes: Attribute[];
  properties: any[];
  memberCount?: number;
  inclusiveMemberCount?: number;
  descendants: string[];
}

export interface GroupData {
  id: string;
  name: string;
  type: string;
  mode: 'create' | 'edit';
}

export interface EdgeData {
  inCycle: boolean;
  inDesc: boolean;
}

export enum LocalKeys {
  EditingLocked = 'Editing Locked',
  CanvasLocked = 'Canvas Locked',
  TargetHasParent = 'A group can only have one parent.',
  AcyclicGraph = 'Graph is acyclic',
  CyclicGraph = 'Warning: Cycles detected in hierarchy!',
  RootNode = 'Root Group',
  SaveSuccess = 'Saved Successfully',
  MemberCount = 'Members',
  Attributes = 'Attributes',
  Description = 'Description',
  GroupIcon = 'Group Icon',
  GroupName = 'Group Name'
}

// --- Component Props & State Interfaces ---

export interface HistoryState {
  nodes: Node<NodeData>[];
  edges: Edge[];
}

export interface InnerProps {
  groupData: GroupData;
  nodes: Node<NodeData>[];
  edges?: Edge[];
  onSave: (data: any) => void;
  onExport: (data: any) => void;
  onBack?: () => void;
  isValidated: boolean;
  enableForce?: boolean;
  // Context dependencies passed down
  onFetchMembers?: MemberFetcher;
  icons?: Record<string, any>;
  // Optional, additive: receive a granular validating AI capability for this
  // editor's live model. Called with the capability when ready and with null on
  // unmount. Does not affect any existing behaviour.
  onAiCapabilityReady?: (cap: EditorAiCapability | null) => void;
}

export interface GroupEditorProps extends InnerProps {
  localKey: string;
}

export interface GroupPanelProps {
  mode: string;
  id: string;
  name: string;
  type: string;
  readOnly: boolean;
  disabled: boolean;
  onGroupEdited: (p: { name: string, type: string }) => void;
  onExport: () => void;
  onImport: () => void;
}

export interface NodeInspectorProps {
  node: Node<NodeData> | null;
  readOnly: boolean;
  onUpdateNode: (nodeId: string, data: Partial<NodeData>) => void;
  onClose: () => void;
}

export interface SaveDialogProps {
  open: boolean;
  onClose: () => void;
  onSubmit: (reason: string, message: string) => void;
  saving: boolean;
}

export interface RegexConnectModalProps {
  open: boolean;
  /** The node that matched nodes will be re-parented under. */
  parentNode: Node<NodeData> | null;
  /** Nodes eligible to be re-parented (excludes the parent, the root and the parent's ancestors). */
  candidates: Node<NodeData>[];
  onClose: () => void;
  /** Called with the ids of the nodes the user confirmed to connect. */
  onConfirm: (targetIds: string[]) => void;
}

export interface PrefixRestructureModalProps {
  open: boolean;
  /** The node that generated prefix paths will be placed under. */
  parentNode: Node<NodeData> | null;
  /** Nodes eligible to participate in the prefix restructure. */
  candidates: Node<NodeData>[];
  onClose: () => void;
  /** Called with the ordered prefix pattern the user confirmed. */
  onConfirm: (lengths: PrefixLengthStep[]) => void;
}

export interface EditorContextType {
  onFetchMembers?: MemberFetcher;
  iconMap: Record<string, any>;
  onRemoveEdge?: (edgeId: string) => void;
  onUpdateNode?: (nodeId: string, data: Partial<NodeData>) => void;
}

// Component Exports
declare const GroupEditor: FC<GroupEditorProps>;
export default GroupEditor;
export { GroupEditor };

// Editor Context
export declare const DefaultIcons: Record<string, any>;
export declare const EditorContextProvider: FC<{
  children: ReactNode;
  onFetchMembers?: MemberFetcher;
  iconMap?: Record<string, any>;
}>;
export declare const useEditorContext: () => EditorContextType;

// Utilities
export declare class DiGraph {
  addVertex(v: string): void;
  addEdge(from: string, to: string): void;
  findCycles(): string[][];
  getDeepChildren(v: string): string[];
}

export declare function createEmptyNode(id: string, position: { x: number; y: number }, isRoot: boolean): Node<NodeData>;
export declare function groupFromNode(node: Node<NodeData>, edges: Edge[]): any;
export declare function transformJsonToGraph(data: any): { nodes: Node<NodeData>[]; edges: Edge[] };
