"use client";

/**
 * 콤팩트 노드 공통 쉘
 * 테마 호환 노드 래퍼
 */

import { memo, ReactNode } from "react";
import { Handle, Position } from "reactflow";

interface CompactNodeShellProps {
  color: string;
  label: string;
  summary?: string;
  icon: ReactNode;
  selected?: boolean;
  children?: ReactNode;
  hasInput?: boolean;
  hasOutput?: boolean;
  inputHandleId?: string;
  outputHandleId?: string;
  customOutputHandles?: boolean;
  customInputHandles?: boolean;
  minWidth?: string;
}

export const CompactNodeShell = memo(
  ({
    color,
    label,
    summary,
    icon,
    selected = false,
    children,
    hasInput = true,
    hasOutput = true,
    inputHandleId,
    outputHandleId,
    customOutputHandles = false,
    customInputHandles = false,
    minWidth = "260px",
  }: CompactNodeShellProps) => {
    return (
      <div
        className={`rounded-lg border bg-background shadow-md transition-all ${
          selected
            ? "border-primary shadow-primary/20 ring-1 ring-primary/30"
            : "border-border hover:border-primary/40"
        }`}
        style={{ minWidth, maxWidth: "320px" }}
      >
        {/* 기본 입력 핸들 */}
        {hasInput && !customInputHandles && (
          <Handle
            type="target"
            position={Position.Left}
            id={inputHandleId}
            className="!h-2.5 !w-2.5 !border-2 !bg-background"
            style={{ borderColor: color }}
          />
        )}

        {/* 컬러바 + 헤더 */}
        <div className="flex items-center gap-2.5 px-3 py-2.5">
          <div
            className="flex h-7 w-7 flex-shrink-0 items-center justify-center rounded-md"
            style={{ backgroundColor: `${color}20` }}
          >
            <div style={{ color }}>{icon}</div>
          </div>
          <div className="min-w-0 flex-1">
            <div className="text-xs font-semibold">{label}</div>
            {summary && (
              <div className="line-clamp-2 text-[10px] leading-relaxed text-muted-foreground">{summary}</div>
            )}
          </div>
        </div>

        {/* 바디 (옵셔널) */}
        {children && (
          <div className="border-t px-3 py-2 text-[10px] text-muted-foreground">
            {children}
          </div>
        )}

        {/* 기본 출력 핸들 */}
        {hasOutput && !customOutputHandles && (
          <Handle
            type="source"
            position={Position.Right}
            id={outputHandleId}
            className="!h-2.5 !w-2.5 !border-2 !bg-background"
            style={{ borderColor: color }}
          />
        )}
      </div>
    );
  },
);

CompactNodeShell.displayName = "CompactNodeShell";
