<script setup>
import { Handle, Position } from '@vue-flow/core'

const props = defineProps({
  data: { type: Object, default: () => ({ label: '' }) },
  sourceHandles: { type: Array, default: () => ['output'] },
  hasTarget: { type: Boolean, default: true },
  color: { type: String, default: '#2563EB' }
})
</script>

<template>
  <div class="base-node" :style="{ '--node-color': color }">
    <Handle v-if="hasTarget" type="target" :position="Position.Left" class="handle-target" />

    <!-- 顶边中点 / 底边中点隐藏 handle：仅供自动关联的虚线连接使用 -->
    <Handle type="target" :position="Position.Top" id="top-target" class="handle-auxiliary" />
    <Handle type="target" :position="Position.Bottom" id="bottom-target" class="handle-auxiliary" />

    <div class="node-header">
      <div class="node-icon">
        <slot name="icon" />
      </div>
      <div class="node-title-wrap">
        <span v-if="data?.nodeName" class="node-title-name" :title="data.nodeName">{{ data.nodeName }}</span>
        <span class="node-title-type">{{ data?.label || '节点' }}</span>
      </div>
    </div>

    <div class="node-body" v-if="$slots.body">
      <slot name="body" />
    </div>

    <Handle
      v-for="h in sourceHandles"
      :key="h"
      type="source"
      :position="Position.Right"
      :id="h"
      class="handle-source"
    />
  </div>
</template>

<style scoped>
.base-node {
  background: var(--bg-card, #1e1e2e);
  border: 2px solid var(--node-color);
  border-radius: 10px;
  width: 250px;
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  transition: box-shadow 0.2s;
}
.base-node:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.node-icon {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  background: var(--node-color);
  color: #fff;
  font-size: 13px;
  flex-shrink: 0;
}

.node-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary, #e0e0e0);
}

.node-title-wrap {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  flex: 1;
}

.node-title-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary, #e0e0e0);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.node-title-type {
  font-size: 11px;
  font-weight: 400;
  color: var(--text-tertiary, #888);
  white-space: nowrap;
  flex-shrink: 0;
}

.node-body {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-secondary, #aaa);
}

.handle-target, .handle-source {
  width: 10px !important;
  height: 10px !important;
  background: var(--node-color) !important;
  border: 2px solid #fff !important;
}

/* 辅助 handle：仅供自动关联虚线引用，视觉上完全隐藏 */
.handle-auxiliary {
  width: 1px !important;
  height: 1px !important;
  min-width: 1px !important;
  min-height: 1px !important;
  background: transparent !important;
  border: none !important;
  opacity: 0 !important;
  pointer-events: none !important;
}
</style>
