<script setup>
import { ref, watch, computed } from 'vue';
import { useProcessBuilderStore } from '~/stores/processBuilder';

const props = defineProps({
  nodeData: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['update']);
const processStore = useProcessBuilderStore();

const localNodeData = ref({});
const selectedProcessDetails = ref(null);
const isLoadingDetails = ref(false);
const searchQuery = ref('');

// Watch for changes to nodeData prop
watch(() => props.nodeData, (value) => {
  localNodeData.value = JSON.parse(JSON.stringify(value));
  if (localNodeData.value.subprocessId) {
    fetchProcessDetails(localNodeData.value.subprocessId);
  }
}, { deep: true, immediate: true });

// Fetch the list of all processes for the selection list
const { data: processes, pending, error } = useFetch('/api/process', {
  lazy: true,
  server: false,
  transform: (response) => {
    if (!response || !response.data || !Array.isArray(response.data.processes)) return [];
    const currentProcessId = processStore.currentProcess?.id;
    return response.data.processes
      .filter(p => p.processID !== currentProcessId)
      .map(p => ({
        label: p.processName,
        value: p.processID,
        description: p.processDescription,
        version: p.processVersion,
        status: p.processStatus,
        modifiedDate: p.processModifiedDate
    }));
  }
});

const filteredProcesses = computed(() => {
  if (!processes.value) return [];
  if (!searchQuery.value) return processes.value;
  return processes.value.filter(p => 
    p.label.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
    (p.description && p.description.toLowerCase().includes(searchQuery.value.toLowerCase()))
  );
});

// Fetch detailed information for a single selected process
const fetchProcessDetails = async (processId) => {
  if (!processId) {
    selectedProcessDetails.value = null;
    return;
  }
  isLoadingDetails.value = true;
  try {
    const { data } = await useFetch(`/api/process/${processId}`);
    if (data.value && data.value.success) {
      selectedProcessDetails.value = data.value.process;
    } else {
      selectedProcessDetails.value = null;
    }
  } catch (e) {
    selectedProcessDetails.value = null;
  } finally {
    isLoadingDetails.value = false;
  }
};

const onSelectionChange = (processId) => {
  localNodeData.value.subprocessId = processId;
  const selected = processes.value.find(p => p.value === processId);
  
  if (selected) {
    localNodeData.value.subprocessName = selected.label;
    localNodeData.value.label = selected.label;
    localNodeData.value.description = `Sub-process: ${selected.label}`;
    fetchProcessDetails(processId);
  } else {
    // This case may not be hit if selection is cleared differently, but good for safety
    localNodeData.value.subprocessId = null;
    localNodeData.value.subprocessName = '';
    localNodeData.value.label = 'Sub Process';
    localNodeData.value.description = 'Executes another process';
    selectedProcessDetails.value = null;
  }
  emit('update', localNodeData.value);
};

// Computed property for content summary
const contentSummary = computed(() => {
    if (!selectedProcessDetails.value) return null;
    const { processDefinition, processVariables } = selectedProcessDetails.value;
    const nodeCount = processDefinition?.nodes?.length || 0;
    const edgeCount = processDefinition?.edges?.length || 0;
    const variableCount = processVariables ? Object.keys(processVariables).length : 0;
    return { nodes: nodeCount, edges: edgeCount, variables: variableCount };
});

// Helper functions
const formatDate = (dateString) => dateString ? new Date(dateString).toLocaleString() : 'N/A';
const getNodeIcon = (type) => ({
    'start': 'heroicons:play-circle', 'end': 'heroicons:stop-circle', 'task': 'heroicons:rectangle-stack',
    'form': 'heroicons:document-text', 'gateway': 'heroicons:arrows-pointing-out', 'script': 'heroicons:code-bracket',
    'api': 'heroicons:cloud-arrow-down', 'notification': 'heroicons:bell', 'business-rule': 'heroicons:document-check',
    'subprocess': 'heroicons:flow-chart'
}[type] || 'heroicons:rectangle-stack');
</script>

<template>
  <div class="flex h-[65vh]">
    <!-- Left Panel: Process List -->
    <div class="w-1/3 border-r border-gray-200 bg-gray-50 flex flex-col">
      <div class="p-4 border-b border-gray-200">
        <h3 class="font-medium text-gray-900">Available Processes</h3>
        <div class="relative mt-2">
          <input
            type="text"
            v-model="searchQuery"
            placeholder="Search processes..."
            class="w-full px-3 py-2 pl-9 bg-white border border-gray-300 rounded text-sm focus:outline-none focus:ring-1 focus:ring-teal-500"
          />
          <Icon name="heroicons:magnifying-glass" class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
        </div>
      </div>
      <div v-if="pending" class="flex-1 flex items-center justify-center text-gray-500">
          <Icon name="material-symbols:progress-activity" class="w-5 h-5 animate-spin mr-2" />
          <span>Loading...</span>
      </div>
      <div v-else-if="error" class="p-4 text-red-600">Error loading processes.</div>
      <div v-else class="flex-1 overflow-y-auto">
        <div
          v-for="process in filteredProcesses"
          :key="process.value"
          @click="onSelectionChange(process.value)"
          class="p-4 border-b border-gray-200 cursor-pointer hover:bg-gray-100"
          :class="{ 'bg-teal-50 border-l-4 border-teal-500': localNodeData.subprocessId === process.value }"
        >
          <div class="font-medium text-gray-900">{{ process.label }}</div>
          <p class="text-xs text-gray-600 mt-1 truncate">{{ process.description || 'No description' }}</p>
        </div>
      </div>
    </div>

    <!-- Right Panel: Details Preview -->
    <div class="w-2/3 bg-white flex-1 overflow-y-auto">
       <div class="p-6">
          <div v-if="!localNodeData.subprocessId" class="flex items-center justify-center h-full text-gray-500">
             <div class="text-center">
                <Icon name="heroicons:document-text" class="h-12 w-12 mx-auto mb-4 text-gray-300" />
                <p>Select a process from the list to see its details.</p>
              </div>
          </div>
          <div v-else-if="isLoadingDetails" class="flex items-center justify-center h-full text-gray-500">
            <Icon name="material-symbols:progress-activity" class="w-6 h-6 animate-spin mr-2" />
            <span>Loading details...</span>
          </div>
          <div v-else-if="selectedProcessDetails">
             <!-- Process Info -->
              <div class="mb-6 p-4 bg-gray-50 rounded-lg">
                <h4 class="font-medium text-gray-900 mb-2">Process Information</h4>
                <div class="grid grid-cols-2 gap-4 text-sm">
                  <div><span class="text-gray-600">Name:</span><span class="ml-2 font-medium">{{ selectedProcessDetails.processName }}</span></div>
                  <div><span class="text-gray-600">Status:</span><RsBadge :color="selectedProcessDetails.processStatus === 'published' ? 'success' : 'warning'" class="ml-2 capitalize">{{ selectedProcessDetails.processStatus }}</RsBadge></div>
                  <div class="col-span-2"><span class="text-gray-600">Description:</span><span class="ml-2">{{ selectedProcessDetails.processDescription || 'N/A' }}</span></div>
                   <div v-if="contentSummary" class="col-span-2 mt-2 pt-4 border-t border-gray-200 grid grid-cols-3 gap-4 text-center">
                      <div><div class="text-lg font-bold text-teal-600">{{ contentSummary.nodes }}</div><div class="text-xs text-gray-500">Nodes</div></div>
                      <div><div class="text-lg font-bold text-teal-600">{{ contentSummary.edges }}</div><div class="text-xs text-gray-500">Connections</div></div>
                      <div><div class="text-lg font-bold text-teal-600">{{ contentSummary.variables }}</div><div class="text-xs text-gray-500">Variables</div></div>
                  </div>
                </div>
              </div>
              <!-- Process Nodes Preview -->
              <div class="space-y-4">
                <h4 class="font-medium text-gray-900">Process Nodes</h4>
                <div class="border rounded-lg p-4 bg-white max-h-64 overflow-y-auto">
                  <div v-if="selectedProcessDetails.processDefinition?.nodes?.length" class="space-y-3">
                    <div v-for="node in selectedProcessDetails.processDefinition.nodes" :key="node.id" class="flex items-center justify-between p-3 border rounded-lg bg-gray-50">
                      <div class="flex items-center space-x-3">
                        <div class="w-8 h-8 bg-blue-100 rounded-lg flex items-center justify-center"><Icon :name="getNodeIcon(node.type)" class="h-4 w-4 text-blue-600" /></div>
                        <div>
                          <div class="font-medium text-gray-900">{{ node.data?.label || node.label || node.type }}</div>
                          <div class="text-sm text-gray-600">{{ node.id }}</div>
                        </div>
                      </div>
                      <div class="text-sm text-gray-500 capitalize">{{ (node.type || 'node').replace(/-/g, ' ') }}</div>
                    </div>
                  </div>
                  <div v-else class="text-center py-8 text-gray-500">No nodes in this process.</div>
                </div>
              </div>
          </div>
       </div>
    </div>
  </div>
</template> 