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

// Define props that Vue Flow passes to custom nodes
const props = defineProps([
  'id',           // Node ID
  'type',         // Node type
  'label',        // Node label
  'selected',     // Selection state
  'data'          // Custom data object
])

// Computed properties for node display
const nodeLabel = computed(() => {
  return props.label || (props.data && props.data.label) || 'Decision Point'
})

const totalPaths = computed(() => {
  return Array.isArray(props.data?.conditions) ? props.data.conditions.length : 0
})

const totalConditions = computed(() => {
  if (!Array.isArray(props.data?.conditions)) return 0
  
  return props.data.conditions.reduce((total, group) => {
    return total + (Array.isArray(group.conditions) ? group.conditions.length : 0)
  }, 0)
})

const conditionSummary = computed(() => {
  if (totalPaths.value === 0) return 'No paths'
  
  const paths = props.data.conditions
    .map(group => group.output || 'Unlabeled')
    .filter(Boolean)
    .join(', ')
    
  return paths || 'Unconfigured paths'
})

const defaultPath = computed(() => {
  return props.data?.defaultPath || 'Default'
})

// Computed for node styling based on colors
const nodeStyle = computed(() => {
  const backgroundColor = props.data?.backgroundColor || '#fff7ed'
  const borderColor = props.data?.borderColor || '#f97316'
  const textColor = props.data?.textColor || '#c2410c'
  
  return {
    '--node-bg-color': backgroundColor,
    '--node-border-color': borderColor,
    '--node-text-color': textColor,
    backgroundColor: backgroundColor,
    borderColor: borderColor,
    color: textColor
  }
})

// Computed for shape class
const shapeClass = computed(() => {
  const shape = props.data?.shape || 'diamond'
  return `shape-${shape}`
})

// Handle node click event
const emit = defineEmits(['node-click'])
const onClick = () => {
  emit('node-click', props.id)
}
</script>

<template>
  <div 
    :class="['custom-node', 'node-gateway', shapeClass, { 'selected': selected }]"
    :style="nodeStyle"
    @click="onClick"
  >
    <!-- Input handles -->
    <Handle
      type="target"
      :position="Position.Top"
      class="handle-gateway-input handle-top"
      :id="id + '-top'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <Handle
      type="target"
      :position="Position.Left"
      class="handle-gateway-input handle-left"
      :id="id + '-left'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <!-- Output handles -->
    <Handle
      type="source"
      :position="Position.Right"
      class="handle-gateway-output handle-right"
      :id="id + '-right'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <Handle
      type="source"
      :position="Position.Bottom"
      class="handle-gateway-output handle-bottom"
      :id="id + '-bottom'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <div class="custom-node-content">
      <div class="material-icons">call_split</div>
      <div class="custom-node-title" :title="nodeLabel">{{ nodeLabel }}</div>
      
      <div class="gateway-details">
        <p class="node-description">{{ data?.description || 'Decision based on conditions' }}</p>
        <div class="node-rule-detail flex items-center justify-between text-xs mt-1">
          <span class="node-rule-detail-label">Paths:</span>
          <span class="node-rule-detail-value ml-1 font-medium text-orange-600">
            {{ totalPaths === 0 ? 'None' : totalPaths }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* Gateway node specific styling - Diamond shape */
.node-gateway {
  width: 120px !important;
  height: 120px !important;
  background: white;
  transform: rotate(45deg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border: 2px solid #f97316;
  position: relative;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.node-gateway.selected {
  border-color: #ff6b6b;
  box-shadow: 0 0 0 2px rgba(255, 107, 107, 0.2);
}

.node-gateway:hover {
  border-color: #ea580c;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.custom-node-content {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  transform: rotate(-45deg);
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px;
  text-align: center;
  z-index: 10;
  overflow: hidden;
  box-sizing: border-box;
}

.material-icons {
  font-size: 16px;
  color: #f97316;
  margin-bottom: 2px;
}

.custom-node-title {
  font-size: 9px;
  font-weight: 700;
  color: #c2410c;
  margin: 0 0 4px 0;
  text-align: center;
  width: 70px;
  white-space: normal;
  overflow: hidden;
  line-height: 1.0;
  max-height: 18px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}

.gateway-details {
  width: 70px;
  text-align: center;
  margin-top: 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.node-description {
  text-align: center;
  margin-bottom: 2px;
  font-size: 8px;
  line-height: 1.1;
  overflow: hidden;
  max-width: 80px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: #666;
}

.node-rule-detail {
  display: flex;
  font-size: 8px;
  color: #666;
  align-items: center;
  justify-content: center;
  width: 80%;
  margin-bottom: 2px;
}

.node-rule-detail-label {
  font-weight: 500;
  margin-right: 4px;
}

.node-rule-detail-value {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 8px;
  max-width: 50px;
  text-align: center;
  font-weight: 600;
  color: #c2410c;
}

/* Handle styles for gateway - adjusted for diamond shape */
.handle-top, .handle-bottom, .handle-left, .handle-right {
  width: 12px !important;
  height: 12px !important;
  border-radius: 50% !important;
  background: #fff !important;
  border: 2px solid #f97316 !important;
  opacity: 0;
  transition: all 0.2s ease;
  cursor: crosshair;
  z-index: 100 !important;
  position: absolute !important;
}

.handle-top {
  top: -6px !important;
  left: 50% !important;
  transform: translateX(-50%) rotate(-45deg) !important;
}

.handle-bottom {
  bottom: -6px !important;
  left: 50% !important;
  transform: translateX(-50%) rotate(-45deg) !important;
}

.handle-left {
  left: -6px !important;
  top: 50% !important;
  transform: translateY(-50%) rotate(-45deg) !important;
}

.handle-right {
  right: -6px !important;
  top: 50% !important;
  transform: translateY(-50%) rotate(-45deg) !important;
}

/* Show handles on hover */
.node-gateway:hover .handle-top,
.node-gateway:hover .handle-bottom,
.node-gateway:hover .handle-left,
.node-gateway:hover .handle-right {
  opacity: 1 !important;
}

/* Handle hover effects for gateway */
.handle-top:hover {
  transform: translateX(-50%) rotate(-45deg) scale(1.1) !important;
  background: #f97316 !important;
}

.handle-bottom:hover {
  transform: translateX(-50%) rotate(-45deg) scale(1.1) !important;
  background: #f97316 !important;
}

.handle-left:hover {
  transform: translateY(-50%) rotate(-45deg) scale(1.1) !important;
  background: #f97316 !important;
}

.handle-right:hover {
  transform: translateY(-50%) rotate(-45deg) scale(1.1) !important;
  background: #f97316 !important;
}
</style> 