import React, {
  useCallback,
  useMemo,
  useState,
  useRef,
  useEffect,
} from 'react';
import { DocumentsList, DocumentsGrid } from '../DocumentsView';
import type { ReactNode } from 'react';
import type {
  DocumentsListEntry,
} from '../../types/documents';
import DesktopWorkspace from '../../desktop/components/DesktopWorkspace';
import {
  WorkspaceSelectionProvider,
  useWorkspaceSelectionContext,
  type WorkspaceSelectionValue,
} from '../../app/WorkspaceSelectionContext';
import DocumentsPanelHeader, {
  DocumentsPanelHeaderConfig,
  DocumentsHeaderBreadcrumb,
} from './DocumentsPanelHeader';
import { SelectionFloatingPanel } from '../features/selection/SelectionFloatingActions';
import { createDocumentsTableHeaderActions } from './DocumentsToolbar';
import { useDocumentsFilter } from '../context/DocumentsFilterContext';
import {
  DEFAULT_GRID_ICON_SIZE,
  DEFAULT_LIST_ICON_SIZE,
  DEFAULT_DESKTOP_CARD_SIZE,
} from '../../constants/documents';
import { DocumentsAssetContext } from '../context/DocumentsAssetContext';
import { DocumentsViewStateContext } from '../context/DocumentsViewStateContext';
import { DocumentsCommandContext } from '../context/DocumentsCommandContext';
import { useDocumentsContextValues } from './useDocumentsContextValues';

const EntryType = {
  folder: 'folder' as const,
  document: 'document' as const,
};

export interface DocumentsPanelInnerProps {
  headerLeading?: ReactNode;
  onBreadcrumbNavigate?: (crumb: DocumentsHeaderBreadcrumb) => void;
  [key: string]: any;
}

interface DocumentsPanelProps extends DocumentsPanelInnerProps {
  selectionValue: WorkspaceSelectionValue;
}

import type { TagDragHandlers } from '../interactions/useTagInteractions';

export interface DocumentsViewProps {
  entries: DocumentsListEntry[];
  tagDragHandlers?: TagDragHandlers;
}

const DocumentsPanelInner: React.FC<DocumentsPanelInnerProps> = (props) => {
  const {
    headerLeading = null,
    onBreadcrumbNavigate,
    currentFolderName,
    breadcrumbs,
    subfolders,
    documents,
    searchResultIds,
    onRefresh = () => { },
    sortField,
    sortDirection,
    onSortFieldChange,
    onSortDirectionToggle,
    onDeleteSelection,
    documentLookup,
    tags,
    correspondents,
    onBulkTagAdd,
    onBulkTagRemove,
    onBulkCorrespondentAdd,
    onBulkCorrespondentRemove,
    onBulkReanalyze,
    folderOptions,
    onMoveDocumentsToFolder,
    viewMode = 'list',
    onViewModeChange,
  } = props;

  const {
    assetContextValue,
    viewStateContextValue,
    commandContextValue,
    tagDragHandlers,
    scrollRef,
    hasDocumentEntries,
  } = useDocumentsContextValues(props);

  const {
    clearSelection,
  } = useWorkspaceSelectionContext();
  const {
    isActive: isFilterActive,
    includeDescendants,
    toggleIncludeDescendants,
  } = useDocumentsFilter();

  const searchDocuments = useMemo(
    () =>
      Array.isArray(searchResultIds)
        ? searchResultIds
          .map((id: any) => documentLookup?.get?.(id) || null)
          .filter((doc: any): doc is Record<string, unknown> => Boolean(doc))
        : null,
    [searchResultIds, documentLookup],
  );

  const showingSearchResults = Array.isArray(searchResultIds);
  const rows = showingSearchResults && searchDocuments ? searchDocuments : documents;


  const headerTitle = showingSearchResults
    ? 'Search results'
    : currentFolderName || 'Documents';

  const headerActions = useMemo(
    () => createDocumentsTableHeaderActions({
      viewMode,
      onViewModeChange,
      onRefresh,
      sortField,
      onSortFieldChange,
      sortDirection,
      onSortDirectionToggle,
      isFilterActive,
      includeDescendants,
      onToggleIncludeDescendants: toggleIncludeDescendants,
    }),
    [
      viewMode,
      onViewModeChange,
      onRefresh,
      sortField,
      onSortFieldChange,
      sortDirection,
      onSortDirectionToggle,
      isFilterActive,
      includeDescendants,
      toggleIncludeDescendants,
    ],
  );
  const floatingActions = useMemo(() => (
    <SelectionFloatingPanel
      documentLookup={documentLookup}
      tags={tags}
      tagLookupById={props.tagLookupById}
      correspondents={correspondents}
      onBulkTagAdd={onBulkTagAdd}
      onBulkTagRemove={onBulkTagRemove}
      onBulkCorrespondentAdd={onBulkCorrespondentAdd}
      onBulkCorrespondentRemove={onBulkCorrespondentRemove}
      onBulkReanalyze={onBulkReanalyze}
      onDeleteSelection={onDeleteSelection}
      folderOptions={folderOptions}
      onMoveDocumentsToFolder={onMoveDocumentsToFolder}
      onClearSelection={clearSelection}
    />
  ), [
    documentLookup,
    tags,
    props.tagLookupById,
    correspondents,
    onBulkTagAdd,
    onBulkTagRemove,
    onBulkCorrespondentAdd,
    onBulkCorrespondentRemove,
    onBulkReanalyze,
    onDeleteSelection,
    folderOptions,
    onMoveDocumentsToFolder,
    clearSelection,
  ]);
  const headerConfig: DocumentsPanelHeaderConfig = useMemo(() => ({
    title: headerTitle,
    subtitle: null,
    leading: headerLeading,
    actions: headerActions,
    breadcrumbs,
    floatingActions,
  }), [
    headerTitle,
    headerLeading,
    headerActions,
    breadcrumbs,
    floatingActions,
  ]);

  const currentFolderId = useMemo(() => {
    if (showingSearchResults) {
      return null;
    }
    const trail = Array.isArray(breadcrumbs) ? breadcrumbs : [];
    if (trail.length === 0) {
      return 'root';
    }
    return trail[trail.length - 1]?.id || 'root';
  }, [breadcrumbs, showingSearchResults]);

  // Context marker logic for clearing selection on nav
  const selectionContextRef = useRef<any>(null);
  useEffect(() => {
    const nextContext = showingSearchResults
      ? { type: 'search', marker: searchResultIds }
      : { type: 'folder', marker: currentFolderId || 'root' };
    const previous = selectionContextRef.current;
    selectionContextRef.current = nextContext;
    if (!previous) {
      return;
    }
    const changed = previous.type !== nextContext.type
      || previous.marker !== nextContext.marker;
    if (changed) {
      clearSelection();
    }
  }, [showingSearchResults, currentFolderId, searchResultIds, clearSelection]);

  const entries = useMemo(() => {
    const list: DocumentsListEntry[] = []; // Explicit type
    if (!showingSearchResults) {
      subfolders.forEach((folder: any) => {
        if (!folder || !folder.id) {
          return;
        }
        list.push({ type: EntryType.folder, id: folder.id, key: `folder:${folder.id}`, folder });
      });
    }
    rows.forEach((doc: any) => {
      if (!doc || !doc.id) {
        return;
      }
      list.push({ type: EntryType.document, id: doc.id, key: `document:${doc.id}`, document: doc });
    });
    return list;
  }, [showingSearchResults, subfolders, rows]);

  const isGridView = viewMode === 'grid';
  const isDeskView = viewMode === 'desk';

  const viewProps = {
    entries,
    viewId: currentFolderId,
    tagDragHandlers,
  };

  const [iconSizes] = useState({
    list: DEFAULT_LIST_ICON_SIZE,
    grid: DEFAULT_GRID_ICON_SIZE,
    desk: DEFAULT_DESKTOP_CARD_SIZE,
  });

  const isSearchLoading = props.isSearchLoading || false;

  const renderBody = () => {
    const hasEntries = entries.length > 0;
    const isSearchEmpty = (showingSearchResults || isFilterActive) && !hasDocumentEntries && !isSearchLoading;

    if (isSearchEmpty) {
      return (
        <div className="empty-state">
          No documents match the current filters.
        </div>
      );
    }

    if (!hasEntries) {
      return (
        <div className="empty-state">
          No documents to show here yet. Drop files to make this space come alive.
        </div>
      );
    }

    switch (viewMode) {
      case 'desk':
        return <DesktopWorkspace {...viewProps} defaultCardSize={iconSizes.desk} />;
      case 'grid':
        return <DocumentsGrid {...viewProps} iconSize={iconSizes.grid} />;
      case 'list':
      default:
        return <DocumentsList {...viewProps} iconSize={iconSizes.list} />;
    }
  };

  const panelVariant = isDeskView ? 'desk' : isGridView ? 'grid' : 'list';
  const shouldHandlePanelInteractions = !isDeskView && entries.length > 0;

  const handleSectionClick = useCallback((event: React.MouseEvent<HTMLElement>) => {
    if (!shouldHandlePanelInteractions || event.target !== event.currentTarget) {
      return;
    }
    clearSelection();
  }, [shouldHandlePanelInteractions, clearSelection]);

  return (
    <DocumentsAssetContext.Provider value={assetContextValue}>
      <DocumentsViewStateContext.Provider value={viewStateContextValue}>
        <DocumentsCommandContext.Provider value={commandContextValue}>
          <DocumentsPanelHeader
            header={headerConfig}
            onBreadcrumbClick={onBreadcrumbNavigate}
          />
          <div className="documents-panel-wrapper">
            <section
              ref={scrollRef}
              className={`documents-panel documents-panel--view-${panelVariant}`}
              onClick={handleSectionClick}
            >
              {renderBody()}
            </section>
          </div>
        </DocumentsCommandContext.Provider>
      </DocumentsViewStateContext.Provider>
    </DocumentsAssetContext.Provider>
  );
};

const DocumentsPanel: React.FC<DocumentsPanelProps> = ({ selectionValue, ...rest }) => (
  <WorkspaceSelectionProvider value={selectionValue}>
    <DocumentsPanelInner {...rest} />
  </WorkspaceSelectionProvider>
);

export default DocumentsPanel;
