"use client";

/**
 * UPSERT 액션 노드
 * INSERT와 UPDATE를 결합한 노드
 */

import { memo } from "react";
import { Handle, Position, NodeProps } from "reactflow";
import { Database, RefreshCw } from "lucide-react";
import type { UpsertActionNodeData } from "@/types/node-editor";

export const UpsertActionNode = memo(({ data, selected }: NodeProps<UpsertActionNodeData>) => {
  return (
    <div
      className={`min-w-[250px] rounded-lg border-2 bg-white shadow-md transition-all ${
        selected ? "border-orange-500 shadow-lg" : "border-gray-200"
      }`}
    >
      {/* 헤더 */}
      <div className="flex items-center gap-2 rounded-t-lg bg-purple-600 px-3 py-2 text-white">
        <RefreshCw className="h-4 w-4" />
        <div className="flex-1">
          <div className="text-sm font-semibold">{data.displayName || "UPSERT 액션"}</div>
          <div className="text-xs opacity-80">{data.targetTable}</div>
        </div>
        <Database className="h-4 w-4 opacity-70" />
      </div>

      {/* 본문 */}
      <div className="p-3">
        <div className="mb-2 text-xs font-medium text-gray-500">
          타겟: {data.displayName || data.targetTable}
          {data.targetTable && data.displayName && data.displayName !== data.targetTable && (
            <span className="ml-1 font-mono text-gray-400">({data.targetTable})</span>
          )}
        </div>

        {/* 충돌 키 */}
        {data.conflictKeys && data.conflictKeys.length > 0 && (
          <div className="mb-2">
            <div className="text-xs font-medium text-gray-700">충돌 키:</div>
            <div className="mt-1 flex flex-wrap gap-1">
              {data.conflictKeys.map((key, idx) => (
                <span key={idx} className="rounded bg-purple-100 px-2 py-0.5 text-xs text-purple-700">
                  {data.conflictKeyLabels?.[idx] || key}
                </span>
              ))}
            </div>
          </div>
        )}

        {/* 필드 매핑 */}
        {data.fieldMappings && data.fieldMappings.length > 0 && (
          <div className="mb-2">
            <div className="text-xs font-medium text-gray-700">필드 매핑:</div>
            <div className="mt-1 space-y-1">
              {data.fieldMappings.slice(0, 3).map((mapping, idx) => (
                <div key={idx} className="rounded bg-gray-50 px-2 py-1 text-xs">
                  <span className="text-gray-600">
                    {mapping.sourceFieldLabel || mapping.sourceField || mapping.staticValue || "?"}
                  </span>
                  <span className="mx-1 text-gray-400">→</span>
                  <span className="font-mono text-gray-700">{mapping.targetFieldLabel || mapping.targetField}</span>
                </div>
              ))}
              {data.fieldMappings.length > 3 && (
                <div className="text-xs text-gray-400">... 외 {data.fieldMappings.length - 3}개</div>
              )}
            </div>
          </div>
        )}

        {/* 옵션 */}
        <div className="flex flex-wrap gap-1">
          {data.options?.updateOnConflict && (
            <span className="rounded bg-blue-100 px-2 py-0.5 text-xs text-blue-700">충돌 시 업데이트</span>
          )}
          {data.options?.batchSize && (
            <span className="rounded bg-gray-100 px-2 py-0.5 text-xs text-gray-600">
              배치: {data.options.batchSize}
            </span>
          )}
        </div>
      </div>

      {/* 핸들 */}
      <Handle type="target" position={Position.Left} className="!h-3 !w-3 !bg-purple-500" />
      <Handle type="source" position={Position.Right} className="!h-3 !w-3 !bg-purple-500" />
    </div>
  );
});

UpsertActionNode.displayName = "UpsertActionNode";
