<script setup>
import { Handle, Position } from '@vue-flow/core'
import { useProcessBuilderStore } from '~/stores/processBuilder'
import ValidationIndicator from '../ValidationIndicator.vue'
import { inject } from 'vue'

// 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
])

// Validation functionality - get from store
const processStore = useProcessBuilderStore()

// Inject child node event handlers from ProcessFlowCanvas
const childNodeEventHandlers = inject('childNodeEventHandlers', {
  onChildNodeClick: () => console.warn('Child node event handler not available'),
  onPageWrapperHeaderClick: () => console.warn('Header click event handler not available'),
  onChildAdd: () => console.warn('Child add event handler not available'),
  onChildDrop: () => console.warn('Child drop event handler not available'),
})

// Get validation issues for this node
const validationIssues = computed(() => {
  return processStore.getNodeValidation(props.id)
})

// Reactive state
const isDragOver = ref(false)
const selectedChildId = ref(null)

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

const visualChildNodes = computed(() => {
  return props.data?.childNodes || []
})

const layoutType = computed(() => {
  return props.data?.layout || 'grid'
})

const hasChildNodes = computed(() => {
  return visualChildNodes.value.length > 0
})

// Node styling following standard pattern
const nodeStyle = computed(() => {
  const backgroundColor = props.data?.backgroundColor || '#f8fafc'
  const borderColor = props.data?.borderColor || '#475569'
  const textColor = props.data?.textColor || '#334155'
  
  return {
    '--node-bg-color': backgroundColor,
    '--node-border-color': borderColor,  
    '--node-text-color': textColor,
    backgroundColor: backgroundColor,
    borderColor: borderColor,
    color: textColor
  }
})

// Container sizing
const containerSize = computed(() => {
  // Base size following standard node pattern but larger for container
  const baseWidth = 220
  const baseHeight = 120
  
  // Additional space for child nodes
  const childCount = visualChildNodes.value.length
  const extraWidth = Math.min(childCount * 40, 200) // Max 200px extra
  const extraHeight = Math.ceil(childCount / 3) * 30 // Height based on rows
  
  return {
    width: `${baseWidth + extraWidth}px`,
    minHeight: `${baseHeight + extraHeight}px`
  }
})

// Child container classes and styles
const childContainerClasses = computed(() => {
  const layout = layoutType.value
  const classes = ['child-nodes-grid']
  
  if (layout === 'grid') {
    classes.push('grid-layout')
  } else if (layout === 'flex') {
    classes.push('flex-layout')
  } else {
    classes.push('stacked-layout')
  }
  
  return classes
})

const childContainerStyle = computed(() => {
  const layout = layoutType.value
  const gap = props.data?.gap || '4px'
  const childCount = visualChildNodes.value.length
  
  if (layout === 'grid') {
    const columns = Math.min(props.data?.columns || 2, Math.max(childCount, 1))
    return {
      display: 'grid',
      gridTemplateColumns: `repeat(${columns}, 1fr)`,
      gap: gap,
      alignItems: 'start'
    }
  } else if (layout === 'flex') {
    return {
      display: 'flex',
      flexWrap: 'wrap',
      gap: gap,
      alignItems: 'center',
      justifyContent: 'flex-start'
    }
  } else {
    return {
      display: 'flex',
      flexDirection: 'column',
      gap: gap,
      alignItems: 'stretch'
    }
  }
})

// Layout-specific styling
const layoutIndicatorClass = computed(() => {
  const layout = layoutType.value
  return `layout-${layout}`
})

const layoutIndicatorStyle = computed(() => {
  const layout = layoutType.value
  if (layout === 'grid') {
    return {
      background: 'linear-gradient(90deg, #dbeafe 25%, transparent 25%), linear-gradient(#dbeafe 25%, transparent 25%)',
      backgroundSize: '8px 8px'
    }
  } else if (layout === 'flex') {
    return {
      background: 'linear-gradient(90deg, #dcfce7 0%, transparent 50%)',
      borderLeft: '3px solid #22c55e'
    }
  } else {
    return {
      background: 'linear-gradient(0deg, #fef3c7 0%, transparent 50%)',
      borderTop: '3px solid #f59e0b'
    }
  }
})

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

// Get child node styling
const getChildNodeStyle = (childNode) => {
  const styles = {}
  
  if (layoutType.value === 'grid' && childNode.position) {
    styles.gridRow = childNode.position.row || 'auto'
    styles.gridColumn = childNode.position.col || 'auto'
  }
  
  return styles
}

// Get child node type icon
const getChildNodeIcon = (type) => {
  const iconMap = {
    form: 'material-symbols:description-outline',
    html: 'material-symbols:code',
    table: 'material-symbols:table'
  }
  return iconMap[type] || 'material-symbols:extension'
}

// Get child node type display name
const getChildNodeTypeName = (type) => {
  const nameMap = {
    form: 'Form',
    html: 'HTML',
    table: 'Table'
  }
  return nameMap[type] || 'Unknown'
}

// Layout display helpers
const getLayoutDisplayName = () => {
  const nameMap = {
    grid: 'Grid',
    flex: 'Side by Side',
    stacked: 'Stacked'
  }
  return nameMap[layoutType.value] || 'Grid'
}

const getLayoutIcon = () => {
  const iconMap = {
    grid: 'material-symbols:grid-view',
    flex: 'material-symbols:view-column',
    stacked: 'material-symbols:view-agenda'
  }
  return iconMap[layoutType.value] || 'material-symbols:grid-view'
}

const getLayoutColorClass = () => {
  const colorMap = {
    grid: 'text-blue-600',
    flex: 'text-green-600',
    stacked: 'text-amber-600'
  }
  return colorMap[layoutType.value] || 'text-blue-600'
}

// Event handlers
const onClick = () => {
  // Handle regular node click (not used for PageWrapper)
  console.log('PageWrapper node clicked (general):', props.id)
}

const onHeaderClick = () => {
  childNodeEventHandlers.onPageWrapperHeaderClick(props.id)
}

const onChildClick = (childNode) => {
  selectedChildId.value = childNode.id
  childNodeEventHandlers.onChildNodeClick({
    parentId: props.id,
    childNode: childNode
  })
}

const onAddChild = () => {
  childNodeEventHandlers.onChildAdd(props.id)
}

// Drag and drop handlers
const onDragOver = (event) => {
  event.preventDefault()
  event.stopPropagation()
  isDragOver.value = true
  event.dataTransfer.dropEffect = 'copy'
}

const onDragLeave = (event) => {
  event.preventDefault()
  event.stopPropagation()
  isDragOver.value = false
}

const onDrop = (event) => {
  event.preventDefault()
  event.stopPropagation()
  isDragOver.value = false
  
  try {
    const dragData = JSON.parse(event.dataTransfer.getData('text/plain'))
    
    // Check if it's a valid child node type
    if (['form', 'html', 'table'].includes(dragData.type)) {
      childNodeEventHandlers.onChildDrop({
        parentId: props.id,
        childData: dragData,
        dropPosition: {
          x: event.offsetX,
          y: event.offsetY
        }  
      })
    }
  } catch (error) {
    console.warn('Invalid drag data:', error)
  }
}

const onChildDragStart = (event) => {
  // Prevent child nodes from being dragged out for now
  event.preventDefault()
}
</script>

<template>
  <div 
    :class="['custom-node', 'node-page-wrapper', shapeClass, { 'selected': selected, 'drop-target': isDragOver }]"
    :style="{ ...nodeStyle, ...containerSize }"
    @click="onClick"
  >
    <!-- Input handles -->
    <Handle
      type="target"
      :position="Position.Top"
      class="handle-top"
      :id="id + '-top'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <Handle
      type="target"
      :position="Position.Left"
      class="handle-left"
      :id="id + '-left'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <!-- Output handles -->
    <Handle
      type="source"
      :position="Position.Right"
      class="handle-right"
      :id="id + '-right'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <Handle
      type="source"
      :position="Position.Bottom"
      class="handle-bottom"
      :id="id + '-bottom'"
      :style="{ zIndex: 1000 }"
      :isConnectable="true"
      :isValidConnection="() => true"
    />
    
    <!-- Validation Indicator -->
    <ValidationIndicator 
      :node-id="id" 
      :validation-issues="validationIssues"
    />
    
    <!-- Standard Node Content Structure -->
    <div class="custom-node-content" @click.stop="onHeaderClick">
      <!-- Node Header -->
      <div class="flex items-center mb-1">
        <div class="custom-node-icon">
          <i class="material-icons text-slate-600">web_asset</i>
        </div>
        <div class="custom-node-label" :title="nodeLabel">{{ nodeLabel }}</div>
      </div>
      
      <!-- Node Details -->
      <div class="node-details">
        <p class="node-description">{{ data?.description || 'Page container with components' }}</p>
        <div class="node-rule-detail flex items-center justify-between text-xs mt-1">
          <span class="node-rule-detail-label">Layout:</span>
          <span class="node-rule-detail-value ml-1 font-medium" :class="getLayoutColorClass()">
            {{ getLayoutDisplayName() }}
          </span>
        </div>
        <div class="node-rule-detail flex items-center justify-between text-xs mt-1">
          <span class="node-rule-detail-label">Components:</span>
          <span class="node-rule-detail-value ml-1 font-medium text-slate-600">
            {{ visualChildNodes.length }}
          </span>
        </div>
      </div>
    </div>
    
    <!-- Child Components Area (Always visible for drag-and-drop) -->
    <div 
      class="child-components-area" 
      :class="[layoutIndicatorClass, { 'drag-over': isDragOver }]"
      :style="layoutIndicatorStyle"
    >
      <!-- Layout Type Indicator -->
      <div class="layout-indicator">
        <Icon :name="getLayoutIcon()" class="w-3 h-3" />
        <span class="layout-name">{{ getLayoutDisplayName() }}</span>
      </div>
      
      <!-- Child Components Grid -->
      <div 
        v-if="hasChildNodes" 
        class="child-components-grid" 
        :class="childContainerClasses" 
        :style="childContainerStyle"
        @dragover="onDragOver"
        @dragleave="onDragLeave"
        @drop="onDrop"
      >
        <div
          v-for="childNode in visualChildNodes"
          :key="childNode.id"
          :class="['child-component', layoutType, { 'selected': childNode.id === selectedChildId }]"
          @click.stop="onChildClick(childNode)"
          @dragstart="onChildDragStart"
          draggable="true"
          :title="`${getChildNodeTypeName(childNode.type)}: ${childNode.label || 'Unnamed'}`"
        >
          <Icon :name="getChildNodeIcon(childNode.type)" class="w-3 h-3" />
          <span class="child-label">{{ childNode.label || getChildNodeTypeName(childNode.type) }}</span>
          <Icon v-if="childNode.conditionalLogic?.enabled" name="material-symbols:rule" class="w-2 h-2 text-orange-500" />
        </div>
      </div>
      
      <!-- Drop Zone (Always visible for drag and drop) -->
      <div 
        class="drop-zone-compact"
        :class="{ 'drag-over': isDragOver, 'has-children': hasChildNodes }"
        @dragover="onDragOver"
        @dragleave="onDragLeave"
        @drop="onDrop"
      >
        <Icon name="material-symbols:add" class="w-4 h-4 text-gray-300" />
        <span class="text-xs text-gray-400">
          {{ hasChildNodes ? 'Drop more components' : 'Drag Form, HTML, or Table nodes here' }}
        </span>
      </div>
      
      <!-- Add Child Button -->
      <div v-if="hasChildNodes" class="add-child-button" @click.stop="onAddChild">
        <Icon name="material-symbols:add" class="w-3 h-3 text-gray-400" />
      </div>
    </div>
  </div>
</template>

<style scoped>
/* PageWrapper Node - Following standard node pattern */
.node-page-wrapper {
  background: white;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  padding: 0;
  border: 1px solid #ddd;
  border-left: 4px solid #475569;
  position: relative;
  font-size: 12px;
  transition: all 0.2s;
  cursor: pointer;
}

.node-page-wrapper:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

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

.node-page-wrapper.drop-target {
  border-left-color: #10b981;
  background-color: #f0fdf4;
  border-color: #10b981;
}

/* Standard Node Content Structure */
.custom-node-content {
  padding: 8px;
  position: relative;
  z-index: 2;
  cursor: pointer;
}

.custom-node-icon {
  margin-right: 6px;
  display: inline-flex;
  align-items: center;
}

.custom-node-icon .material-icons {
  font-size: 16px;
}

.custom-node-label {
  font-weight: 500;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.node-details {
  margin-top: 4px;
}

.node-description {
  margin-bottom: 2px;
  color: #666;
  white-space: normal;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 10px;
}

.node-rule-detail {
  display: flex;
  font-size: 10px;
  color: #666;
  align-items: center;
  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;
  max-width: 100px;
}

/* Child Components Area */
.child-components-area {
  border-top: 1px solid #f3f4f6;
  padding: 6px 8px 8px 8px;
  background: #fafafa;
  min-height: 50px;
  position: relative;
  transition: all 0.2s ease;
}

.child-components-area.drag-over {
  background: #f0fdf4;
  border-top-color: #10b981;
}

/* Layout-specific area styling */
.child-components-area.layout-grid {
  background: linear-gradient(90deg, #dbeafe 25%, transparent 25%), 
              linear-gradient(#dbeafe 25%, transparent 25%);
  background-size: 6px 6px;
  background-position: 0 0, 0 0;
}

.child-components-area.layout-flex {
  background: linear-gradient(90deg, #dcfce7 0%, transparent 100%);
  border-left: 2px solid #22c55e;
}

.child-components-area.layout-stacked {
  background: linear-gradient(180deg, #fef3c7 0%, transparent 100%);
  border-top: 2px solid #f59e0b;
}

/* Layout Indicator */
.layout-indicator {
  position: absolute;
  top: 3px;
  left: 4px;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px 4px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  font-size: 8px;
  font-weight: 500;
  color: #64748b;
  backdrop-filter: blur(2px);
  z-index: 10;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.layout-name {
  font-size: 7px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.child-components-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

.child-components-grid.grid-layout {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
  gap: 4px;
}

.child-components-grid.flex-layout {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.child-components-grid.stacked-layout {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* Child Components */
.child-component {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 3px;
  padding: 3px 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 9px;
  min-width: 50px;
  max-width: 100px;
  user-select: none;
  position: relative;
}

.child-component:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  transform: translateY(-1px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.child-component.selected {
  background: #dbeafe;
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

.child-component:active {
  transform: scale(0.98);
  opacity: 0.8;
}

/* Layout-specific child component styling */
.child-component.grid {
  border-left: 3px solid #3b82f6;
  background: linear-gradient(135deg, #dbeafe 0%, #ffffff 100%);
}

.child-component.flex {
  border-left: 3px solid #22c55e;
  background: linear-gradient(135deg, #dcfce7 0%, #ffffff 100%);
  flex: 1;
  min-width: 60px;
}

.child-component.stacked {
  border-top: 3px solid #f59e0b;
  background: linear-gradient(135deg, #fef3c7 0%, #ffffff 100%);
  width: 100%;
  max-width: none;
}

/* Drag feedback */
.child-component[draggable="true"]:hover {
  cursor: grab;
}

.child-component[draggable="true"]:active {
  cursor: grabbing;
}

.child-label {
  font-weight: 500;
  color: #334155;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

/* Drop Zone */
.drop-zone-compact {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 8px;
  border: 1px dashed #d1d5db;
  border-radius: 4px;
  background: #f9fafb;
  transition: all 0.2s ease;
  min-height: 28px;
  margin-top: 6px;
  cursor: pointer;
  clear: both;
}

.drop-zone-compact:hover {
  border-color: #9ca3af;
  background: #f3f4f6;
}

.drop-zone-compact.drag-over {
  border-color: #10b981;
  background: #f0fdf4;
  color: #059669;
  border-style: solid;
}

.drop-zone-compact.has-children {
  margin-top: 6px;
  padding: 6px;
  min-height: 28px;
}

/* Add Child Button */
.add-child-button {
  position: absolute;
  top: 3px;
  right: 4px;
  width: 18px;
  height: 18px;
  border: 1px dashed #9ca3af;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.95);
  transition: all 0.15s ease;
  z-index: 10;
  backdrop-filter: blur(2px);
}

.add-child-button:hover {
  border-color: #3b82f6;
  background: #eff6ff;
  color: #3b82f6;
}


/* Handle styles - Following standard pattern */
.handle-top, .handle-bottom, .handle-left, .handle-right {
  width: 12px !important;
  height: 12px !important;
  border-radius: 50% !important;
  background: #fff !important;
  border: 2px solid #666 !important;
  opacity: 0;
  transition: all 0.2s ease;
  cursor: crosshair;
  z-index: 100 !important;
}

.node-page-wrapper:hover .handle-top,
.node-page-wrapper:hover .handle-bottom,
.node-page-wrapper:hover .handle-left,
.node-page-wrapper:hover .handle-right {
  opacity: 1;
}

.handle-top:hover, .handle-bottom:hover, 
.handle-left:hover, .handle-right:hover {
  border-color: #3b82f6 !important;
  background: #eff6ff !important;
}

/* Shape variations */
.shape-rectangle {
  border-radius: 4px;
}

.shape-diamond {
  border-radius: 0;
  transform: rotate(45deg);
}

.shape-circle {
  border-radius: 50%;
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .child-components-grid.grid-layout {
    grid-template-columns: 1fr !important;
  }
  
  .child-components-grid.flex-layout {
    flex-direction: column;
  }
  
  .child-component {
    min-width: 80px;
    max-width: none;
  }
}
</style>