<template>
  <div :class="['custom-node', `status-${data.status || 'normal'}`, { 'node-selected': selected }]" :style="nodeStyle">
    <Handle type="target" :position="Position.Top" class="handle handle-top" />

    <div class="node-content">
      <div class="icon-wrapper" :style="iconWrapperStyle">
        <component :is="iconComponent" :size="32" :stroke-width="1.5" />
      </div>

      <div class="node-info">
        <div class="node-label" :title="data.label">{{ data.label }}</div>
        <div v-if="data.ip" class="node-ip" :title="data.ip">{{ data.ip }}</div>
      </div>

      <div v-if="data.alerts && data.alerts > 0" class="alert-badge">
        <span class="alert-count">{{ data.alerts }}</span>
      </div>
    </div>

    <Handle type="source" :position="Position.Bottom" class="handle handle-bottom" />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Handle, Position, type NodeProps } from '@vue-flow/core'
import { DEVICE_TYPE_CONFIG } from '../config'
import type { NodeData } from '../types'
import { IconServer } from '@tabler/icons-vue'

const props = defineProps<NodeProps<NodeData>>()

const data = computed(() => props.data)
const selected = computed(() => props.selected)

const config = computed(() => DEVICE_TYPE_CONFIG[data.value.type] || DEVICE_TYPE_CONFIG.server)
const iconComponent = computed(() => config.value.icon)

const statusColors: Record<string, { border: string; bg: string; glow: string }> = {
  normal: { border: '#E5E7EB', bg: '#FFFFFF', glow: 'rgba(59, 130, 246, 0.1)' },
  warning: { border: '#FCD34D', bg: '#FFFBEB', glow: 'rgba(251, 191, 36, 0.3)' },
  error: { border: '#EF4444', bg: '#FEF2F2', glow: 'rgba(239, 68, 68, 0.4)' },
}

const statusConfig = computed(() => statusColors[data.value.status || 'normal'])

const nodeStyle = computed(() => ({
  '--border-color': selected.value ? '#3B82F6' : statusConfig.value.border,
  '--bg-color': statusConfig.value.bg,
  '--node-color': config.value.color,
  '--glow-color': selected.value ? 'rgba(59, 130, 246, 0.3)' : statusConfig.value.glow,
}))

const iconWrapperStyle = computed(() => ({
  background: `linear-gradient(135deg, ${config.value.color}15 0%, ${config.value.color}25 100%)`,
  color: config.value.color,
  boxShadow: `0 2px 8px ${config.value.color}30`,
}))
</script>

<style scoped lang="less">
.custom-node {
  background: var(--bg-color);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  padding: 0;
  min-width: 160px;
  box-shadow:
    0 4px 12px rgba(0, 0, 0, 0.08),
    0 0 0 1px rgba(0, 0, 0, 0.02);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: visible;

  &::before {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 16px;
    background: var(--glow-color);
    opacity: 0;
    transition: opacity 0.3s;
    z-index: -1;
  }

  &:hover {
    box-shadow:
      0 8px 24px rgba(0, 0, 0, 0.12),
      0 0 0 1px rgba(0, 0, 0, 0.04);
    transform: translateY(-2px);

    &::before {
      opacity: 1;
    }
  }

  &.node-selected {
    border-color: #3b82f6;
    box-shadow:
      0 0 0 3px rgba(59, 130, 246, 0.1),
      0 8px 24px rgba(59, 130, 246, 0.15);

    &::before {
      opacity: 1;
    }
  }

  &.status-warning {
    border-left: 4px solid #f59e0b;
  }

  &.status-error {
    border-left: 4px solid #ef4444;
    animation: pulse 2s ease-in-out infinite;
  }

  .node-content {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    position: relative;
  }

  .icon-wrapper {
    width: 52px;
    height: 52px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.3s;
  }

  .node-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .node-label {
    font-weight: 600;
    font-size: 14px;
    color: var(--color-text-1);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.4;
  }

  .node-ip {
    font-size: 12px;
    color: var(--color-text-3);
    font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', monospace;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.4;
  }

  .alert-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    min-width: 20px;
    height: 20px;
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(239, 68, 68, 0.4);
    animation: bounce 1s ease-in-out infinite;

    .alert-count {
      font-size: 11px;
      font-weight: 700;
      color: #fff;
      padding: 0 6px;
    }
  }
}

.handle {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  width: 12px;
  height: 12px;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
  transition: all 0.2s;
  z-index: 10;

  &:hover {
    transform: scale(1.3);
    box-shadow:
      0 0 0 4px rgba(59, 130, 246, 0.2),
      0 4px 8px rgba(59, 130, 246, 0.4);
  }

  &.handle-top {
    top: -6px;
    left: 50%;
    transform: translateX(-50%);

    &:hover {
      transform: translateX(-50%) scale(1.3);
    }
  }

  &.handle-bottom {
    bottom: -6px;
    left: 50%;
    transform: translateX(-50%);

    &:hover {
      transform: translateX(-50%) scale(1.3);
    }
  }
}

@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);
  }
}

@keyframes bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-4px);
  }
}
</style>
