/**
 * useCanvasHistory Hook
 *
 * Provides undo/redo functionality for canvas operations.
 */

import { useAtomValue, useSetAtom, useStore } from 'jotai';
import {
    canUndoAtom,
    canRedoAtom,
    undoAtom,
    redoAtom,
    pushHistoryAtom,
    clearHistoryAtom,
    undoCountAtom,
    redoCountAtom,
    historyLabelsAtom,
    historyStateAtom,
} from '../core/history-store';
import { showToastAtom } from '../core/toast-store';

export interface UseCanvasHistoryOptions {
	/** Deprecated: keyboard shortcuts are owned by InputProvider. */
	enableKeyboardShortcuts?: boolean;
}

/**
 * Hook for undo/redo functionality.
 *
 * @example
 * ```tsx
 * function CanvasToolbar() {
 *   const { undo, redo, canUndo, canRedo, recordSnapshot } = useCanvasHistory();
 *
 *   // Before making changes, record a snapshot:
 *   const handleAddNode = () => {
 *     recordSnapshot('Add node');
 *     addNode({ ... });
 *   };
 *
 *   return (
 *     <>
 *       <button onClick={undo} disabled={!canUndo}>Undo</button>
 *       <button onClick={redo} disabled={!canRedo}>Redo</button>
 *     </>
 *   );
 * }
 * ```
 */
export function useCanvasHistory(options: UseCanvasHistoryOptions = {}) {
    const { enableKeyboardShortcuts: _enableKeyboardShortcuts = false } = options;

    const canUndo = useAtomValue(canUndoAtom);
    const canRedo = useAtomValue(canRedoAtom);
    const undoCount = useAtomValue(undoCountAtom);
    const redoCount = useAtomValue(redoCountAtom);
    const historyLabels = useAtomValue(historyLabelsAtom);

    const undoAction = useSetAtom(undoAtom);
    const redoAction = useSetAtom(redoAtom);
    const pushHistory = useSetAtom(pushHistoryAtom);
    const clearHistory = useSetAtom(clearHistoryAtom);
    const showToast = useSetAtom(showToastAtom);
    const store = useStore();

    const undo = () => {
        const state = store.get(historyStateAtom);
        const label = state.past[state.past.length - 1]?.label;
        const result = undoAction();
        if (result && label) showToast(`Undo: ${label}`);
        return result;
    };

    const redo = () => {
        const state = store.get(historyStateAtom);
        const label = state.future[0]?.label;
        const result = redoAction();
        if (result && label) showToast(`Redo: ${label}`);
        return result;
    };

    /**
     * Record a snapshot before making changes.
     * Call this BEFORE mutating the graph.
     */
    const recordSnapshot = (label?: string) => {
        pushHistory(label);
    };

    const clear = () => {
        clearHistory();
    };

    return {
        /** Undo the last operation */
        undo,
        /** Redo the last undone operation */
        redo,
        /** Whether undo is available */
        canUndo,
        /** Whether redo is available */
        canRedo,
        /** Number of undo steps available */
        undoCount,
        /** Number of redo steps available */
        redoCount,
        /** Labels for history entries (for display) */
        historyLabels,
        /** Record a snapshot before making changes */
        recordSnapshot,
        /** Clear all history */
        clear,
    };
}
