# Enhanced Import UI Design Document

**Version**: 1.0
**Date**: September 11, 2025
**Author**: Quinn Johns (w/Cursor)

---

## Executive Summary

This document outlines the design for a comprehensive front-end enhancement to the NetSuite record import page, leveraging the extensive backend metrics now available through the Wave Coordination System, dependency resolution, and batch processing architecture. The enhanced UI will provide unprecedented visibility into import progress, dependency relationships, and real-time performance metrics.

## Current State Analysis

### ✅ Available Backend Infrastructure
The system currently provides rich metrics that are underutilized:

| **Metric Source** | **Available Data** | **Current Usage** |
|-------------------|-------------------|-------------------|
| **Wave Coordination** | Wave progress, batch completion, error categorization, performance metrics | ❌ Not displayed |
| **Dependency Resolver** | Complete dependency maps, execution order, record type relationships | ❌ Limited visibility |
| **Job Status Tracker** | Record-type-specific progress, error counts, processing stages | ✅ Basic progress bars |
| **Performance Monitor** | Throughput, processing times, efficiency ratings | ❌ Not displayed |
| **Error Categorization** | Timeout, API, database, permanent failures with retry logic | ❌ Generic error display |

### 🔍 Current UI Limitations
1. **Limited Visibility**: Shows only individual record type progress
2. **No Dependency Context**: Users don't see what dependencies are being processed
3. **Basic Error Handling**: Generic failure messages without categorization
4. **No Performance Insights**: No indication of processing speed or bottlenecks
5. **Polling Inefficiency**: 2-second polling regardless of import stage

---

## Enhanced UI/UX Requirements

### 🎯 Primary Goals
1. **Complete Transparency**: Show all record types involved in import
2. **Dependency Awareness**: Visual representation of dependency relationships
3. **Real-Time Intelligence**: Adaptive updates based on import stage
4. **Performance Visibility**: Clear insights into processing speed and efficiency
5. **Advanced Error Handling**: Categorized errors with intelligent retry status

### 🎨 Core UI Components

#### 1. **Import Overview Dashboard**
```
┌─────────────────────────────────────────────────────────────────┐
│ 📊 IMPORT OVERVIEW                                              │
├─────────────────────────────────────────────────────────────────┤
│ Import ID: 68bc625ccb8904         Status: 🔄 Processing Wave 3  │
│ Started: 2h 15m ago               ETA: ~45 minutes remaining     │
│ Progress: ████████░░ 78%          Speed: 1,247 records/min      │
│                                                                 │
│ 📈 Performance: ⭐⭐⭐⭐⭐ Excellent (5.2x faster than baseline) │
└─────────────────────────────────────────────────────────────────┘
```

#### 2. **Dependency Flow Visualization**
```
┌─────────────────────────────────────────────────────────────────┐
│ 🔗 DEPENDENCY FLOW                                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│ Level 0 (Dependencies):                                         │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐                │
│ │ Subsidiaries│ │ Locations   │ │ PriceLevels │                │
│ │ ✅ 45/45    │ │ ✅ 12/12    │ │ ✅ 8/8      │                │
│ │ Completed   │ │ Completed   │ │ Completed   │                │
│ └─────────────┘ └─────────────┘ └─────────────┘                │
│        │                │                │                     │
│        └────────────────┼────────────────┘                     │
│                         ▼                                       │
│ Level 1 (Main Records):                                         │
│ ┌─────────────┐ ┌─────────────┐                                │
│ │ Customers   │ │ Projects    │                                │
│ │ 🔄 2,847/   │ │ ⏳ Pending  │                                │
│ │   4,521     │ │ 1,200 recs  │                                │
│ │ Wave 3/8    │ │             │                                │
│ └─────────────┘ └─────────────┘                                │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

#### 3. **Record Type Progress Cards**
```
┌─────────────────────────────────────────────────────────────────┐
│ 📋 RECORD TYPE PROGRESS                                         │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│ ┌───────────────────────────┐ ┌───────────────────────────┐    │
│ │ 👥 Customers              │ │ 🏢 Projects               │    │
│ │ ████████░░ 63% (2,847/    │ │ ⏳ Queued                 │    │
│ │           4,521)          │ │ 1,200 records             │    │
│ │                           │ │ Dependency: Customers     │    │
│ │ 🌊 Wave 3 of 8           │ │ ETA: 15 minutes           │    │
│ │ ⚡ 847 rec/min            │ │                           │    │
│ │ 🔄 Processing Batch 23/30 │ │ 📊 Complexity: Medium     │    │
│ │                           │ │                           │    │
│ │ ⚠️ 3 retries (API limits) │ │                           │    │
│ └───────────────────────────┘ └───────────────────────────┘    │
│                                                                 │
│ ┌───────────────────────────┐ ┌───────────────────────────┐    │
│ │ 🏭 Subsidiaries           │ │ 📍 Locations              │    │
│ │ ✅ Completed              │ │ ✅ Completed              │    │
│ │ 45/45 records (100%)      │ │ 12/12 records (100%)      │    │
│ │                           │ │                           │    │
│ │ ⏱️ 2m 15s                │ │ ⏱️ 1m 32s                │    │
│ │ ⚡ 1,247 rec/min          │ │ ⚡ 1,158 rec/min          │    │
│ │ 🎯 0 errors               │ │ 🎯 0 errors               │    │
│ └───────────────────────────┘ └───────────────────────────┘    │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

#### 4. **Wave Processing Status**
```
┌─────────────────────────────────────────────────────────────────┐
│ 🌊 WAVE PROCESSING STATUS                                       │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│ Wave 1: ✅ Dependencies Complete (65 batches, 2m 45s)          │
│ Wave 2: ✅ More Dependencies (120 batches, 4m 12s)             │
│ Wave 3: 🔄 Processing Customers (300 batches)                   │
│         ████████░░ 23/30 batches (76% complete)                │
│         ⚡ Batch processing: 847 records/min                    │
│         🔄 3 batches retrying (API rate limits)                 │
│         ⏱️ ETA: 8 minutes                                       │
│ Wave 4: ⏳ Queued (300 batches)                                │
│ Wave 5: ⏳ Queued (300 batches)                                │
│ ...                                                             │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

#### 5. **Performance Metrics Panel**
```
┌─────────────────────────────────────────────────────────────────┐
│ 📊 PERFORMANCE METRICS                                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│ 🚀 Batch Processing: ENABLED (10x faster)                      │
│ ⚡ Current Speed: 1,247 records/minute                          │
│ 🎯 Efficiency: 94% (vs 85% baseline)                           │
│ 🔧 Workers: 8 active (optimal configuration)                    │
│                                                                 │
│ 📈 Progress Trend:                                              │
│ [▁▂▃▅▆▇█▇▆▅] Steady processing                                   │
│                                                                 │
│ 🛠️ Error Recovery:                                              │
│ • API Rate Limits: 12 retries → 11 successful                  │
│ • Database Timeouts: 2 retries → 2 successful                  │
│ • Permanent Failures: 0                                        │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

#### 6. **Enhanced Error Display**
```
┌─────────────────────────────────────────────────────────────────┐
│ ⚠️ ERROR ANALYSIS                                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│ 🔄 Active Retries: 3 batches                                   │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Batch #247: API Rate Limit (Retry 2/3)                     │ │
│ │ • Next attempt in: 15 seconds                               │ │
│ │ • Records: 1,000 Customer records                           │ │
│ │ • Strategy: Exponential backoff                             │ │
│ └─────────────────────────────────────────────────────────────┘ │
│                                                                 │
│ ✅ Recovered Errors: 23 total                                  │
│ • API Timeouts: 15 → 14 successful retries                     │
│ • Concurrency Limits: 8 → 8 successful retries                │
│                                                                 │
│ 🚫 Permanent Failures: 0                                       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘
```

---

## Data Flow Architecture

### 🔄 Backend → Frontend Data Pipeline

```mermaid
graph TD
    A[Wave Coordination Tables] --> E[Import Metrics API]
    B[Job Status Tracker] --> E
    C[Dependency Resolver] --> E
    D[Performance Monitor] --> E

    E --> F[Enhanced Livewire Component]
    F --> G[Real-Time UI Updates]

    H[Frontend Polling Logic] --> I{Import Stage Detection}
    I -->|Initializing| J[Every 5 seconds]
    I -->|Processing| K[Every 2 seconds]
    I -->|Completing| L[Every 1 second]
    I -->|Complete/Failed| M[Stop polling]

    J --> E
    K --> E
    L --> E
```

### 📊 Key Data Endpoints

#### 1. **Enhanced Status API Endpoint**
```php
// Route: GET /api/import/enhanced-status/{jobId}
{
    "job_id": "68bc625ccb8904",
    "overall_status": "processing",
    "overall_progress": 78,
    "eta_minutes": 45,
    "performance_rating": 5,
    "processing_speed": 1247,

    "dependency_flow": {
        "levels": [
            {
                "level": 0,
                "name": "Dependencies",
                "record_types": [
                    {
                        "id": 15,
                        "name": "Subsidiaries",
                        "status": "completed",
                        "progress": 100,
                        "total_records": 45,
                        "processed_records": 45,
                        "duration_seconds": 135
                    }
                ]
            }
        ]
    },

    "wave_status": {
        "current_wave": 3,
        "total_waves": 8,
        "waves": [
            {
                "wave_number": 3,
                "status": "processing",
                "progress": 76,
                "total_batches": 30,
                "completed_batches": 23,
                "failed_batches": 0,
                "retrying_batches": 3,
                "eta_minutes": 8
            }
        ]
    },

    "record_types": [
        {
            "id": -3,
            "name": "Customers",
            "status": "processing",
            "progress": 63,
            "total_records": 4521,
            "processed_records": 2847,
            "current_wave": 3,
            "total_waves": 8,
            "processing_speed": 847,
            "error_count": 3,
            "retry_status": "api_rate_limits"
        }
    ],

    "performance_metrics": {
        "batch_processing_enabled": true,
        "current_speed_per_minute": 1247,
        "efficiency_percentage": 94,
        "active_workers": 8,
        "progress_trend": "steady",
        "optimization_factor": 10.2
    },

    "error_analysis": {
        "active_retries": 3,
        "retry_details": [
            {
                "batch_id": "batch_247",
                "error_category": "api_rate_limit",
                "retry_attempt": 2,
                "max_retries": 3,
                "next_attempt_seconds": 15,
                "records_count": 1000,
                "strategy": "exponential_backoff"
            }
        ],
        "recovered_errors": 23,
        "permanent_failures": 0
    }
}
```

#### 2. **Dependency Map API Endpoint**
```php
// Route: GET /api/import/dependency-map/{integrationId}/{recordTypeIds}
{
    "dependency_levels": [
        {
            "level": 0,
            "name": "Dependencies",
            "record_types": [
                {
                    "id": 15,
                    "name": "Subsidiaries",
                    "model_name": "Subsidiary",
                    "expected_records": 45,
                    "complexity": "low"
                }
            ]
        }
    ],
    "execution_order": [15, 12, 8, -3, -15],
    "total_record_types": 5,
    "estimated_total_records": 5786
}
```

---

## Implementation Strategy

### 🎯 **Technology Decision: React with Feature Flag Strategy**

Based on comprehensive analysis ([see FRONTEND_IMPLEMENTATION_ANALYSIS.md](FRONTEND_IMPLEMENTATION_ANALYSIS.md)), this enhanced import UI will be implemented using **React** with a **feature flag rollout strategy**.

**Key Rationale:**
- ✅ **Strategic Alignment**: Advances React migration goals
- ✅ **Technical Superiority**: Perfect fit for complex real-time UI requirements
- ✅ **Existing Infrastructure**: React 18.3.1 already available and working
- ✅ **Safety**: Feature flag provides fallback to existing Livewire implementation

### 🏗️ **React Component Architecture**

#### 1. **React Component Structure**
```
resources/js/enhanced-import/
├── components/
│   ├── ImportDashboard.jsx            # Main dashboard wrapper
│   ├── ImportOverview.jsx             # Overview metrics panel
│   ├── DependencyFlowChart.jsx        # Interactive dependency visualization
│   ├── WaveProcessingStatus.jsx       # Real-time wave progress
│   ├── RecordTypeProgressCards.jsx    # Individual record type cards
│   ├── PerformanceMetricsPanel.jsx    # Performance monitoring
│   ├── ErrorAnalysisPanel.jsx         # Error categorization & recovery
│   └── AdaptivePollingManager.jsx     # Intelligent polling controller
├── hooks/
│   ├── useImportStatus.js             # Import status management
│   ├── useAdaptivePolling.js          # Stage-based polling logic
│   ├── useDependencyFlow.js           # Dependency data management
│   ├── usePerformanceMetrics.js       # Performance tracking
│   └── useErrorRecovery.js            # Error handling & recovery
├── services/
│   ├── enhancedImportAPI.js           # API service layer
│   ├── statusTracker.js               # Status tracking utilities
│   └── metricsCollector.js            # Performance metrics collection
├── utils/
│   ├── formatters.js                  # Data formatting utilities
│   ├── constants.js                   # Application constants
│   └── validators.js                  # Data validation helpers
└── enhanced-import.jsx                # Main app entry point
```

#### 2. **Scoped React Build Configuration**
```javascript
// vite.config.js - Scoped React for import feature only
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.scss',
                'resources/js/app.js',
                'resources/js/enhanced-import.jsx', // 🎯 SCOPED: React only for import
            ],
            refresh: true,
        }),
        react({
            // 🎯 SCOPED: Only process React for enhanced-import
            include: /enhanced-import/,
        }),
    ],
    optimizeDeps: {
        include: ['react', 'react-dom'], // Include for enhanced import only
    },
});
```

#### 3. **Documented React State Management**
```javascript
/**
 * Enhanced Import State Management
 *
 * Primary purpose: Manage complex import state across 6 UI components
 * Key requirements:
 * - Prevent duplicate imports for same dataset
 * - Periodic polling for status updates (user checks back periodically)
 * - Real-time state coordination between all 6 components
 * - Simple error handling with user-friendly messages
 */
function useEnhancedImportState(integrationId, recordTypeIds) {
    // 🎯 PRIMARY STATE: Import control and status
    const [importState, setImportState] = useState({
        // Import control (CRITICAL: prevent duplicates)
        canStartImport: true,
        activeJobId: null,
        isImportActive: false,

        // Status data for all 6 UI components
        jobStatus: null,           // Overview Dashboard
        dependencyMap: null,       // Dependency Flow Chart
        waveStatus: null,          // Wave Processing Status
        recordTypes: {},           // Record Type Progress Cards
        performanceMetrics: null,  // Performance Metrics Panel
        errorAnalysis: null,       // Error Analysis Panel

        // User experience state
        lastUpdated: null,
        isLoading: false,
        errorMessage: null
    });

    /**
     * 🎯 CRITICAL FUNCTION: Check if import can be started
     * Prevents duplicate imports for same integration/record type combination
     */
    const checkCanStartImport = useCallback(async () => {
        try {
            const response = await fetch('/api/v1/import/check-active', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    integration_id: integrationId,
                    record_type_ids: recordTypeIds
                })
            });

            const data = await response.json();

            setImportState(prev => ({
                ...prev,
                canStartImport: data.can_start,
                errorMessage: data.can_start ? null : data.message
            }));

        } catch (error) {
            setImportState(prev => ({
                ...prev,
                canStartImport: false,
                errorMessage: 'Unable to check import status. Please try again.'
            }));
        }
    }, [integrationId, recordTypeIds]);

    /**
     * Periodic status polling (user checks back periodically)
     * Polling frequency: 30 seconds (not real-time, user-controlled)
     */
    const refreshStatus = useCallback(async (jobId) => {
        if (!jobId) return;

        try {
            setImportState(prev => ({ ...prev, isLoading: true }));

            const response = await fetch(`/api/v1/import/status/${jobId}`);
            const statusData = await response.json();

            setImportState(prev => ({
                ...prev,
                isLoading: false,
                lastUpdated: new Date(),
                isImportActive: statusData.is_active,
                canStartImport: statusData.can_start_new_import,

                // Update all 6 UI components data
                jobStatus: statusData,
                dependencyMap: statusData.dependency_flow,
                waveStatus: statusData.wave_status,
                recordTypes: statusData.record_types,
                performanceMetrics: statusData.performance_metrics,
                errorAnalysis: statusData.error_analysis
            }));

        } catch (error) {
            setImportState(prev => ({
                ...prev,
                isLoading: false,
                errorMessage: 'Failed to load import status. Please try again.'
            }));
        }
    }, []);

    return {
        importState,
        checkCanStartImport,
        refreshStatus,
        clearError: () => setImportState(prev => ({ ...prev, errorMessage: null }))
    };
}
```

### 🚩 **Simplified Feature Flag Strategy: Direct Integration**

**Key Decision**: The enhanced React UI will be **the default interface when `chunked_import` is enabled**. The existing `chunked_import` feature flag in `suitex.feature_flags.tenant_ids` is sufficient to control both chunked imports and the React UI enhancement.

#### 1. **Use Existing HandlesChunkedImport Trait Directly**
```php
// No changes needed to existing trait - it already works perfectly
trait HandlesChunkedImport
{
    // ... existing methods work as-is ...

    public function useChunkedImport(): bool
    {
        // Existing implementation - checks chunked_import feature flag
        // and tenant_ids array - this is all we need!
    }
}
```

#### 2. **Enhanced ImportSync Component**
```php
// Update existing ImportSync Livewire component
class ImportSync extends Component
{
    use HandlesChunkedImport;

    // ... existing properties and methods preserved ...

    public function render()
    {
        // Simple: if chunked imports enabled, use React UI
        if ($this->useChunkedImport()) {
            return $this->renderReactView();
        }

        // Otherwise use legacy system
        return $this->renderLegacyView();
    }

    protected function renderReactView()
    {
        return view('imports.react-enhanced', [
            'integration' => $this->integration,
            'record' => $this->record,
            'recType' => $this->recType,
            'mapping' => $this->mapping,
            'groupimport' => $this->groupimport,
            'reactProps' => $this->getReactProps(),
            'fallbackUrl' => $this->getLegacyUrl()
        ]);
    }

    protected function renderLegacyView()
    {
        return view('livewire.integrations.syncing', [
            'useChunkedImport' => false, // Force legacy mode
        ]);
    }

    protected function getReactProps(): array
    {
        return [
            'integrationId' => $this->integration->id,
            'recordId' => $this->record?->id,
            'recordTypeId' => $this->recType?->id,
            'groupImport' => $this->groupimport,
            'group' => $this->group,
            'useChunkedImport' => true,
            'apiEndpoints' => [
                'status' => route('import.chunked.status'), // Use existing endpoints
                'start' => route('import.chunked'),         // Use existing endpoints
                'cancel' => route('import.chunked.cancel')  // Use existing endpoints
            ]
        ];
    }

    protected function getLegacyUrl(): string
    {
        // Build URL to legacy Livewire interface for fallback
        return route('import.sync.legacy', [
            'integration' => $this->integration->id,
            'record' => $this->record?->id
        ]);
    }
}
```

#### 3. **No Database Changes Required**
```sql
-- Existing feature flag works perfectly as-is:
-- chunked_import feature flag with tenant_ids array
-- No metadata, no additional columns needed

-- To enable React UI for a tenant, just add them to chunked_import:
UPDATE feature_flags
SET tenant_ids = JSON_ARRAY_APPEND(tenant_ids, '$', 4515181)
WHERE name = 'chunked_import';
```

#### 4. **Simple Route Strategy**
```php
// Use existing route patterns - no changes needed
Route::get('/integrations/{integration}/sync/{record?}', [ImportSync::class, 'show'])
    ->name('import.sync.show');

// Optional: Add legacy route for explicit fallback
Route::get('/integrations/{integration}/sync/{record}/legacy', [ImportSync::class, 'showLegacy'])
    ->name('import.sync.legacy');
```

#### 5. **Simplified Management**
```bash
# Use existing chunked import management
# React UI automatically enabled when chunked imports are enabled

# Enable chunked imports (and React UI) for tenant
php artisan feature:chunked-import enable --tenant=4515181

# Check status
php artisan feature:chunked-import status

# Disable for tenant (falls back to legacy import)
php artisan feature:chunked-import disable --tenant=4515181
```

### 🎯 **React Implementation Strategy**

#### **Phase-Based Implementation: All Components, Complete Rollout**

**Key Requirements Based on Clarifications:**

1. ✅ **Scoped React Build**: Import-specific React entry point to avoid global conflicts
2. ✅ **v1 Backend API**: New comprehensive status and dependency preview endpoints
3. ✅ **All 6 UI Components**: Full feature implementation for enhanced user experience
4. ✅ **Active Import Detection**: Prevent duplicate imports with real-time status checking
5. ✅ **Trusted Libraries**: @xyflow/react for dependency visualization (mature, well-supported)
6. ✅ **Documented State Management**: Complex React patterns with inline documentation
7. ✅ **Simple Error Handling**: User-friendly error messages (no complex fallbacks)
8. ✅ **Complete Rollout**: Enable for all chunked_import tenants immediately

#### **React Component Architecture (All 6 Components)**
```
resources/js/enhanced-import/
├── components/
│   ├── ImportOverviewDashboard.jsx      # Priority 1: Overall metrics & status
│   ├── RecordTypeProgressCards.jsx      # Priority 1: Individual progress cards
│   ├── WaveProcessingStatus.jsx         # Priority 2: Real-time wave progress
│   ├── PerformanceMetricsPanel.jsx      # Priority 2: Throughput & efficiency
│   ├── DependencyFlowChart.jsx          # Priority 3: Interactive dependency visualization
│   └── ErrorAnalysisPanel.jsx           # Priority 3: Error categorization & recovery
├── hooks/
│   ├── useImportState.js                # 🎯 PRIMARY: Prevents duplicate imports
│   ├── usePeriodicPolling.js            # User-controlled refresh intervals
│   ├── useActiveImportDetection.js      # Detects when imports are running
│   └── useErrorHandling.js              # Simple error message display
├── services/
│   ├── importStatusAPI.js               # v1 API integration
│   └── dependencyPreviewAPI.js          # Dependency map fetching
└── enhanced-import.jsx                  # Scoped entry point
```

---

## Backend API Requirements

### 🔌 **v1 Backend API Endpoints (New)**

#### 1. **Enhanced Import Status Service v1**
```php
// src/App/Services/ImportJobs/v1/EnhancedImportStatusService.php

class EnhancedImportStatusService
{
    /**
     * Get comprehensive import status for React UI
     *
     * @param string $jobId Import job identifier
     * @return array Complete status including active state detection
     */
    public function getEnhancedStatus(string $jobId): array
    {
        return [
            'job_id' => $jobId,
            'is_active' => $this->isImportActive($jobId), // 🎯 CRITICAL: Prevent duplicates
            'overall_status' => $this->calculateOverallStatus($jobId),
            'overall_progress' => $this->calculateOverallProgress($jobId),
            'eta_minutes' => $this->calculateETA($jobId),
            'can_start_new_import' => !$this->hasActiveImportForDataset($jobId),

            // All 6 UI components data
            'dependency_flow' => $this->getDependencyFlow($jobId),
            'wave_status' => $this->getWaveStatus($jobId),
            'record_types' => $this->getRecordTypeProgress($jobId),
            'performance_metrics' => $this->getPerformanceMetrics($jobId),
            'error_analysis' => $this->getErrorAnalysis($jobId),

            'last_updated' => now()->toISOString()
        ];
    }

    /**
     * 🎯 CRITICAL: Detect if import is actively running
     * Prevents duplicate imports for same dataset
     */
    private function isImportActive(string $jobId): bool
    {
        return DB::connection('tenant_connection')
            ->table('sync_status')
            ->where('job_id', $jobId)
            ->whereIn('status', ['pending', 'processing', 'Importing Dependencies'])
            ->exists();
    }

    /**
     * Check if any import is running for this integration/record type combination
     */
    private function hasActiveImportForDataset(string $currentJobId): bool
    {
        // Implementation to prevent duplicate imports for same dataset
    }
}
```

#### 2. **Dependency Preview Service v1**
```php
// src/App/Services/ImportJobs/v1/DependencyPreviewService.php

class DependencyPreviewService
{
    /**
     * Get dependency map before import starts
     * Used for "What will be imported?" preview
     */
    public function getDependencyMap(int $integrationId, array $recordTypeIds): array
    {
        $dependencyResolver = app(DependencyResolver::class);

        return [
            'integration_id' => $integrationId,
            'dependency_levels' => $dependencyResolver->buildDependencyGraph($recordTypeIds),
            'execution_order' => $dependencyResolver->getExecutionOrder(),
            'estimated_total_records' => $this->estimateRecordCounts($integrationId, $recordTypeIds),
            'estimated_duration_minutes' => $this->estimateImportDuration($recordTypeIds),
            'complexity_score' => $this->calculateComplexityScore($recordTypeIds)
        ];
    }
}
```

#### 3. **Enhanced Import Controller v1**
```php
// src/App/Http/Controllers/Api/v1/EnhancedImportController.php

class EnhancedImportController extends Controller
{
    /**
     * GET /api/v1/import/status/{jobId}
     * Primary endpoint for React UI polling
     */
    public function getStatus(string $jobId)
    {
        $statusService = app(EnhancedImportStatusService::class);
        return response()->json($statusService->getEnhancedStatus($jobId));
    }

    /**
     * GET /api/v1/import/preview
     * Get dependency preview before starting import
     */
    public function getPreview(Request $request)
    {
        $request->validate([
            'integration_id' => 'required|integer',
            'record_type_ids' => 'required|array'
        ]);

        $previewService = app(DependencyPreviewService::class);
        return response()->json($previewService->getDependencyMap(
            $request->integration_id,
            $request->record_type_ids
        ));
    }

    /**
     * POST /api/v1/import/check-active
     * Check if import can be started (no active imports for dataset)
     */
    public function checkCanStart(Request $request)
    {
        $request->validate([
            'integration_id' => 'required|integer',
            'record_type_ids' => 'required|array'
        ]);

        $canStart = !$this->hasActiveImportForDataset(
            $request->integration_id,
            $request->record_type_ids
        );

        return response()->json([
            'can_start' => $canStart,
            'message' => $canStart
                ? 'Import can be started'
                : 'Another import is already running for this dataset'
        ]);
    }
}
```

---

## Error Handling & Edge Cases

### 🚨 **Simplified Error Handling Strategy**

**Requirement**: Simple user-friendly error messages only (no complex fallbacks)

#### 1. **Import State Conflicts**
```javascript
// Primary error scenario: Prevent duplicate imports
function useActiveImportDetection(integrationId, recordTypeIds) {
    const [canStartImport, setCanStartImport] = useState(true);
    const [errorMessage, setErrorMessage] = useState(null);

    const checkCanStart = async () => {
        try {
            const response = await fetch('/api/v1/import/check-active', {
                method: 'POST',
                body: JSON.stringify({ integration_id: integrationId, record_type_ids: recordTypeIds })
            });

            const data = await response.json();

            if (!data.can_start) {
                setCanStartImport(false);
                setErrorMessage('Another import is already running for this dataset. Please wait for it to complete.');
            } else {
                setCanStartImport(true);
                setErrorMessage(null);
            }
        } catch (error) {
            setErrorMessage('Unable to check import status. Please try again.');
        }
    };

    return { canStartImport, errorMessage, checkCanStart };
}
```

#### 2. **API Communication Errors**
```javascript
// Simple error display for API failures
function useErrorHandling() {
    const [error, setError] = useState(null);

    const handleError = (error, context = '') => {
        console.error(`Import UI Error ${context}:`, error);

        // Simple user-friendly messages
        const userMessage = getUserFriendlyMessage(error);
        setError(userMessage);

        // Auto-clear error after 10 seconds
        setTimeout(() => setError(null), 10000);
    };

    const getUserFriendlyMessage = (error) => {
        if (error.message?.includes('Network')) {
            return 'Network connection issue. Please check your internet connection and try again.';
        }
        if (error.status === 500) {
            return 'Server error occurred. Please try again in a few minutes.';
        }
        return 'Something went wrong. Please refresh the page and try again.';
    };

    return { error, handleError, clearError: () => setError(null) };
}
```

#### 3. **Error Display Component**
```javascript
// Simple error message display
function ErrorMessage({ error, onDismiss }) {
    if (!error) return null;

    return (
        <div className="bg-red-50 border border-red-200 rounded-md p-4 mb-4">
            <div className="flex">
                <div className="flex-shrink-0">
                    <svg className="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                        <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
                    </svg>
                </div>
                <div className="ml-3">
                    <p className="text-sm text-red-700">{error}</p>
                </div>
                {onDismiss && (
                    <div className="ml-auto">
                        <button onClick={onDismiss} className="text-red-400 hover:text-red-600">
                            <span className="sr-only">Dismiss</span>
                            ×
                        </button>
                    </div>
                )}
            </div>
        </div>
    );
}
```

### 🔧 **No Fallback Strategy**

**Simplified Approach**: If React UI fails, show error message to user
- ❌ No automatic fallback to Livewire
- ❌ No complex graceful degradation
- ✅ Simple error messages with manual refresh option
- ✅ Clear user guidance on what to do next

---

## Performance Considerations

### ⚡ Optimization Strategies

#### 1. **Smart Caching**
```php
// Cache expensive operations
$cacheKey = "import_status_{$jobId}";
$ttl = $this->determineCacheTTL($jobStatus);

return Cache::remember($cacheKey, $ttl, function() use ($jobId) {
    return $this->calculateComplexMetrics($jobId);
});
```

#### 2. **Adaptive Polling**
```javascript
// Reduce polling when appropriate
const getPollingInterval = (status) => {
    switch (status) {
        case 'initializing': return 5000;
        case 'processing': return 2000;
        case 'completing': return 1000;
        default: return null; // Stop polling
    }
};
```

#### 3. **Efficient Data Queries**
```sql
-- Optimized wave status query
SELECT
    w.wave_number,
    w.status,
    w.total_batches,
    w.completed_batches,
    COUNT(b.id) as batch_details_count,
    AVG(b.processing_speed) as avg_speed
FROM wave_coordination w
LEFT JOIN wave_batches b ON w.job_id = b.job_id AND w.wave_number = b.wave_number
WHERE w.job_id = ?
GROUP BY w.wave_number
ORDER BY w.wave_number;
```

---

## Testing Strategy

### 🧪 **React Testing Infrastructure Setup**

**Current State**: No React testing infrastructure exists
**Recommendation**: Set up comprehensive React testing with Jest + React Testing Library

#### 1. **Testing Dependencies Installation**
```json
// package.json additions
{
  "devDependencies": {
    "@testing-library/react": "^14.1.0",
    "@testing-library/jest-dom": "^6.1.0",
    "@testing-library/user-event": "^14.5.0",
    "jest": "^29.7.0",
    "jest-environment-jsdom": "^29.7.0",
    "@babel/preset-react": "^7.23.0"
  }
}
```

#### 2. **Jest Configuration**
```javascript
// jest.config.js (new file)
module.exports = {
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
  moduleNameMapping: {
    '^@/(.*)$': '<rootDir>/resources/js/$1',
  },
  transform: {
    '^.+\\.(js|jsx)$': 'babel-jest',
  },
  moduleFileExtensions: ['js', 'jsx'],
  testMatch: [
    '<rootDir>/tests/React/**/*.test.{js,jsx}',
  ],
  collectCoverageFrom: [
    'resources/js/enhanced-import/**/*.{js,jsx}',
  ],
};
```

#### 3. **React Component Testing Examples**
```javascript
// tests/React/ImportDashboard.test.jsx
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ImportDashboard } from '../../resources/js/enhanced-import/components/ImportDashboard';

/**
 * Test suite for ImportDashboard component
 * Tests all 6 UI components integration and active import detection
 */
describe('ImportDashboard', () => {
    test('displays all 6 UI components correctly', async () => {
        const mockProps = {
            jobId: 'test-job-123',
            integrationId: 1,
            canStartImport: true
        };

        render(<ImportDashboard {...mockProps} />);

        // Verify all 6 components are rendered
        expect(screen.getByTestId('import-overview')).toBeInTheDocument();
        expect(screen.getByTestId('record-type-progress')).toBeInTheDocument();
        expect(screen.getByTestId('wave-processing')).toBeInTheDocument();
        expect(screen.getByTestId('performance-metrics')).toBeInTheDocument();
        expect(screen.getByTestId('dependency-flow')).toBeInTheDocument();
        expect(screen.getByTestId('error-analysis')).toBeInTheDocument();
    });

    test('prevents duplicate imports when active import detected', async () => {
        const mockProps = {
            jobId: 'test-job-123',
            integrationId: 1,
            canStartImport: false,
            errorMessage: 'Another import is already running for this dataset.'
        };

        render(<ImportDashboard {...mockProps} />);

        expect(screen.getByText(/another import is already running/i)).toBeInTheDocument();
        expect(screen.getByRole('button', { name: /start import/i })).toBeDisabled();
    });

    test('handles periodic polling for status updates', async () => {
        const mockFetch = jest.fn().mockResolvedValue({
            json: () => Promise.resolve({ is_active: true, overall_progress: 45 })
        });
        global.fetch = mockFetch;

        render(<ImportDashboard jobId="test-job-123" />);

        // Should poll status every 30 seconds (periodic checking requirement)
        await waitFor(() => {
            expect(mockFetch).toHaveBeenCalledWith('/api/v1/import/status/test-job-123');
        });
    });
});
```

#### 4. **Testing Setup File**
```javascript
// tests/setup.js
import '@testing-library/jest-dom';

// Mock fetch for API calls
global.fetch = jest.fn();

// Mock ResizeObserver for @xyflow/react dependency visualization
global.ResizeObserver = jest.fn().mockImplementation(() => ({
  observe: jest.fn(),
  unobserve: jest.fn(),
  disconnect: jest.fn(),
}));

// Reset mocks after each test
afterEach(() => {
  jest.clearAllMocks();
});
```

#### 2. **React Hooks Testing**
```javascript
// Custom hooks testing
import { renderHook, act } from '@testing-library/react';
import { useAdaptivePolling } from '../hooks/useAdaptivePolling';

describe('useAdaptivePolling', () => {
    test('adjusts polling interval based on status', () => {
        const { result } = renderHook(() => useAdaptivePolling());

        act(() => {
            result.current.updateStatus('processing');
        });

        expect(result.current.pollingInterval).toBe(2000);

        act(() => {
            result.current.updateStatus('completed');
        });

        expect(result.current.pollingInterval).toBe(null);
    });
});
```

#### 3. **API Integration Testing**
```php
// Backend API tests for React integration
class EnhancedImportAPITest extends TestCase
{
    /** @test */
    public function it_returns_react_compatible_status_data()
    {
        $response = $this->getJson('/api/import/enhanced-status/test-job-123');

        $response->assertStatus(200)
            ->assertJsonStructure([
                'job_id',
                'overall_status',
                'dependency_flow' => [
                    'levels' => [
                        '*' => [
                            'level',
                            'name',
                            'record_types' => [
                                '*' => ['id', 'name', 'status', 'progress']
                            ]
                        ]
                    ]
                ],
                'wave_status',
                'performance_metrics',
                'error_analysis'
            ]);
    }

    /** @test */
    public function it_handles_chunked_import_react_ui_routing()
    {
        // Enable chunked imports (which enables React UI)
        $this->enableChunkedImportForTenant($this->tenantId);

        $response = $this->get('/import/123');

        $response->assertViewIs('imports.react-enhanced');
        $response->assertViewHas(['reactProps', 'fallbackUrl']);
    }
}
```

#### 4. **End-to-End Testing**
```javascript
// E2E testing with Playwright or Cypress
describe('Enhanced Import UI E2E', () => {
    test('complete import flow with React UI', async () => {
        await page.goto('/import/123');

        // Verify React UI loads
        await expect(page.locator('#enhanced-import-root')).toBeVisible();

        // Test dependency visualization interaction
        await page.click('[data-testid="dependency-flow-chart"]');
        await expect(page.locator('[data-testid="dependency-details"]')).toBeVisible();

        // Test real-time updates
        await page.waitForSelector('[data-testid="wave-progress"]');
        await expect(page.locator('[data-testid="wave-progress"]')).toContainText('Processing');

        // Test error handling
        if (await page.locator('[data-testid="error-panel"]').isVisible()) {
            await expect(page.locator('[data-testid="retry-status"]')).toBeVisible();
        }
    });

    test('fallback to Livewire when React fails', async () => {
        // Simulate React failure
        await page.route('**/enhanced-import.jsx', route => route.abort());

        await page.goto('/import/123');

        // Should redirect to Livewire fallback
        await page.waitForURL('**/sync/index');
        await expect(page.locator('livewire\\:integrations\\.syncing')).toBeVisible();
    });
});
```

---

## Success Metrics

### 📊 **User Experience Success Metrics**

**Primary Goal**: Enhanced User Experience (defined by requirements)

#### 1. **User Experience Metrics** (Primary Focus)
- **Import Visibility**: Users can see what records will be imported before starting (target: 100%)
- **Active Import Detection**: Users cannot start duplicate imports (target: 100% prevention)
- **Status Clarity**: Users understand current import progress across all record types (target: >95%)
- **Error Understanding**: Users receive clear, actionable error messages (target: >90% clarity)

#### 2. **Import Control Metrics** (Critical Requirements)
- **Duplicate Prevention**: No simultaneous imports for same dataset (target: 100%)
- **Periodic Update Success**: Status updates work when users check back (target: >99%)
- **All Component Functionality**: 6 UI components display relevant data (target: 100%)
- **Legacy Preservation**: Existing import functionality unaffected (target: 100%)

#### 3. **Technical Delivery Metrics** (Phase-Based ASAP)
- **4-Phase Delivery**: Complete implementation through all phase gates (target: 100%)
- **Complete Rollout**: All chunked_import tenants using React UI (target: 100%)
- **Testing Infrastructure**: React testing setup functional (target: 100%)
- **API Performance**: v1 endpoints responsive for React UI (target: <1s response)

---

## Implementation Strategy

### 📅 **Phase-Based Implementation Strategy**

**Primary Goal: Complete rollout ASAP while preserving legacy system**
**Success Metric: Enhanced User Experience**

#### **Phase 1: Foundation & Infrastructure**
**Deliverables:**
- Scoped React build pipeline (import-specific entry point only)
- ImportSync component updated with chunked_import feature flag integration
- v1 enhanced backend API endpoints (comprehensive status, dependency preview, active import detection)

**Completion Criteria:**
- React builds successfully in isolation without global conflicts
- ImportSync component renders React view when chunked_import enabled
- All v1 API endpoints functional and returning proper data structures
- Backend can detect active imports and prevent duplicates

#### **Phase 2: Core UI Components Implementation**
**Deliverables:**
- Import Overview Dashboard + Record Type Progress Cards
- Wave Processing Status + Performance Metrics Panel
- Dependency Flow Visualization (using @xyflow/react)
- Error Analysis Panel + basic import state management
- Integration between all components and backend APIs

**Completion Criteria:**
- All 6 UI components render correctly with mock data
- Components successfully fetch and display real backend data
- Basic periodic polling (30-second intervals) functional
- Component integration and state sharing working

#### **Phase 3: Advanced State Management & Import Control**
**Deliverables:**
- Complex React state management with comprehensive documentation
- Active import detection and duplicate prevention system
- Error handling with user-friendly error messages
- UI polish, responsive design, and user experience refinements

**Completion Criteria:**
- Cannot start duplicate imports (100% prevention working)
- State management documented with clear purpose and patterns
- Error messages are user-friendly and actionable
- UI is responsive and polished across all device sizes
- All 6 components work seamlessly together

#### **Phase 4: Testing Infrastructure & Production Rollout**
**Deliverables:**
- React testing infrastructure setup (Jest + React Testing Library)
- Comprehensive component and integration tests
- Performance optimization and production preparation
- Complete rollout for all chunked_import enabled tenants

**Completion Criteria:**
- Testing infrastructure functional with example tests passing
- Key components have test coverage for critical functionality
- Performance optimizations applied (React.memo, etc.)
- All chunked_import tenants successfully using React UI
- Legacy system preserved and unaffected

### 🎯 **Phase Completion Gates**
- **Phase 1 Gate**: Backend APIs working + React infrastructure ready for development
- **Phase 2 Gate**: All 6 UI components functional with real backend data integration
- **Phase 3 Gate**: Import state management prevents duplicates + error handling complete
- **Phase 4 Gate**: Production ready with testing infrastructure + complete rollout successful

---

## Risk Mitigation

### ⚠️ **Phase-Based Implementation Risks**

#### 1. **Phase Dependencies**
- **Risk**: Later phases blocked by earlier phase completion
- **Mitigation**: Clear completion criteria for each phase gate
- **Contingency**: Phase 1 & 2 are critical - Phase 3 & 4 scope can be adjusted if needed

#### 2. **No Existing React Testing Infrastructure**
- **Risk**: Testing setup delays final rollout
- **Mitigation**: Testing infrastructure setup in Phase 4 (parallel with production prep)
- **Contingency**: Manual testing acceptable for initial rollout

#### 3. **Complex State Management Documentation**
- **Risk**: Complex React patterns may be hard to maintain
- **Mitigation**: Comprehensive inline documentation with clear purpose statements during Phase 3
- **Contingency**: State management can be simplified if needed

### 🛡️ **Technical Risks**

#### 1. **Scoped React Build Configuration**
- **Risk**: React conflicts with existing JavaScript
- **Mitigation**: Scoped React plugin configuration, isolated entry point
- **Contingency**: If conflicts occur, further isolate React bundle

#### 2. **Active Import Detection**
- **Risk**: Duplicate import prevention fails
- **Mitigation**: Backend validation with database-level checks
- **Contingency**: UI warnings + manual user confirmation

#### 3. **Dependency Visualization Complexity (@xyflow/react)**
- **Risk**: Complex dependency flows overwhelm users
- **Mitigation**: Simple, clear visual design with progressive disclosure
- **Contingency**: Fallback to simple list view if visualization issues occur

### 🚀 **Simplified Risk Strategy**

**No Complex Fallbacks**: Simple error messages only
- ✅ **Risk**: React UI fails → Show error message, manual refresh
- ✅ **Risk**: API fails → User-friendly error message
- ✅ **Risk**: Performance issues → Accept degraded performance initially

**Focus on Core Success**: Enhanced User Experience
- 🎯 **Primary**: All 6 components working and informative
- 🎯 **Secondary**: Prevent duplicate imports
- 🎯 **Tertiary**: Performance optimization

### 📊 **Trusted React Libraries**

**Selected Libraries** (mature, well-supported):
```json
{
  "@xyflow/react": "^12.3.6",          // Dependency visualization (5+ years, active)
  "@headlessui/react": "^2.2.0",       // UI components (already installed, Tailwind team)
  "react": "^18.3.1",                  // Core React (already installed, stable)
  "react-dom": "^18.3.1"               // Core React DOM (already installed, stable)
}
```

**Library Selection Criteria**:
- ✅ **@xyflow/react**: 1M+ weekly downloads, actively maintained, perfect for dependency visualization
- ✅ **@headlessui/react**: Already in project, Tailwind team support, excellent accessibility
- ✅ **React 18**: Current stable, already working in project
- ❌ **No additional state management libraries**: useState + useContext sufficient for this scope

---

## Future Enhancements

### 🚀 Phase 4+ Roadmap

#### 1. **AI-Powered Insights**
- Predictive failure detection
- Automatic optimization recommendations
- Intelligent scheduling suggestions

#### 2. **Advanced Visualizations**
- Interactive dependency graphs
- Real-time performance charts
- Historical trend analysis

#### 3. **Integration Ecosystem**
- Webhook notifications for import events
- API access for external monitoring
- Dashboard widgets for executive reporting

---

## Conclusion

This enhanced import UI design delivers **maximum user experience improvement** through a comprehensive React-based interface that provides complete visibility into NetSuite import processes. The design addresses all clarified requirements for ASAP delivery while preserving legacy functionality.

### 🎯 **Key Design Decisions Based on Requirements**

1. **✅ Scoped React Implementation**: Import-specific React entry point avoids global conflicts
2. **✅ v1 Backend API**: New comprehensive status and dependency preview endpoints
3. **✅ All 6 UI Components**: Complete feature implementation for enhanced visibility
4. **✅ Active Import Detection**: Robust duplicate prevention with database validation
5. **✅ Simplified Error Handling**: User-friendly messages with manual refresh approach
6. **✅ Complete Rollout**: All chunked_import tenants get React UI immediately
7. **✅ 4-Phase ASAP Implementation**: Clear deliverables with critical completion gates

### 🏆 **Expected User Experience Impact**

**Primary Success Metric: Enhanced User Experience**
- 🔍 **Complete Import Visibility**: Users see all record types, dependencies, and progress
- 🛡️ **Duplicate Prevention**: Cannot start conflicting imports for same dataset
- 📊 **Real-Time Status**: All 6 UI components provide comprehensive import monitoring
- ⚡ **Periodic Updates**: Users can check back periodically for status updates
- 🎯 **Error Clarity**: Simple, actionable error messages replace confusing system errors

### 📅 **Delivery Commitment**
- **Phase 1**: Foundation + v1 Backend APIs ready
- **Phase 2**: All 6 UI components functional
- **Phase 3**: Import state management + error handling complete
- **Phase 4**: React testing infrastructure + complete rollout

### 🎨 **Strategic Value**
This implementation establishes React as a proven technology choice for complex UI requirements while delivering immediate user experience improvements. The scoped approach minimizes risk while maximizing strategic advancement toward React adoption.

**The enhanced import UI transforms import monitoring from basic progress bars to comprehensive operational intelligence, directly addressing user needs for clarity and control.**

---

**Document Maintainer**: Development Team
**Implementation Approach**: 4-phase ASAP delivery
**Primary Success Metric**: User Experience Enhancement
**Related Documents**: WAVE_COORDINATION_DESIGN.md, CHUNKED_IMPORT_README.md, ai_rules.md, ai_tests.md
