import React, { type DragEvent } from 'react';
import { createDocumentEntryKey } from '../../app/entryKey';
import { useDocumentOpen } from '../../lib/context/DocumentOpenContext';
import type { Document } from '../../types/documents';
import { useDocumentsCommandContext } from '../context/DocumentsCommandContext';
import { useDocumentsViewStateContext } from '../context/DocumentsViewStateContext';
import type { DocumentViewLogic } from './useDocumentViewLogic';

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

interface UseDocumentItemLogicArgs {
    doc: Document;
    tagDragHandlers?: TagDragHandlers;
    viewLogic: DocumentViewLogic; // Retained from original props
}

export const useDocumentItemLogic = ({ doc, tagDragHandlers, viewLogic }: UseDocumentItemLogicArgs) => {
    const {
        draggingDocumentIdsSet
    } = useDocumentsViewStateContext();

    const {
        document: {
            onDrag: { start: onDocumentDragStart, end: onDocumentDragEnd },
            onRename: onDocumentRename
        },
        onEntryPointer
    } = useDocumentsCommandContext();

    const {
        selectedDocumentIdsSet,
        totalSelectionCount,
        documentRename: {
            editingId: editingDocumentId,
            draftValue: documentDraft,
            setDraftValue: setDocumentDraft,
            beginEditing: beginDocumentEditing,
            cancelEditing: cancelDocumentEditing,
            submitEditing: submitDocumentEditing,
            savingId: savingDocumentId,
            attachInputRef: attachDocumentInputRef,
        },
        handleEntrySelection,
    } = viewLogic;

    const isSelected = selectedDocumentIdsSet?.has(doc.id);
    const isDraggingDoc = draggingDocumentIdsSet?.has(doc.id);
    const isEditingDoc = editingDocumentId === doc.id;
    const documentDraftValue = isEditingDoc ? documentDraft : doc.title;
    const trimmedDocumentDraft = isEditingDoc ? documentDraft.trim() : '';
    const isDocumentSaving = savingDocumentId === doc.id;
    const canSubmitDocument =
        isEditingDoc && trimmedDocumentDraft.length > 0 && trimmedDocumentDraft !== doc.title;
    const allowInlineDocumentEdit = onDocumentRename && isSelected && totalSelectionCount === 1;

    const { openDocument } = useDocumentOpen();

    const handlers = {
        onClick: (event: React.MouseEvent) => {
            if (onEntryPointer) {
                onEntryPointer({ type: 'document', id: doc.id, key: createDocumentEntryKey(doc.id), document: doc }, event);
            } else {
                const key = createDocumentEntryKey(doc.id);
                handleEntrySelection(key, event);
            }
        },
        onDoubleClick: (event: React.MouseEvent) => {
            const isPreview = event && (event.altKey || event.button === 1);
            openDocument(doc, isPreview ? 'preview' : 'sidepanel');
        },
        onDragStart: (event: DragEvent<HTMLElement>) => onDocumentDragStart?.(event, doc),
        onDragEnd: (event: DragEvent<HTMLElement>) => onDocumentDragEnd?.(event),
        onDragOver: (event: DragEvent<HTMLElement>) => tagDragHandlers?.onTagDragOver(event, doc),
        onDragLeave: (event: DragEvent<HTMLElement>) => tagDragHandlers?.onTagDragLeave(event, doc.id),
        onDrop: (event: DragEvent<HTMLElement>) => { tagDragHandlers?.onTagDrop(event, doc); },
        tagDragHandlers,
        onRenameChange: setDocumentDraft,
        onRenameSubmit: () => submitDocumentEditing(doc),
        onRenameCancel: (event?: React.SyntheticEvent) => cancelDocumentEditing(event),
        onRenameBegin: (event: React.SyntheticEvent) => {
            if (!allowInlineDocumentEdit) return;
            event.preventDefault();
            event.stopPropagation();
            beginDocumentEditing(doc);
        },
    };

    return {
        isSelected,
        isDraggingDoc,
        isEditingDoc,
        documentDraftValue,
        isDocumentSaving,
        canSubmitDocument,
        allowInlineDocumentEdit,
        attachDocumentInputRef,
        handlers,
    };
};


