"use client";

import React from "react";
import { Handle, Position } from "@xyflow/react";

interface TableColumn {
  columnName: string;
  name?: string; // 호환성을 위해 유지
  columnLabel?: string;
  displayName?: string;
  dataType?: string;
  type?: string; // 호환성을 위해 유지
  description?: string;
}

interface Table {
  tableName: string;
  displayName: string;
  description: string;
  columns: TableColumn[];
}

interface TableNodeData {
  table: Table;
  onColumnClick: (tableName: string, columnName: string) => void;
  onScrollAreaEnter?: () => void;
  onScrollAreaLeave?: () => void;
  selectedColumns?: string[]; // 선택된 컬럼 목록
}

export const TableNode: React.FC<{ data: TableNodeData }> = ({ data }) => {
  const { table, onColumnClick, onScrollAreaEnter, onScrollAreaLeave, selectedColumns = [] } = data;

  return (
    <div className="relative flex min-w-[280px] cursor-pointer flex-col overflow-hidden rounded-lg border-2 border-input bg-white shadow-lg transition-all hover:shadow-xl">
      {/* React Flow Handles - 숨김 처리 */}
      <Handle type="target" position={Position.Left} id="left" className="!invisible !h-1 !w-1" />
      <Handle type="source" position={Position.Right} id="right" className="!invisible !h-1 !w-1" />

      {/* 테이블 헤더 - 통일된 디자인 */}
      <div className="bg-primary p-3 text-white">
        <h3 className="truncate text-sm font-semibold">{table.displayName}</h3>
        {table.description && <p className="mt-1 truncate text-xs opacity-75">{table.description}</p>}
      </div>

      {/* 컬럼 목록 */}
      <div className="flex-1 overflow-hidden p-2" onMouseEnter={onScrollAreaEnter} onMouseLeave={onScrollAreaLeave}>
        <div className="space-y-1">
          {table.columns.map((column) => {
            const columnKey = column.columnName || column.name || "";
            const columnDisplayName = column.displayName || column.columnLabel || column.name || column.columnName;
            const columnType = column.dataType || column.type || "";
            const isSelected = selectedColumns.includes(columnKey);

            return (
              <div
                key={columnKey}
                className={`relative cursor-pointer rounded px-2 py-1 text-xs transition-colors ${
                  isSelected ? "bg-primary/20 text-primary ring-2 ring-ring" : "text-foreground hover:bg-muted"
                }`}
                onClick={() => onColumnClick(table.tableName, columnKey)}
              >
                {/* 핸들 제거됨 - 컬럼 클릭으로만 연결 생성 */}

                <div className="flex items-center justify-between">
                  <span className="font-mono font-medium">{columnDisplayName}</span>
                  <span className="text-muted-foreground">{columnType}</span>
                </div>
                {column.description && <div className="mt-0.5 text-muted-foreground">{column.description}</div>}
              </div>
            );
          })}
        </div>
      </div>
    </div>
  );
};
