import React, { ReactNode } from 'react';
import DocumentViewerPanel from '../../preview/DocumentViewerPanel';
import SelectionFloatingActions from '../SelectionFloatingActions';
import createWorkspaceSurfaceConfig from '../workspaceHeader';
import DocumentsPanel from './DocumentsPanel';
import { createDocumentsTableHeaderActions } from './DocumentsToolbar';

interface CreateDocumentsSurfaceArgs {
  tableProps: Record<string, any>;
  renderSidebarToggle?: () => ReactNode;
  detailProps?: Record<string, any> | null;
  detailOpen?: boolean;
}

const createDocumentsSurface = ({
  tableProps,
  renderSidebarToggle,
  detailProps,
  detailOpen = false,
}: CreateDocumentsSurfaceArgs) => {
  const {
    currentFolderName,
    breadcrumbs,
    searchResults,
    isFilterActive,
    viewMode,
    onViewModeChange,
    onRefresh,
    sortField,
    sortDirection,
    onSortFieldChange,
    onSortDirectionToggle,
    selectedDocumentIds,
    selectedFolderIds,
    onDeleteSelection,
    onClearSelection,
    tags,
    correspondents,
    documentLookup,
    tagLookupById,
    onBulkTagAdd,
    onBulkTagRemove,
    onBulkCorrespondentAdd,
    onBulkCorrespondentRemove,
    onBulkReanalyze,
    folderOptions,
    onMoveDocumentsToFolder,
    searchIncludeDescendants,
    onToggleSearchIncludeDescendants,
    onInspectDocument,
  } = tableProps;

  const title = Array.isArray(searchResults) ? 'Search results' : currentFolderName;
  const subtitle = Array.isArray(searchResults)
    ? `${searchResults.length} matching document${searchResults.length === 1 ? '' : 's'}`
    : null;

  const documentSelectionCount = Array.isArray(selectedDocumentIds) ? selectedDocumentIds.length : 0;
  const folderSelectionCount = Array.isArray(selectedFolderIds) ? selectedFolderIds.length : 0;
  const selectionCount = documentSelectionCount + folderSelectionCount;

  const actions = createDocumentsTableHeaderActions({
    viewMode,
    onViewModeChange,
    onRefresh,
    sortField,
    onSortFieldChange,
    sortDirection,
    onSortDirectionToggle,
    isFilterActive,
    includeDescendants: searchIncludeDescendants,
    onToggleIncludeDescendants: onToggleSearchIncludeDescendants,
  });

  const floatingActions = selectionCount > 0
    ? (
        <SelectionFloatingActions
          selectionCount={selectionCount}
          selectedDocumentIds={selectedDocumentIds}
          selectedFolderIds={selectedFolderIds}
          documentLookup={documentLookup}
          tags={tags}
          tagLookupById={tagLookupById}
          correspondents={correspondents}
          onBulkTagAdd={onBulkTagAdd}
          onBulkTagRemove={onBulkTagRemove}
          onBulkCorrespondentAdd={onBulkCorrespondentAdd}
          onBulkCorrespondentRemove={onBulkCorrespondentRemove}
          onBulkReanalyze={onBulkReanalyze}
          onDeleteSelection={onDeleteSelection}
          onClearSelection={onClearSelection}
          folderOptions={folderOptions}
          onMoveDocumentsToFolder={onMoveDocumentsToFolder}
        />
      )
    : null;

  const sidebarToggle = renderSidebarToggle ? renderSidebarToggle() : null;
  const detail = detailOpen && detailProps
    ? (() => {
        const { onClose, onOpenPreview, tags: tagOptions, ...restDetailProps } = detailProps;
        return (
          <DocumentViewerPanel
            variant="sidebar"
            onCollapsePanel={onClose}
            onMaximizePanel={onOpenPreview}
            tagOptions={tagOptions}
            {...restDetailProps}
          />
        );
      })()
    : null;

  return createWorkspaceSurfaceConfig({
    key: 'documents',
    variant: 'documents',
    title,
    subtitle,
    sidebarToggle,
    actions,
    breadcrumbs,
    selectionLabel: null,
    floatingActions,
    content: (
      <DocumentsPanel
        {...tableProps}
        showHeader={false}
        onInspectDocument={onInspectDocument}
      />
    ),
    detail,
  });
};

export default createDocumentsSurface;
