"use client";

import React from "react";
import { TableNodeData } from "@/types/dataflowTypes";

interface SelectedTablesPanelProps {
  selectedNodes: string[];
  nodes: Array<{
    id: string;
    data: TableNodeData;
  }>;
  onClose: () => void;
  onOpenConnectionModal: () => void;
  onClear: () => void;
  canCreateConnection: boolean;
}

export const SelectedTablesPanel: React.FC<SelectedTablesPanelProps> = ({
  selectedNodes,
  nodes,
  onClose,
  onOpenConnectionModal,
  onClear,
  canCreateConnection,
}) => {
  return (
    <div className="pointer-events-auto absolute top-4 left-4 z-40 w-80 rounded-lg border border-border bg-background shadow-md">
      {/* 헤더 */}
      <div className="flex items-center justify-between rounded-t-lg border-b border-border bg-muted p-3">
        <div className="flex items-center gap-2">
          <div className="flex h-6 w-6 items-center justify-center rounded-full bg-primary/20">
            <span className="text-sm text-primary">📋</span>
          </div>
          <div>
            <div className="text-sm font-semibold text-foreground">선택된 테이블</div>
            <div className="text-xs text-muted-foreground">
              {selectedNodes.length === 1
                ? "FROM 테이블 선택됨"
                : selectedNodes.length === 2
                  ? "FROM → TO 연결 준비"
                  : `${selectedNodes.length}개 테이블`}
            </div>
          </div>
        </div>
        <button
          onClick={onClose}
          className="flex h-5 w-5 items-center justify-center rounded-full text-muted-foreground/70 hover:bg-muted hover:text-muted-foreground"
        >
          ✕
        </button>
      </div>

      {/* 컨텐츠 */}
      <div className="max-h-80 overflow-y-auto p-3">
        <div className="space-y-3">
          {selectedNodes.map((nodeId, index) => {
            const node = nodes.find((n) => n.id === nodeId);
            if (!node) return null;

            const { tableName, displayName } = node.data.table;
            return (
              <div key={`selected-${nodeId}-${index}`}>
                {/* 테이블 정보 */}
                <div
                  className={`rounded-lg p-2 ${
                    index === 0
                      ? "border-l-4 border-success/60 bg-success/10"
                      : index === 1
                        ? "border-l-4 border-primary/60 bg-accent"
                        : "bg-muted"
                  }`}
                >
                  <div className="mb-1 flex items-center justify-between">
                    <div
                      className={`text-xs font-medium ${
                        index === 0 ? "text-success" : index === 1 ? "text-primary" : "text-foreground"
                      }`}
                    >
                      {displayName}
                    </div>
                    {selectedNodes.length === 2 && (
                      <div
                        className={`rounded-full px-2 py-0.5 text-xs font-bold ${
                          index === 0 ? "bg-success/20 text-success" : "bg-primary/20 text-primary"
                        }`}
                      >
                        {index === 0 ? "FROM" : "TO"}
                      </div>
                    )}
                  </div>
                  <div className="text-xs text-muted-foreground">{tableName}</div>
                </div>

                {/* 연결 화살표 (마지막이 아닌 경우) */}
                {index < selectedNodes.length - 1 && (
                  <div className="flex justify-center py-1">
                    <div className="text-muted-foreground/70">→</div>
                  </div>
                )}
              </div>
            );
          })}
        </div>
      </div>

      {/* 액션 버튼 */}
      <div className="flex gap-2 border-t border-border p-3">
        <button
          onClick={onOpenConnectionModal}
          disabled={!canCreateConnection}
          className={`flex flex-1 items-center justify-center gap-1 rounded-md px-3 py-2 text-xs font-medium transition-colors ${
            canCreateConnection
              ? "bg-primary text-primary-foreground hover:bg-primary/90"
              : "cursor-not-allowed bg-muted text-muted-foreground"
          }`}
        >
          <span>연결 설정</span>
        </button>
        <button
          onClick={onClear}
          className="flex flex-1 items-center justify-center gap-1 rounded-md border border-border bg-background px-3 py-2 text-xs font-medium text-muted-foreground hover:bg-muted"
        >
          <span>초기화</span>
        </button>
      </div>
    </div>
  );
};
