/**
 * Canvas Graph Hook
 *
 * Convenience hook for reading graph structure (node/edge counts and keys).
 */

'use client';

import { useAtomValue } from 'jotai';
import { graphAtom } from '../core/graph-store';
import { nodeKeysAtom, edgeKeysAtom } from '../core/graph-derived';
import type { GraphNodeAttributes, GraphEdgeAttributes } from '../core/types';

export interface UseCanvasGraphReturn {
	/** Number of nodes in the graph */
	nodeCount: number;
	/** Number of edges in the graph */
	edgeCount: number;
	/** All node IDs */
	nodeKeys: string[];
	/** All edge IDs */
	edgeKeys: string[];
	/** Get a node's attributes by ID (returns undefined if not found) */
	getNode: (id: string) => GraphNodeAttributes | undefined;
	/** Get an edge's attributes by ID (returns undefined if not found) */
	getEdge: (id: string) => GraphEdgeAttributes | undefined;
}

/**
 * Hook to access graph structure information.
 *
 * @example
 * ```tsx
 * function GraphInfo() {
 *   const { nodeCount, edgeCount, getNode } = useCanvasGraph();
 *   return <div>{nodeCount} nodes, {edgeCount} edges</div>;
 * }
 * ```
 */
export function useCanvasGraph(): UseCanvasGraphReturn {
	const graph = useAtomValue(graphAtom);
	const nodeKeys = useAtomValue(nodeKeysAtom);
	const edgeKeys = useAtomValue(edgeKeysAtom);

	const getNode = (id: string): GraphNodeAttributes | undefined => {
		return graph.hasNode(id) ? graph.getNodeAttributes(id) : undefined;
	};

	const getEdge = (id: string): GraphEdgeAttributes | undefined => {
		return graph.hasEdge(id) ? graph.getEdgeAttributes(id) : undefined;
	};

	return {
		nodeCount: nodeKeys.length,
		edgeCount: edgeKeys.length,
		nodeKeys,
		edgeKeys,
		getNode,
		getEdge,
	};
}
