import { useState, useImperativeHandle, forwardRef, useMemo, useCallback, useRef, useEffect } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
import { AlertCircle } from 'lucide-react';
import { Card, CardContent } from '@/components/ui/card';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { TooltipProvider } from '@/components/ui/tooltip';
import { useToast } from '@/hooks/use-toast';
import { useUserRole } from '@/hooks/useUserRole';
import { useAuth } from '@/hooks/useAuth';
import { getErrorMessage } from '@/lib/errorHandler';
import { PhotoModal } from './PhotoModal';
import { SubmissionReviewManager } from './SubmissionReviewManager';
import { ItemEditDialog } from './ItemEditDialog';
import { ItemSelectorDialog } from './ItemSelectorDialog';
import { useIsMobile } from '@/hooks/use-mobile';
import { useAdminSettings } from '@/hooks/useAdminSettings';
import { useModerationQueueManager } from '@/hooks/moderation';
import { QueueItem } from './QueueItem';
import { ModerationErrorBoundary } from '@/components/error/ModerationErrorBoundary';
import { QueueSkeleton } from './QueueSkeleton';
import { EnhancedLockStatusDisplay } from './EnhancedLockStatusDisplay';
import { getLockStatus } from '@/lib/moderation/lockHelpers';
import { QueueStats } from './QueueStats';
import { QueueFilters } from './QueueFilters';
import { ActiveFiltersDisplay } from './ActiveFiltersDisplay';
import { AutoRefreshIndicator } from './AutoRefreshIndicator';
import { NewItemsAlert } from './NewItemsAlert';
import { EnhancedEmptyState } from './EnhancedEmptyState';
import { QueuePagination } from './QueuePagination';
import { ConfirmationDialog } from './ConfirmationDialog';
import { KeyboardShortcutsHelp } from './KeyboardShortcutsHelp';
import { useKeyboardShortcuts } from '@/hooks/useKeyboardShortcuts';
import { fetchSubmissionItems, type SubmissionItemWithDeps } from '@/lib/submissionItemsService';
import type { ModerationQueueRef, ModerationItem } from '@/types/moderation';
import type { PhotoItem } from '@/types/photos';

interface ModerationQueueProps {
  optimisticallyUpdateStats?: (delta: Partial<{ pendingSubmissions: number; openReports: number; flaggedContent: number }>) => void;
}

export const ModerationQueue = forwardRef<ModerationQueueRef, ModerationQueueProps>((props, ref) => {
  const { optimisticallyUpdateStats } = props;
  const isMobile = useIsMobile();
  const { user } = useAuth();
  const { toast } = useToast();
  const { isAdmin, isSuperuser } = useUserRole();
  const adminSettings = useAdminSettings();
  
  // Extract settings values to stable primitives for memoization
  const refreshMode = adminSettings.getAdminPanelRefreshMode();
  const pollInterval = adminSettings.getAdminPanelPollInterval();
  const refreshStrategy = adminSettings.getAutoRefreshStrategy();
  const preserveInteraction = adminSettings.getPreserveInteractionState();
  const useRealtimeQueue = adminSettings.getUseRealtimeQueue();
  
  // Memoize settings object using stable primitive dependencies
  const settings = useMemo(() => ({
    refreshMode,
    pollInterval,
    refreshStrategy,
    preserveInteraction,
    useRealtimeQueue,
  }), [refreshMode, pollInterval, refreshStrategy, preserveInteraction, useRealtimeQueue]);
  
  // Initialize queue manager (replaces all state management, fetchItems, effects)
  const queueManager = useModerationQueueManager({
    user,
    isAdmin: isAdmin(),
    isSuperuser: isSuperuser(),
    toast,
    optimisticallyUpdateStats,
    settings,
  });
  
  // UI-only state
  const [notes, setNotes] = useState<Record<string, string>>({});
  const [photoModalOpen, setPhotoModalOpen] = useState(false);
  const [selectedPhotos, setSelectedPhotos] = useState<PhotoItem[]>([]);
  const [selectedPhotoIndex, setSelectedPhotoIndex] = useState(0);
  const [reviewManagerOpen, setReviewManagerOpen] = useState(false);
  const [selectedSubmissionId, setSelectedSubmissionId] = useState<string | null>(null);
  const [showItemEditDialog, setShowItemEditDialog] = useState(false);
  const [editingItem, setEditingItem] = useState<SubmissionItemWithDeps | null>(null);
  const [showItemSelector, setShowItemSelector] = useState(false);
  const [availableItems, setAvailableItems] = useState<SubmissionItemWithDeps[]>([]);
  const [bulkEditMode, setBulkEditMode] = useState(false);
  const [bulkEditItems, setBulkEditItems] = useState<SubmissionItemWithDeps[]>([]);
  
  // Confirmation dialog state
  const [confirmDialog, setConfirmDialog] = useState<{
    open: boolean;
    title: string;
    description: string;
    onConfirm: () => void;
  }>({
    open: false,
    title: '',
    description: '',
    onConfirm: () => {},
  });
  
  // Keyboard shortcuts help dialog
  const [showShortcutsHelp, setShowShortcutsHelp] = useState(false);
  
  // Offline detection state
  const [isOffline, setIsOffline] = useState(!navigator.onLine);
  
  // Offline detection effect
  useEffect(() => {
    const handleOnline = () => {
      setIsOffline(false);
      toast({
        title: 'Connection Restored',
        description: 'You are back online. Refreshing queue...',
      });
      queueManager.refresh();
    };
    
    const handleOffline = () => {
      setIsOffline(true);
    };
    
    window.addEventListener('online', handleOnline);
    window.addEventListener('offline', handleOffline);
    
    return () => {
      window.removeEventListener('online', handleOnline);
      window.removeEventListener('offline', handleOffline);
    };
  }, [queueManager, toast]);
  
  // Virtual scrolling setup
  const parentRef = useRef<HTMLDivElement>(null);
  const virtualizer = useVirtualizer({
    count: queueManager.items.length,
    getScrollElement: () => parentRef.current,
    estimateSize: () => 420, // Estimated average height of QueueItem (card + spacing)
    overscan: 3, // Render 3 items above/below viewport for smoother scrolling
    enabled: queueManager.items.length > 10, // Only enable virtual scrolling for 10+ items
  });
  
  // UI-specific handlers
  const handleNoteChange = (id: string, value: string) => {
    setNotes(prev => ({ ...prev, [id]: value }));
  };
  
  // Wrapped delete with confirmation
  const handleDeleteSubmission = useCallback((item: ModerationItem) => {
    setConfirmDialog({
      open: true,
      title: 'Delete Submission',
      description: 'Are you sure you want to permanently delete this submission? This action cannot be undone.',
      onConfirm: () => queueManager.deleteSubmission(item),
    });
  }, [queueManager]);
  
  // Clear filters handler
  const handleClearFilters = useCallback(() => {
    queueManager.filters.clearFilters();
  }, [queueManager.filters]);
  
  // Keyboard shortcuts
  const { shortcuts } = useKeyboardShortcuts({
    shortcuts: [
      {
        key: '?',
        handler: () => setShowShortcutsHelp(true),
        description: 'Show keyboard shortcuts',
      },
      {
        key: 'r',
        handler: () => queueManager.refresh(),
        description: 'Refresh queue',
      },
      {
        key: 'k',
        ctrlOrCmd: true,
        handler: () => {
          // Focus search/filter (if implemented)
          document.querySelector<HTMLInputElement>('[data-filter-search]')?.focus();
        },
        description: 'Focus filters',
      },
      {
        key: 'e',
        handler: () => {
          // Edit first claimed submission
          const claimedItem = queueManager.items.find(item => 
            queueManager.queue.isLockedByMe(item.id, item.assigned_to, item.locked_until)
          );
          if (claimedItem) {
            handleOpenItemEditor(claimedItem.id);
          }
        },
        description: 'Edit claimed submission',
      },
    ],
    enabled: true,
  });
  
  const handleOpenPhotos = (photos: PhotoItem[], index: number) => {
    setSelectedPhotos(photos);
    setSelectedPhotoIndex(index);
    setPhotoModalOpen(true);
  };
  
  const handleOpenReviewManager = (submissionId: string) => {
    setSelectedSubmissionId(submissionId);
    setReviewManagerOpen(true);
  };
  
  const handleOpenItemEditor = async (submissionId: string) => {
    try {
      const items = await fetchSubmissionItems(submissionId);
      
      if (!items || items.length === 0) {
        toast({
          title: 'No Items Found',
          description: 'This submission has no items to edit',
          variant: 'destructive',
        });
        return;
      }
      
      // Phase 3: Multi-item selector for submissions with multiple items
      if (items.length > 1) {
        setAvailableItems(items);
        setShowItemSelector(true);
      } else {
        // Single item - edit directly
        setEditingItem(items[0]);
        setShowItemEditDialog(true);
      }
    } catch (error: unknown) {
      toast({
        title: 'Error',
        description: getErrorMessage(error),
        variant: 'destructive',
      });
    }
  };
  
  const handleSelectItem = (item: SubmissionItemWithDeps) => {
    setEditingItem(item);
    setShowItemSelector(false);
    setShowItemEditDialog(true);
  };
  
  const handleBulkEdit = async (submissionId: string) => {
    try {
      const items = await fetchSubmissionItems(submissionId);
      
      if (!items || items.length === 0) {
        toast({
          title: 'No Items Found',
          description: 'This submission has no items to edit',
          variant: 'destructive',
        });
        return;
      }
      
      setBulkEditItems(items);
      setBulkEditMode(true);
      setShowItemEditDialog(true);
    } catch (error: unknown) {
      toast({
        title: 'Error',
        description: getErrorMessage(error),
        variant: 'destructive',
      });
    }
  };
  
  // Expose imperative API
  useImperativeHandle(ref, () => ({
    refresh: async () => {
      await queueManager.refresh();
    }
  }));
  
  return (
    <div className="space-y-4">
      {/* Offline Banner */}
      {isOffline && (
        <Alert variant="destructive">
          <AlertCircle className="h-4 w-4" />
          <AlertTitle>No Internet Connection</AlertTitle>
          <AlertDescription>
            You're offline. The moderation queue will automatically sync when your connection is restored.
          </AlertDescription>
        </Alert>
      )}
      
      {/* Queue Statistics & Lock Status */}
      {queueManager.queue.queueStats && (
        <Card className="bg-gradient-to-r from-primary/5 to-primary/10 border-primary/20">
          <CardContent className="p-4">
            <div className="flex flex-col sm:flex-row gap-4 items-start sm:items-center justify-between">
              <QueueStats stats={queueManager.queue.queueStats} isMobile={isMobile} />
              <EnhancedLockStatusDisplay
                currentLock={queueManager.queue.currentLock}
                queueStats={queueManager.queue.queueStats}
                loading={queueManager.queue.isLoading}
                onExtendLock={() => queueManager.queue.extendLock(queueManager.queue.currentLock?.submissionId || '')}
                onReleaseLock={() => queueManager.queue.releaseLock(queueManager.queue.currentLock?.submissionId || '', false)}
                getCurrentTime={() => new Date()}
              />
            </div>
          </CardContent>
        </Card>
      )}
      
      {/* Filter Bar */}
        <QueueFilters
          activeEntityFilter={queueManager.filters.entityFilter}
          activeStatusFilter={queueManager.filters.statusFilter}
          sortConfig={queueManager.filters.sortConfig}
          isMobile={isMobile ?? false}
          isLoading={queueManager.loadingState === 'loading'}
          onEntityFilterChange={queueManager.filters.setEntityFilter}
          onStatusFilterChange={queueManager.filters.setStatusFilter}
          onSortChange={queueManager.filters.setSortConfig}
          onClearFilters={queueManager.filters.clearFilters}
          showClearButton={queueManager.filters.hasActiveFilters}
        />
      
      {/* Active Filters Display */}
      {queueManager.filters.hasActiveFilters && (
        <ActiveFiltersDisplay
          entityFilter={queueManager.filters.entityFilter}
          statusFilter={queueManager.filters.statusFilter}
        />
      )}
      
      {/* Auto-refresh Indicator */}
      {adminSettings.getAdminPanelRefreshMode() === 'auto' && (
        <AutoRefreshIndicator
          enabled={true}
          intervalSeconds={Math.round(adminSettings.getAdminPanelPollInterval() / 1000)}
          mode={adminSettings.getUseRealtimeQueue() ? 'realtime' : 'polling'}
        />
      )}
      
      {/* New Items Alert */}
      {queueManager.newItemsCount > 0 && (
        <NewItemsAlert
          count={queueManager.newItemsCount}
          onShowNewItems={queueManager.showNewItems}
        />
      )}
      
      {/* Queue Content */}
      {queueManager.loadingState === 'loading' || queueManager.loadingState === 'initial' ? (
        <QueueSkeleton count={queueManager.pagination.pageSize} />
      ) : queueManager.items.length === 0 ? (
        <EnhancedEmptyState
          entityFilter={queueManager.filters.entityFilter}
          statusFilter={queueManager.filters.statusFilter}
          onClearFilters={queueManager.filters.hasActiveFilters ? handleClearFilters : undefined}
        />
      ) : (
        <TooltipProvider>
          {queueManager.items.length <= 10 ? (
            // Standard rendering for small lists (no virtual scrolling overhead)
            <div className="space-y-6">
              {queueManager.items.map((item) => (
                <ModerationErrorBoundary key={item.id} submissionId={item.id}>
                  <QueueItem
                    item={item}
                    isMobile={isMobile ?? false}
                    actionLoading={queueManager.actionLoading}
                    isLockedByMe={queueManager.queue.isLockedByMe(item.id, item.assigned_to || null, item.locked_until || null)}
                    isLockedByOther={queueManager.queue.isLockedByOther(item.id, item.assigned_to || null, item.locked_until || null)}
                    lockStatus={getLockStatus({ assigned_to: item.assigned_to || null, locked_until: item.locked_until || null }, user?.id || '')}
                    currentLockSubmissionId={queueManager.queue.currentLock?.submissionId}
                    notes={notes}
                    isAdmin={isAdmin()}
                    isSuperuser={isSuperuser()}
                    queueIsLoading={queueManager.queue.isLoading}
                    onNoteChange={handleNoteChange}
                    onApprove={queueManager.performAction}
                    onResetToPending={queueManager.resetToPending}
                    onRetryFailed={queueManager.retryFailedItems}
                    onOpenPhotos={handleOpenPhotos}
                    onOpenReviewManager={handleOpenReviewManager}
                    onOpenItemEditor={handleOpenItemEditor}
                    onClaimSubmission={queueManager.queue.claimSubmission}
                    onDeleteSubmission={handleDeleteSubmission}
                    onInteractionFocus={(id) => queueManager.markInteracting(id, true)}
                    onInteractionBlur={(id) => queueManager.markInteracting(id, false)}
                  />
                </ModerationErrorBoundary>
              ))}
            </div>
          ) : (
            // Virtual scrolling for large lists (10+ items)
            <div
              ref={parentRef}
              className="overflow-auto"
              style={{
                height: '70vh',
                contain: 'strict',
              }}
            >
              <div
                style={{
                  height: `${virtualizer.getTotalSize()}px`,
                  width: '100%',
                  position: 'relative',
                }}
              >
                {virtualizer.getVirtualItems().map((virtualItem) => {
                  const item = queueManager.items[virtualItem.index];
                  return (
                    <div
                      key={item.id}
                      data-index={virtualItem.index}
                      ref={virtualizer.measureElement}
                      style={{
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        width: '100%',
                        transform: `translateY(${virtualItem.start}px)`,
                      }}
                      className="pb-6"
                    >
                      <ModerationErrorBoundary submissionId={item.id}>
                        <QueueItem
                          item={item}
                          isMobile={isMobile ?? false}
                          actionLoading={queueManager.actionLoading}
                          isLockedByMe={queueManager.queue.isLockedByMe(item.id, item.assigned_to || null, item.locked_until || null)}
                          isLockedByOther={queueManager.queue.isLockedByOther(item.id, item.assigned_to || null, item.locked_until || null)}
                          lockStatus={getLockStatus({ assigned_to: item.assigned_to || null, locked_until: item.locked_until || null }, user?.id || '')}
                          currentLockSubmissionId={queueManager.queue.currentLock?.submissionId}
                          notes={notes}
                          isAdmin={isAdmin()}
                          isSuperuser={isSuperuser()}
                          queueIsLoading={queueManager.queue.isLoading}
                          onNoteChange={handleNoteChange}
                          onApprove={queueManager.performAction}
                          onResetToPending={queueManager.resetToPending}
                          onRetryFailed={queueManager.retryFailedItems}
                          onOpenPhotos={handleOpenPhotos}
                          onOpenReviewManager={handleOpenReviewManager}
                          onOpenItemEditor={handleOpenItemEditor}
                          onClaimSubmission={queueManager.queue.claimSubmission}
                          onDeleteSubmission={handleDeleteSubmission}
                          onInteractionFocus={(id) => queueManager.markInteracting(id, true)}
                          onInteractionBlur={(id) => queueManager.markInteracting(id, false)}
                        />
                      </ModerationErrorBoundary>
                    </div>
                  );
                })}
              </div>
            </div>
          )}
        </TooltipProvider>
      )}
      
      {/* Pagination */}
      {queueManager.loadingState === 'ready' && queueManager.pagination.totalPages > 1 && (
        <QueuePagination
          currentPage={queueManager.pagination.currentPage}
          totalPages={queueManager.pagination.totalPages}
          pageSize={queueManager.pagination.pageSize}
          totalCount={queueManager.pagination.totalCount}
          isMobile={isMobile ?? false}
          onPageChange={queueManager.pagination.setCurrentPage}
          onPageSizeChange={queueManager.pagination.setPageSize}
        />
      )}
      
      {/* Modals */}
      <PhotoModal
        photos={selectedPhotos.map(photo => ({
          ...photo,
          caption: photo.caption ?? undefined
        }))}
        initialIndex={selectedPhotoIndex}
        isOpen={photoModalOpen}
        onClose={() => setPhotoModalOpen(false)}
      />
      
      {selectedSubmissionId && (
        <SubmissionReviewManager
          submissionId={selectedSubmissionId}
          open={reviewManagerOpen}
          onOpenChange={setReviewManagerOpen}
          onComplete={() => {
            queueManager.refresh();
            setSelectedSubmissionId(null);
          }}
        />
      )}
      
      {/* Phase 3: Item Selector Dialog */}
      <ItemSelectorDialog
        items={availableItems}
        open={showItemSelector}
        onOpenChange={setShowItemSelector}
        onSelectItem={handleSelectItem}
        onBulkEdit={() => {
          setShowItemSelector(false);
          setBulkEditItems(availableItems);
          setBulkEditMode(true);
          setShowItemEditDialog(true);
        }}
      />
      
      {/* Phase 4 & 5: Enhanced Item Edit Dialog */}
      <ItemEditDialog
        item={bulkEditMode ? null : editingItem}
        items={bulkEditMode ? bulkEditItems : undefined}
        open={showItemEditDialog}
        onOpenChange={(open) => {
          setShowItemEditDialog(open);
          if (!open) {
            setEditingItem(null);
            setBulkEditMode(false);
            setBulkEditItems([]);
          }
        }}
        onComplete={() => {
          queueManager.refresh();
          setEditingItem(null);
          setBulkEditMode(false);
          setBulkEditItems([]);
        }}
      />
      
      {/* Confirmation Dialog */}
      <ConfirmationDialog
        open={confirmDialog.open}
        onOpenChange={(open) => setConfirmDialog(prev => ({ ...prev, open }))}
        title={confirmDialog.title}
        description={confirmDialog.description}
        onConfirm={confirmDialog.onConfirm}
        variant="destructive"
        confirmLabel="Delete"
      />
      
      {/* Keyboard Shortcuts Help */}
      <KeyboardShortcutsHelp
        open={showShortcutsHelp}
        onOpenChange={setShowShortcutsHelp}
        shortcuts={shortcuts}
      />
    </div>
  );
});

ModerationQueue.displayName = 'ModerationQueue';

export type { ModerationQueueRef } from '@/types/moderation';
