<script setup>
import { ref, computed, onMounted, watch, onUnmounted } from 'vue';
import { useProcessBuilderStore } from '~/stores/processBuilder';
import { useRouter } from 'vue-router';
import { useToast } from '~/composables/useToast';

// Define page meta
definePageMeta({
  title: "Process Management Dashboard",
  description: "Manage and monitor your business processes",
  layout: "empty",
  middleware: ["auth"],
  requiresAuth: true,
});

// Initialize the store and router
const processStore = useProcessBuilderStore();
const router = useRouter();
const toast = useToast();

// State
const searchQuery = ref('');
const loading = ref(false);
const showDeleteConfirm = ref(false);
const processToDelete = ref(null);
const statusFilter = ref('');
const categoryFilter = ref('');
const sortBy = ref('processCreatedDate');
const sortOrder = ref('desc');
const currentView = ref('dashboard'); // 'dashboard', 'list', 'analytics'

// Copy link modal state
const showCopyLinkModal = ref(false);
const selectedProcess = ref(null);
const copyLinkType = ref('direct'); // 'direct' or 'iframe'
const iframeOptions = ref({
  debug: false,
  hideComplete: true,
  theme: 'light'
});

// Pagination state
const currentPage = ref(1);
const itemsPerPage = ref(20); // Default to 20 items per page
const totalItems = ref(0);

// Dashboard metrics and data
const dashboardMetrics = ref({
  totalProcesses: 0,
  publishedProcesses: 0,
  draftProcesses: 0,
  totalCases: 0,
  activeCases: 0,
  completedCases: 0,
  totalTasks: 0,
  pendingTasks: 0,
  averageCompletionTime: 0,
  successRate: 0
});

const recentActivity = ref([]);
const processChartData = ref([]);
const caseChartData = ref([]);
const dashboardLoading = ref(false);

// Status and category options
const statusOptions = [
  { value: '', label: 'All Status' },
  { value: 'draft', label: 'Draft' },
  { value: 'published', label: 'Published' },
  { value: 'archived', label: 'Archived' },
  { value: 'deleted', label: 'Deleted' }
];

const categoryOptions = [
  { value: '', label: 'All Categories' },
  { value: 'HR', label: 'Human Resources' },
  { value: 'Finance', label: 'Finance' },
  { value: 'IT', label: 'Information Technology' },
  { value: 'Operations', label: 'Operations' },
  { value: 'Procurement', label: 'Procurement' }
];

// Filtered processes (all processes after applying filters)
const filteredProcesses = computed(() => {
  let filtered = processStore.processes;
  
  // Apply search filter
  if (searchQuery.value.trim()) {
    const query = searchQuery.value.toLowerCase().trim();
    filtered = filtered.filter(process => 
      process.name?.toLowerCase().includes(query) ||
      process.description?.toLowerCase().includes(query) ||
      process.category?.toLowerCase().includes(query) ||
      process.creator?.userFullName?.toLowerCase().includes(query) ||
      process.creator?.userUsername?.toLowerCase().includes(query)
    );
  }
  
  // Apply status filter
  if (statusFilter.value) {
    filtered = filtered.filter(process => process.status === statusFilter.value);
  }
  
  // Apply category filter
  if (categoryFilter.value) {
    filtered = filtered.filter(process => process.category === categoryFilter.value);
  }
  
  // Update total items for pagination
  totalItems.value = filtered.length;
  
  return filtered;
});

// Paginated processes (processes for current page)
const paginatedProcesses = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage.value;
  const end = start + itemsPerPage.value;
  return filteredProcesses.value.slice(start, end);
});

// Pagination computed properties
const totalPages = computed(() => {
  return Math.ceil(totalItems.value / itemsPerPage.value);
});

const hasNextPage = computed(() => {
  return currentPage.value < totalPages.value;
});

const hasPrevPage = computed(() => {
  return currentPage.value > 1;
});

const paginationInfo = computed(() => {
  const start = totalItems.value === 0 ? 0 : (currentPage.value - 1) * itemsPerPage.value + 1;
  const end = Math.min(currentPage.value * itemsPerPage.value, totalItems.value);
  return {
    start,
    end,
    total: totalItems.value
  };
});

// Load dashboard summary data from API
const loadDashboardData = async () => {
  dashboardLoading.value = true;
  try {
    const response = await $fetch('/api/process/dashboard/summary');
    
    if (response.success) {
      const data = response.data;
      
      // Update metrics
      dashboardMetrics.value = {
        totalProcesses: data.overview.totalProcesses,
        publishedProcesses: data.overview.publishedProcesses,
        draftProcesses: data.overview.draftProcesses,
        totalCases: data.overview.totalCases,
        activeCases: data.overview.activeCases,
        completedCases: data.overview.completedCases,
        totalTasks: data.overview.totalTasks,
        pendingTasks: data.overview.pendingTasks || data.overview.totalTasks,
        averageCompletionTime: data.overview.averageCompletionTime,
        successRate: data.overview.successRate
      };
      
      // Update chart data
      processChartData.value = data.charts.processDistribution;
      caseChartData.value = data.charts.caseStatus;
      
      // Update recent activity
      recentActivity.value = data.recentActivity;
    }
  } catch (error) {
    console.error('Error loading dashboard data:', error);
    // Fallback to basic metrics from processes
    updateDashboardMetricsFromProcesses();
  } finally {
    dashboardLoading.value = false;
  }
};

// Fallback method to update metrics from processes data
const updateDashboardMetricsFromProcesses = () => {
  const processes = processStore.processes;
  
  dashboardMetrics.value = {
    totalProcesses: processes.length,
    publishedProcesses: processes.filter(p => p.status === 'published').length,
    draftProcesses: processes.filter(p => p.status === 'draft').length,
    totalCases: 0,
    activeCases: 0,
    completedCases: 0,
    totalTasks: 0,
    pendingTasks: 0,
    averageCompletionTime: 0,
    successRate: 0
  };

  processChartData.value = [
    { label: 'Draft', value: processes.filter(p => p.status === 'draft').length, color: '#F59E0B' },
    { label: 'Published', value: processes.filter(p => p.status === 'published').length, color: '#10B981' },
    { label: 'Archived', value: processes.filter(p => p.status === 'archived').length, color: '#6B7280' },
    { label: 'Deleted', value: processes.filter(p => p.status === 'deleted').length, color: '#EF4444' }
  ];
};

// Format date for display
const formatDate = (isoString) => {
  if (!isoString) return '';
  const date = new Date(isoString);
  return new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric'
  }).format(date);
};

// Format relative time
const formatRelativeTime = (isoString) => {
  if (!isoString) return '';
  const date = new Date(isoString);
  const now = new Date();
  const diffMs = now - date;
  const diffMinutes = Math.floor(diffMs / 60000);
  const diffHours = Math.floor(diffMinutes / 60);
  const diffDays = Math.floor(diffHours / 24);
  
  if (diffMinutes < 60) return `${diffMinutes}m ago`;
  if (diffHours < 24) return `${diffHours}h ago`;
  return `${diffDays}d ago`;
};

// Get status badge variant
const getStatusVariant = (status) => {
  switch (status) {
    case 'published': return 'success';
    case 'draft': return 'warning';
    case 'archived': return 'secondary';
    case 'deleted': return 'danger';
    default: return 'primary';
  }
};

// Get activity icon
const getActivityIcon = (type) => {
  switch (type) {
    case 'process_created': return 'material-symbols:add-circle-outline';
    case 'process_published': return 'material-symbols:publish';
    case 'case_completed': return 'material-symbols:check-circle-outline';
    default: return 'material-symbols:info';
  }
};

// Load processes from API with filters
const loadProcesses = async () => {
  loading.value = true;
  try {
    const options = {
      sortBy: sortBy.value,
      sortOrder: sortOrder.value,
      limit: 1000 // Set a high limit to get all processes
    };
    
    if (statusFilter.value) {
      options.status = statusFilter.value;
    }
    
    if (categoryFilter.value) {
      options.category = categoryFilter.value;
    }
    
    if (searchQuery.value.trim()) {
      options.search = searchQuery.value.trim();
    }
    
    await processStore.fetchProcesses(options);
    if (currentView.value === 'dashboard') {
      await loadDashboardData();
    } else {
      updateDashboardMetricsFromProcesses();
    }
  } catch (error) {
    console.error('Error loading processes:', error);
  } finally {
    loading.value = false;
  }
};

// Watch for changes in filters and reload processes
watch([searchQuery, statusFilter, categoryFilter], () => {
  // Reset to first page when filters change
  currentPage.value = 1;
  clearTimeout(searchTimeout);
  searchTimeout = setTimeout(() => {
    loadProcesses();
  }, 500);
});

let searchTimeout = null;

// Navigation functions
const switchView = (view) => {
  currentView.value = view;
};

const editProcess = async (processId) => {
  try {
    router.push(`/process-builder?id=${processId}`);
  } catch (error) {
    console.error('Error navigating to process:', error);
  }
};

const viewProcessAnalytics = (processId) => {
  router.push(`/process-builder/analytics/${processId}`);
};

const viewProcessWorkflow = (processId) => {
  router.push(`/workflow/${processId}`);
};

const duplicateProcess = async (process) => {
  try {
    loading.value = true;
    const newName = `${process.name} (Copy)`;
    await processStore.duplicateProcess(process.id, newName);
    await loadProcesses();
  } catch (error) {
    console.error('Error duplicating process:', error);
  } finally {
    loading.value = false;
  }
};

const deleteProcess = async () => {
  if (!processToDelete.value) return;
  
  try {
    loading.value = true;
    await processStore.deleteProcess(processToDelete.value);
    await loadProcesses();
    showDeleteConfirm.value = false;
    processToDelete.value = null;
  } catch (error) {
    console.error('Error deleting process:', error);
  } finally {
    loading.value = false;
  }
};

const publishProcess = async (processId) => {
  try {
    loading.value = true;
    await processStore.publishProcess(processId);
    await loadProcesses();
    toast.success('Process published successfully');
  } catch (error) {
    console.error('Error publishing process:', error);
    toast.error('Failed to publish process: ' + (error.message || 'Unknown error'));
  } finally {
    loading.value = false;
  }
};

const unpublishProcess = async (processId) => {
  try {
    loading.value = true;
    await processStore.unpublishProcess(processId);
    await loadProcesses();
    toast.success('Process unpublished successfully');
  } catch (error) {
    console.error('Error unpublishing process:', error);
    toast.error('Failed to unpublish process: ' + (error.message || 'Unknown error'));
  } finally {
    loading.value = false;
  }
};

const restoreProcess = async (processId) => {
  try {
    loading.value = true;
    await processStore.restoreProcess(processId);
    await loadProcesses();
  } catch (error) {
    console.error('Error restoring process:', error);
  } finally {
    loading.value = false;
  }
};

const confirmDelete = (processId) => {
  const process = processStore.processes.find(p => p.id === processId);
  
  // If process is published, show a message instead of allowing deletion
  if (process && process.status === 'published') {
    toast.warning('Please unpublish the process first before deleting it.');
    return;
  }
  
  processToDelete.value = processId;
  showDeleteConfirm.value = true;
};

const cancelDelete = () => {
  showDeleteConfirm.value = false;
  processToDelete.value = null;
};

const createNewProcess = () => {
  processStore.clearCurrentProcess();
  router.push('/process-builder');
};

const goToBuilder = () => {
  router.push('/');
};

const clearFilters = () => {
  searchQuery.value = '';
  statusFilter.value = '';
  categoryFilter.value = '';
  currentPage.value = 1; // Reset to first page
};

// Pagination methods
const goToPage = (page) => {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page;
  }
};

const nextPage = () => {
  if (hasNextPage.value) {
    currentPage.value++;
  }
};

const prevPage = () => {
  if (hasPrevPage.value) {
    currentPage.value--;
  }
};

const changeItemsPerPage = (newItemsPerPage) => {
  itemsPerPage.value = newItemsPerPage;
  currentPage.value = 1; // Reset to first page when changing items per page
};

// Load processes on component mount
onMounted(async () => {
  await loadProcesses();
});

// Clean up the search timeout on component unmount
onUnmounted(() => {
  clearTimeout(searchTimeout);
});

// Enhanced copy workflow link functionality
const openCopyLinkModal = (process) => {
  selectedProcess.value = process;
  showCopyLinkModal.value = true;
};

const copyWorkflowLink = async (processId, linkType = 'direct', options = {}) => {
  try {
    // Check if we're on the client side
    if (process.client) {
      let link = `${baseUrl.value}/workflow/${processId}`;
      
      if (linkType === 'iframe') {
        const params = new URLSearchParams();
        if (options.debug !== undefined) params.append('debug', options.debug);
        if (options.hideComplete !== undefined) params.append('hideComplete', options.hideComplete);
        if (options.theme) params.append('theme', options.theme);
        
        if (params.toString()) {
          link += `?${params.toString()}`;
        }
      }
      
      await navigator.clipboard.writeText(link);
      toast.success(`${linkType === 'iframe' ? 'Iframe link' : 'Direct link'} copied to clipboard!`);
    }
  } catch (err) {
    toast.error('Failed to copy link');
  }
};

const openWorkflowLink = (processId, linkType = 'direct', options = {}) => {
  // Check if we're on the client side
  if (process.client) {
    let link = `${baseUrl.value}/workflow/${processId}`;
    
    if (linkType === 'iframe') {
      const params = new URLSearchParams();
      if (options.debug !== undefined) params.append('debug', options.debug);
      if (options.hideComplete !== undefined) params.append('hideComplete', options.hideComplete);
      if (options.theme) params.append('theme', options.theme);
      
      if (params.toString()) {
        link += `?${params.toString()}`;
      }
    }
    
    window.open(link, '_blank');
  }
};

const closeCopyLinkModal = () => {
  showCopyLinkModal.value = false;
  selectedProcess.value = null;
  copyLinkType.value = 'direct';
  iframeOptions.value = {
    debug: false,
    hideComplete: true,
    theme: 'light'
  };
};

const handleCopyLink = async () => {
  if (!selectedProcess.value) return;
  
  await copyWorkflowLink(selectedProcess.value.id, copyLinkType.value, copyLinkType.value === 'iframe' ? iframeOptions.value : {});
  closeCopyLinkModal();
};

const handleOpenLink = () => {
  if (!selectedProcess.value) return;
  
  openWorkflowLink(selectedProcess.value.id, copyLinkType.value, copyLinkType.value === 'iframe' ? iframeOptions.value : {});
  closeCopyLinkModal();
};

// Computed properties for link generation
const baseUrl = computed(() => {
  return process.client ? window.location.origin : '';
});

const generatedLink = computed(() => {
  if (!selectedProcess.value || !baseUrl.value) return '';
  
  if (copyLinkType.value === 'iframe') {
    const params = new URLSearchParams();
    if (iframeOptions.value.debug !== undefined) params.append('debug', iframeOptions.value.debug);
    if (iframeOptions.value.hideComplete !== undefined) params.append('hideComplete', iframeOptions.value.hideComplete);
    if (iframeOptions.value.theme) params.append('theme', iframeOptions.value.theme);
    
    return `${baseUrl.value}/workflow/${selectedProcess.value.id}${params.toString() ? `?${params.toString()}` : ''}`;
  }
  
  return `${baseUrl.value}/workflow/${selectedProcess.value.id}`;
});

const generateIframeCode = () => {
  if (!selectedProcess.value || !baseUrl.value) return '';
  
  const params = new URLSearchParams();
  if (iframeOptions.value.debug !== undefined) params.append('debug', iframeOptions.value.debug);
  if (iframeOptions.value.hideComplete !== undefined) params.append('hideComplete', iframeOptions.value.hideComplete);
  if (iframeOptions.value.theme) params.append('theme', iframeOptions.value.theme);
  
  const url = `${baseUrl.value}/workflow/${selectedProcess.value.id}${params.toString() ? `?${params.toString()}` : ''}`;
  
  return `<iframe 
  src="${url}"
  width="100%" 
  height="600px"
  style="border: none; border-radius: 8px;">
</iframe>`;
};
</script>

<template>
  <div class="flex flex-col h-screen bg-gray-50">
    <!-- Header Bar -->
    <header class="bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between shadow-sm">
      <div class="flex items-center gap-4">
        <Icon
          @click="goToBuilder"
          name="ph:arrow-circle-left-duotone"
          class="cursor-pointer w-6 h-6 hover:text-gray-600 text-gray-500"
        />
        <div class="flex items-center gap-3">
        <img
            src="@/assets/img/logo/logo-word-black.svg"
          alt="Corrad Logo"
            class="h-8"
        />
          <div class="border-l border-gray-300 pl-3">
            <h1 class="text-xl font-semibold text-gray-900">Process Management</h1>
            <p class="text-sm text-gray-500">Monitor and manage your business processes</p>
          </div>
        </div>
      </div>

      <div class="flex items-center gap-3">
        <!-- View Toggle -->
        <div class="flex bg-gray-100 rounded-lg p-1">
          <button
            @click="switchView('dashboard')"
            :class="[
              'px-3 py-1 text-sm rounded-md transition-colors',
              currentView === 'dashboard' 
                ? 'bg-white text-gray-900 shadow-sm' 
                : 'text-gray-600 hover:text-gray-900'
            ]"
          >
            <Icon name="material-symbols:dashboard-outline" class="mr-1" />
            Dashboard
          </button>
          <button
            @click="switchView('list')"
            :class="[
              'px-3 py-1 text-sm rounded-md transition-colors',
              currentView === 'list' 
                ? 'bg-white text-gray-900 shadow-sm' 
                : 'text-gray-600 hover:text-gray-900'
            ]"
          >
            <Icon name="material-symbols:list" class="mr-1" />
            List View
          </button>
      </div>
      
        <RsButton @click="createNewProcess" variant="primary" size="sm">
          <Icon name="material-symbols:add" class="mr-1" />
          Create Process
        </RsButton>
      </div>
    </header>

    <!-- Main Content Area -->
    <div class="flex-1 overflow-auto">
      <!-- Dashboard View -->
      <div v-if="currentView === 'dashboard'" class="p-6 space-y-6">
        <!-- Metrics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
          <!-- Total Processes -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="flex items-center justify-between">
              <div>
                <p class="text-sm font-medium text-gray-600">Total Processes</p>
                <p class="text-3xl font-bold text-gray-900">{{ dashboardMetrics.totalProcesses }}</p>
                <p class="text-sm text-gray-500 mt-1">
                  {{ dashboardMetrics.publishedProcesses }} published
                </p>
              </div>
              <div class="p-3 bg-blue-50 rounded-lg">
                <Icon name="material-symbols:shape-line-outline-sharp" class="w-6 h-6 text-blue-600" />
              </div>
            </div>
          </div>

          <!-- Active Cases -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="flex items-center justify-between">
              <div>
                <p class="text-sm font-medium text-gray-600">Active Cases</p>
                <p class="text-3xl font-bold text-gray-900">{{ dashboardMetrics.activeCases }}</p>
                <p class="text-sm text-green-600 mt-1">
                  {{ (dashboardMetrics.activeCases + dashboardMetrics.completedCases) > 0 ? `${dashboardMetrics.completedCases} completed` : 'No cases' }}
                </p>
              </div>
              <div class="p-3 bg-green-50 rounded-lg">
                <Icon name="material-symbols:play-circle-outline" class="w-6 h-6 text-green-600" />
              </div>
            </div>
          </div>

          <!-- Completion Rate -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="flex items-center justify-between">
              <div>
                <p class="text-sm font-medium text-gray-600">Success Rate</p>
                <p class="text-3xl font-bold text-gray-900">{{ dashboardMetrics.successRate }}%</p>
                <p class="text-sm text-blue-600 mt-1">
                  {{ dashboardMetrics.completedCases }} completed
                </p>
              </div>
              <div class="p-3 bg-purple-50 rounded-lg">
                <Icon name="material-symbols:check-circle-outline" class="w-6 h-6 text-purple-600" />
              </div>
            </div>
          </div>

          <!-- Avg. Completion Time -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="flex items-center justify-between">
              <div>
                <p class="text-sm font-medium text-gray-600">Avg. Completion</p>
                <p class="text-3xl font-bold text-gray-900">{{ dashboardMetrics.averageCompletionTime }}{{ dashboardMetrics.averageCompletionTime > 0 ? 'h' : '' }}</p>
                <p class="text-sm text-orange-600 mt-1">
                  {{ dashboardMetrics.pendingTasks }} tasks pending
                </p>
              </div>
              <div class="p-3 bg-orange-50 rounded-lg">
                <Icon name="material-symbols:timer-outline" class="w-6 h-6 text-orange-600" />
              </div>
            </div>
          </div>
        </div>

        <!-- Charts and Recent Activity -->
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
          <!-- Process Distribution Chart -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">Process Distribution</h3>
            <div class="h-64 flex items-center justify-center">
              <div class="text-center">
                <Icon name="material-symbols:pie-chart-outline" class="w-16 h-16 text-gray-400 mx-auto mb-2" />
                <p class="text-gray-500">Chart will be implemented</p>
              </div>
            </div>
          </div>

          <!-- Case Status Chart -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">Case Status</h3>
            <div class="h-64 flex items-center justify-center">
              <div class="text-center">
                <Icon name="material-symbols:donut-small" class="w-16 h-16 text-gray-400 mx-auto mb-2" />
                <p class="text-gray-500">Chart will be implemented</p>
              </div>
            </div>
          </div>

          <!-- Recent Activity -->
          <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">Recent Activity</h3>
            <div class="space-y-4">
              <div
                v-for="activity in recentActivity"
                :key="activity.id"
                class="flex items-start gap-3"
              >
                <div class="p-2 bg-gray-50 rounded-lg">
                  <Icon
                    :name="getActivityIcon(activity.type)"
                    class="w-4 h-4 text-gray-600"
                  />
                </div>
                <div class="flex-1 min-w-0">
                  <p class="text-sm text-gray-900">{{ activity.message }}</p>
                  <p class="text-xs text-gray-500 mt-1">
                    by {{ activity.user }} • {{ formatRelativeTime(activity.timestamp) }}
                  </p>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- Quick Actions -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
          <h3 class="text-lg font-semibold text-gray-900 mb-4">Quick Actions</h3>
          <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <RsButton @click="createNewProcess" variant="primary" class="justify-center">
              <Icon name="material-symbols:add" class="mr-2" />
              Create New Process
            </RsButton>
            <RsButton @click="switchView('list')" variant="secondary" class="justify-center">
              <Icon name="material-symbols:list" class="mr-2" />
              View All Processes
            </RsButton>
            <RsButton @click="router.push('/execution')" variant="tertiary" class="justify-center">
              <Icon name="material-symbols:play-arrow" class="mr-2" />
              Execute Processes
            </RsButton>
          </div>
        </div>
      </div>

      <!-- List View -->
      <div v-else-if="currentView === 'list'" class="p-6">
      <!-- Filters and Search -->
        <div class="mb-6 bg-white rounded-xl shadow-sm border border-gray-200 p-4">
          <div class="flex flex-col lg:flex-row gap-4">
        <div class="flex-1 max-w-md">
          <FormKit
            v-model="searchQuery"
            type="text"
            placeholder="Search processes..."
            :classes="{
              outer: 'mb-0',
              wrapper: 'relative',
              inner: 'relative',
                  input: 'w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500',
              prefixIcon: 'absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 pointer-events-none'
            }"
          >
            <template #prefixIcon>
              <Icon name="material-symbols:search" class="text-lg" />
            </template>
          </FormKit>
        </div>
        
        <div class="min-w-48">
          <FormKit
            v-model="statusFilter"
            type="select"
            :options="statusOptions"
            placeholder="Filter by status"
            :classes="{
              outer: 'mb-0',
                  input: 'w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white'
                }"
              />
            </div>

            <div class="min-w-48">
              <FormKit
                v-model="categoryFilter"
                type="select"
                :options="categoryOptions"
                placeholder="Filter by category"
                :classes="{
                  outer: 'mb-0',
                  input: 'w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white'
            }"
          />
        </div>
        
        <div class="flex gap-2">
              <RsButton 
                @click="clearFilters" 
                variant="secondary" 
                size="sm" 
                :disabled="loading" 
                v-if="searchQuery || statusFilter || categoryFilter"
              >
            <Icon name="material-symbols:filter-alt-off" class="mr-1" />
                Clear
          </RsButton>
          
          <RsButton @click="loadProcesses" variant="tertiary" size="sm" :disabled="loading">
            <Icon name="material-symbols:refresh" class="mr-1" />
            Refresh
          </RsButton>
            </div>
        </div>
      </div>
      
      <!-- Loading State -->
      <div v-if="loading && processStore.processes.length === 0" class="flex justify-center items-center py-12">
        <div class="text-center">
          <Icon name="material-symbols:progress-activity" class="w-8 h-8 animate-spin text-blue-500 mx-auto mb-2" />
          <p class="text-gray-500">Loading processes...</p>
        </div>
      </div>
      
        <!-- Processes Grid -->
        <div v-else-if="filteredProcesses.length > 0" class="grid gap-4">
          
          <div
            v-for="process in paginatedProcesses"
            :key="process.id"
            class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 hover:shadow-md transition-shadow"
          >
            <div class="flex items-start justify-between">
              <div class="flex-1">
                <div class="flex items-center gap-3 mb-2">
                  <h3 class="text-lg font-semibold text-gray-900">{{ process.name }}</h3>
                  <RsBadge :variant="getStatusVariant(process.status)">
                    {{ process.status || 'draft' }}
                  </RsBadge>
                </div>
                
                <p class="text-gray-600 mb-3">
                  {{ process.description || 'No description available' }}
                </p>
                
                <div class="flex items-center gap-6 text-sm text-gray-500">
                  <div class="flex items-center gap-1">
                    <Icon name="material-symbols:folder-outline" class="w-4 h-4" />
                    {{ process.category || 'Uncategorized' }}
                  </div>
                  <div class="flex items-center gap-1">
                    <Icon name="material-symbols:person-outline" class="w-4 h-4" />
                    {{ process.creator?.userFullName || process.creator?.userUsername || 'Unknown' }}
                  </div>
                  <div class="flex items-center gap-1">
                    <Icon name="material-symbols:schedule" class="w-4 h-4" />
                    {{ formatDate(process.updatedAt) }}
                  </div>
                </div>
                </div>
              
              <div class="flex items-center gap-2 ml-4">
                <!-- Copy Run Link Button (for published processes) -->
                <button 
                  v-if="process.status === 'published'"
                  @click="openCopyLinkModal(process)"
                  class="p-2 text-green-600 hover:text-green-800 hover:bg-green-50 rounded-lg transition-colors"
                  title="Copy or Open Run Link"
                  :disabled="loading"
                >
                  <Icon name="material-symbols:link" class="text-lg" />
                </button>
                
                <!-- Analytics Button -->
                <!-- <button 
                  @click="viewProcessAnalytics(process.id)"
                  class="p-2 text-blue-600 hover:text-blue-800 hover:bg-blue-50 rounded-lg transition-colors"
                  title="View Analytics"
                  :disabled="loading"
                >
                  <Icon name="material-symbols:analytics" class="text-lg" />
                </button> -->
                
                  <!-- Show restore button for deleted processes -->
                  <button 
                    v-if="process.status === 'deleted'"
                    @click="restoreProcess(process.id)"
                  class="p-2 text-green-600 hover:text-green-800 hover:bg-green-50 rounded-lg transition-colors"
                    title="Restore Process"
                    :disabled="loading"
                  >
                    <Icon name="material-symbols:restore" class="text-lg" />
                  </button>
                  
                  <!-- Regular action buttons for non-deleted processes -->
                  <template v-else>
                    <button 
                      @click="editProcess(process.id)"
                    class="p-2 text-blue-600 hover:text-blue-800 hover:bg-blue-50 rounded-lg transition-colors"
                      title="Edit Process"
                      :disabled="loading"
                    >
                      <Icon name="material-symbols:edit" class="text-lg" />
                    </button>
                    
                    <button 
                      v-if="process.status === 'draft'"
                      @click="publishProcess(process.id)"
                      class="p-2 text-green-600 hover:text-green-800 hover:bg-green-50 rounded-lg transition-colors"
                      title="Publish Process"
                      :disabled="loading"
                    >
                      <Icon name="material-symbols:publish" class="text-lg" />
                    </button>
                    
                    <button 
                      v-if="process.status === 'published'"
                      @click="unpublishProcess(process.id)"
                      class="p-2 text-orange-600 hover:text-orange-800 hover:bg-orange-50 rounded-lg transition-colors"
                      title="Unpublish Process (Move to Draft)"
                      :disabled="loading"
                    >
                      <Icon name="material-symbols:unpublished" class="text-lg" />
                    </button>
                    
                    <button 
                      @click="duplicateProcess(process)"
                    class="p-2 text-indigo-600 hover:text-indigo-800 hover:bg-indigo-50 rounded-lg transition-colors"
                      title="Duplicate Process"
                      :disabled="loading"
                    >
                      <Icon name="material-symbols:content-copy" class="text-lg" />
                    </button>
                    
                    <button 
                      @click="confirmDelete(process.id)"
                      class="p-2 text-red-600 hover:text-red-800 hover:bg-red-50 rounded-lg transition-colors"
                      :title="process.status === 'published' ? 'Unpublish first to delete this process' : 'Delete Process'"
                      :disabled="loading || process.status === 'published'"
                    >
                      <Icon name="material-symbols:delete" class="text-lg" />
                    </button>
                  </template>
                </div>
            </div>
          </div>
        </div>

        <!-- Pagination -->
        <div v-if="filteredProcesses.length > 0 && totalPages > 1" class="mt-6 bg-white rounded-xl shadow-sm border border-gray-200 p-4">
          <div class="flex flex-col sm:flex-row items-center justify-between gap-4">
            <!-- Pagination Info -->
            <div class="text-sm text-gray-700">
              Showing {{ paginationInfo.start }}-{{ paginationInfo.end }} of {{ paginationInfo.total }} processes
            </div>
            
            <!-- Items per page selector -->
            <div class="flex items-center gap-2">
              <span class="text-sm text-gray-700">Items per page:</span>
              <FormKit
                :model-value="itemsPerPage"
                @update:model-value="changeItemsPerPage"
                type="select"
                :options="[
                  { label: '5', value: 5 },
                  { label: '10', value: 10 },
                  { label: '20', value: 20 },
                  { label: '50', value: 50 },
                  { label: '100', value: 100 }
                ]"
                :classes="{
                  outer: 'mb-0',
                  input: 'px-2 py-1 border border-gray-300 rounded text-sm bg-white min-w-16'
                }"
              />
            </div>
            
            <!-- Pagination Controls -->
            <div class="flex items-center gap-1">
              <!-- Previous Button -->
              <button
                @click="prevPage"
                :disabled="!hasPrevPage"
                class="px-3 py-2 text-sm border border-gray-300 rounded-lg bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              >
                <Icon name="material-symbols:chevron-left" class="w-4 h-4" />
              </button>
              
              <!-- Page Numbers -->
              <template v-if="totalPages <= 7">
                <!-- Show all pages if 7 or fewer -->
                <button
                  v-for="page in totalPages"
                  :key="page"
                  @click="goToPage(page)"
                  :class="[
                    'px-3 py-2 text-sm border rounded-lg transition-colors',
                    page === currentPage 
                      ? 'bg-blue-600 text-white border-blue-600' 
                      : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'
                  ]"
                >
                  {{ page }}
                </button>
              </template>
              <template v-else>
                <!-- Show first page -->
                <button
                  @click="goToPage(1)"
                  :class="[
                    'px-3 py-2 text-sm border rounded-lg transition-colors',
                    1 === currentPage 
                      ? 'bg-blue-600 text-white border-blue-600' 
                      : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'
                  ]"
                >
                  1
                </button>
                
                <!-- Show ellipsis if needed -->
                <span v-if="currentPage > 3" class="px-2 text-gray-500">...</span>
                
                <!-- Show pages around current page -->
                <button
                  v-for="page in [currentPage - 1, currentPage, currentPage + 1].filter(p => p > 1 && p < totalPages)"
                  :key="page"
                  @click="goToPage(page)"
                  :class="[
                    'px-3 py-2 text-sm border rounded-lg transition-colors',
                    page === currentPage 
                      ? 'bg-blue-600 text-white border-blue-600' 
                      : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'
                  ]"
                >
                  {{ page }}
                </button>
                
                <!-- Show ellipsis if needed -->
                <span v-if="currentPage < totalPages - 2" class="px-2 text-gray-500">...</span>
                
                <!-- Show last page -->
                <button
                  v-if="totalPages > 1"
                  @click="goToPage(totalPages)"
                  :class="[
                    'px-3 py-2 text-sm border rounded-lg transition-colors',
                    totalPages === currentPage 
                      ? 'bg-blue-600 text-white border-blue-600' 
                      : 'bg-white text-gray-700 border-gray-300 hover:bg-gray-50'
                  ]"
                >
                  {{ totalPages }}
                </button>
              </template>
              
              <!-- Next Button -->
              <button
                @click="nextPage"
                :disabled="!hasNextPage"
                class="px-3 py-2 text-sm border border-gray-300 rounded-lg bg-white hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              >
                <Icon name="material-symbols:chevron-right" class="w-4 h-4" />
              </button>
            </div>
          </div>
        </div>

        <!-- Empty State -->
        <div v-else-if="filteredProcesses.length === 0" class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
          <Icon name="material-symbols:folder-open-outline" class="w-16 h-16 text-gray-300 mx-auto mb-4" />
          <h3 class="text-lg font-medium text-gray-900 mb-2">
            {{ (searchQuery || statusFilter || categoryFilter) ? 'No processes match your filters' : 'No processes found' }}
          </h3>
          <p class="text-gray-600 mb-6">
            {{ (searchQuery || statusFilter || categoryFilter) ? 'Try adjusting your search or filter criteria' : 'Create your first process to get started' }}
          </p>
          
          <!-- Show current filters if any -->
          <div v-if="searchQuery || statusFilter || categoryFilter" class="text-sm text-gray-400 mb-6 space-y-1">
            <div v-if="searchQuery">Search: "{{ searchQuery }}"</div>
            <div v-if="statusFilter">Status: {{ statusOptions.find(opt => opt.value === statusFilter)?.label }}</div>
            <div v-if="categoryFilter">Category: {{ categoryOptions.find(opt => opt.value === categoryFilter)?.label }}</div>
          </div>
          
          <div class="flex justify-center gap-3">
            <RsButton v-if="searchQuery || statusFilter || categoryFilter" @click="clearFilters" variant="secondary">
              <Icon name="material-symbols:filter-alt-off" class="mr-2" />
              Clear Filters
            </RsButton>
            <RsButton v-if="!searchQuery && !statusFilter && !categoryFilter" @click="createNewProcess" variant="primary">
              <Icon name="material-symbols:add" class="mr-2" />
              Create New Process
            </RsButton>
          </div>
        </div>
      </div>
      
      <!-- Global Loading Overlay -->
      <div v-if="loading && processStore.processes.length > 0" class="fixed inset-0 bg-black bg-opacity-20 flex items-center justify-center z-50">
        <div class="bg-white rounded-lg p-6 flex items-center space-x-3 shadow-lg">
          <Icon name="material-symbols:progress-activity" class="w-6 h-6 animate-spin text-blue-500" />
          <span class="text-gray-700">Processing...</span>
        </div>
      </div>
    </div>
    
    <!-- Delete confirmation dialog -->
    <RsModal v-model="showDeleteConfirm" title="Move Process to Trash" size="md" position="center">
      <div class="p-4">
        <div class="flex items-center mb-4">
          <Icon name="material-symbols:delete-outline" class="text-orange-500 w-8 h-8 mr-3 flex-shrink-0" />
          <div>
            <p class="text-gray-600 font-medium mb-1">Move to Trash</p>
            <p class="text-gray-600 text-sm">
              This process will be moved to trash but not permanently deleted. You can restore it later from the "Deleted" filter if needed.
            </p>
            <p class="text-gray-500 text-xs mt-2">
              Note: To delete a published process, first unpublish it using the orange unpublish button.
            </p>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="flex justify-end gap-2">
          <RsButton @click="cancelDelete" variant="tertiary" :disabled="loading">
            Cancel
          </RsButton>
          <RsButton @click="deleteProcess" variant="warning" :disabled="loading">
            <Icon v-if="loading" name="material-symbols:progress-activity" class="w-4 h-4 animate-spin mr-1" />
            Move to Trash
          </RsButton>
        </div>
      </template>
    </RsModal>
    
    <!-- Copy Link Modal -->
    <RsModal v-model="showCopyLinkModal" title="Share Workflow Link" size="lg" position="center">
      <div class="p-6">
        <!-- Process Info -->
        <div class="mb-6 p-4 bg-gray-50 rounded-lg">
          <div class="flex items-center gap-3">
            <Icon name="material-symbols:link" class="text-blue-600 w-6 h-6" />
            <div>
              <h3 class="font-semibold text-gray-900">{{ selectedProcess?.name }}</h3>
              <p class="text-sm text-gray-600">{{ selectedProcess?.description || 'No description available' }}</p>
            </div>
          </div>
        </div>
        
        <!-- Link Type Selection -->
        <div class="mb-6">
          <h4 class="font-medium text-gray-900 mb-3">Link Type</h4>
          <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div 
              @click="copyLinkType = 'direct'"
              :class="[
                'p-4 border-2 rounded-lg cursor-pointer transition-colors',
                copyLinkType === 'direct' 
                  ? 'border-blue-500 bg-blue-50' 
                  : 'border-gray-200 hover:border-gray-300'
              ]"
            >
              <div class="flex items-center gap-3">
                <div :class="[
                  'w-4 h-4 rounded-full border-2',
                  copyLinkType === 'direct' 
                    ? 'border-blue-500 bg-blue-500' 
                    : 'border-gray-300'
                ]">
                  <Icon v-if="copyLinkType === 'direct'" name="material-symbols:check" class="w-3 h-3 text-white" />
                </div>
                <div>
                  <h5 class="font-medium text-gray-900">Direct Link</h5>
                  <p class="text-sm text-gray-600">Direct URL to the workflow</p>
                </div>
              </div>
            </div>
            
            <div 
              @click="copyLinkType = 'iframe'"
              :class="[
                'p-4 border-2 rounded-lg cursor-pointer transition-colors',
                copyLinkType === 'iframe' 
                  ? 'border-blue-500 bg-blue-50' 
                  : 'border-gray-200 hover:border-gray-300'
              ]"
            >
              <div class="flex items-center gap-3">
                <div :class="[
                  'w-4 h-4 rounded-full border-2',
                  copyLinkType === 'iframe' 
                    ? 'border-blue-500 bg-blue-500' 
                    : 'border-gray-300'
                ]">
                  <Icon v-if="copyLinkType === 'iframe'" name="material-symbols:check" class="w-3 h-3 text-white" />
                </div>
                <div>
                  <h5 class="font-medium text-gray-900">Iframe Integration</h5>
                  <p class="text-sm text-gray-600">Embed in external applications</p>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- Iframe Options -->
        <div v-if="copyLinkType === 'iframe'" class="mb-6">
          <h4 class="font-medium text-gray-900 mb-3">Iframe Options</h4>
          <div class="space-y-4">
            <div class="flex items-center justify-between">
              <div>
                <label class="text-sm font-medium text-gray-700">Hide Debug Information</label>
                <p class="text-xs text-gray-500">Remove debug UI elements</p>
              </div>
              <FormKit
                v-model="iframeOptions.debug"
                type="checkbox"
                :classes="{
                  wrapper: 'flex items-center',
                  input: 'w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500'
                }"
              />
            </div>
            
            <div class="flex items-center justify-between">
              <div>
                <label class="text-sm font-medium text-gray-700">Hide Completion Message</label>
                <p class="text-xs text-gray-500">Auto-advance after completion</p>
              </div>
              <FormKit
                v-model="iframeOptions.hideComplete"
                type="checkbox"
                :classes="{
                  wrapper: 'flex items-center',
                  input: 'w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500'
                }"
              />
            </div>
            
            <div>
              <label class="text-sm font-medium text-gray-700">Theme</label>
              <FormKit
                v-model="iframeOptions.theme"
                type="select"
                :options="[
                  { label: 'Light', value: 'light' },
                  { label: 'Dark', value: 'dark' }
                ]"
                :classes="{
                  outer: 'mt-1',
                  input: 'w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500'
                }"
              />
            </div>
          </div>
        </div>
        
        <!-- Generated Link Preview -->
        <div class="mb-6">
          <h4 class="font-medium text-gray-900 mb-3">Generated Link</h4>
          <div class="bg-gray-50 p-3 rounded-lg">
            <code class="text-sm text-gray-800 break-all">
              {{ generatedLink }}
            </code>
          </div>
        </div>
        
        <!-- Iframe Code Preview -->
        <div v-if="copyLinkType === 'iframe'" class="mb-6">
          <h4 class="font-medium text-gray-900 mb-3">Iframe Code</h4>
          <div class="bg-gray-50 p-3 rounded-lg">
            <pre class="text-sm text-gray-800 overflow-x-auto"><code>{{ generateIframeCode() }}</code></pre>
          </div>
        </div>
      </div>
      
      <template #footer>
        <div class="flex justify-between items-center">
          <div class="flex gap-2">
            <RsButton @click="handleOpenLink" variant="secondary" :disabled="!selectedProcess">
              <Icon name="material-symbols:open-in-new" class="mr-2" />
              Open Link
            </RsButton>
            <RsButton @click="handleCopyLink" variant="primary" :disabled="!selectedProcess">
              <Icon name="material-symbols:content-copy" class="mr-2" />
              Copy Link
            </RsButton>
          </div>
        </div>
      </template>
    </RsModal>
  </div>
</template>

<style scoped>
/* Custom transitions and animations */
.transition-colors {
  transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  transition-duration: 150ms;
}

.transition-shadow {
  transition-property: box-shadow;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  transition-duration: 150ms;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Glassmorphism effect for cards */
.bg-white {
  backdrop-filter: blur(10px);
}
</style> 