"use client";

import { useState, useCallback, useRef } from "react";
import { Node, Edge, useNodesState, useEdgesState } from "@xyflow/react";
import { TableNodeData, ExtendedJsonRelationship, ConnectionInfo, SelectedEdgeInfo } from "@/types/dataflowTypes";

export const useDataFlowDesigner = () => {
  const [nodes, setNodes, onNodesChange] = useNodesState<Node<TableNodeData>>([]);
  const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);

  // 상태 관리
  const [selectedColumns, setSelectedColumns] = useState<{ [tableName: string]: string[] }>({});
  const [selectedNodes, setSelectedNodes] = useState<string[]>([]);
  const [pendingConnection, setPendingConnection] = useState<ConnectionInfo | null>(null);
  const [relationships, setRelationships] = useState<any[]>([]); // eslint-disable-line @typescript-eslint/no-unused-vars
  const [currentDiagramId, setCurrentDiagramId] = useState<number | null>(null);
  const [selectedEdgeInfo, setSelectedEdgeInfo] = useState<SelectedEdgeInfo | null>(null);

  // 메모리 기반 상태들
  const [tempRelationships, setTempRelationships] = useState<ExtendedJsonRelationship[]>([]);
  const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
  const [showSaveModal, setShowSaveModal] = useState(false);
  const [isSaving, setIsSaving] = useState(false);
  const [currentDiagramName, setCurrentDiagramName] = useState<string>("");
  const [currentDiagramCategory, setCurrentDiagramCategory] = useState<string>("simple-key");
  const [selectedEdgeForEdit, setSelectedEdgeForEdit] = useState<Edge | null>(null);
  const [showEdgeActions, setShowEdgeActions] = useState(false);
  const [edgeActionPosition] = useState<{ x: number; y: number }>({ x: 0, y: 0 });
  const [editingRelationshipId, setEditingRelationshipId] = useState<string | null>(null);
  const [showRelationshipListModal, setShowRelationshipListModal] = useState(false);
  const [selectedTablePairRelationships, setSelectedTablePairRelationships] = useState<ExtendedJsonRelationship[]>([]);
  const toastShownRef = useRef(false); // eslint-disable-line @typescript-eslint/no-unused-vars

  return {
    // Node & Edge states
    nodes,
    setNodes,
    onNodesChange,
    edges,
    setEdges,
    onEdgesChange,

    // Selection states
    selectedColumns,
    setSelectedColumns,
    selectedNodes,
    setSelectedNodes,

    // Connection states
    pendingConnection,
    setPendingConnection,
    relationships,
    setRelationships,

    // Diagram states
    currentDiagramId,
    setCurrentDiagramId,
    currentDiagramName,
    setCurrentDiagramName,
    currentDiagramCategory,
    setCurrentDiagramCategory,

    // Memory-based states
    tempRelationships,
    setTempRelationships,
    hasUnsavedChanges,
    setHasUnsavedChanges,

    // Modal states
    showSaveModal,
    setShowSaveModal,
    isSaving,
    setIsSaving,
    showRelationshipListModal,
    setShowRelationshipListModal,
    selectedTablePairRelationships,
    setSelectedTablePairRelationships,

    // Edge states
    selectedEdgeInfo,
    setSelectedEdgeInfo,
    selectedEdgeForEdit,
    setSelectedEdgeForEdit,
    showEdgeActions,
    setShowEdgeActions,
    edgeActionPosition,
    editingRelationshipId,
    setEditingRelationshipId,

    // Refs
    toastShownRef,
  };
};
