'use client';

import { useEffect, useCallback } from 'react';
import { useVoiceCommand } from '@/hooks/useVoiceCommand';
import { useAuth } from '@/contexts/AuthContext';
import { useVoiceCommandContext } from '@/contexts/VoiceCommandContext';

/**
 * 전역 음성 명령 프로바이더 (확장 버전)
 * 
 * 전역 레벨 명령 (내비게이션 + 검색)을 직접 처리하고,
 * 페이지별 명령 (에디터/문서/녹음/AI)은 VoiceCommandContext에서 가져와 위임합니다.
 * 
 * 31개 명령 · 6개 그룹
 * - 내비게이션: "새 문서", "홈으로", "뒤로", "데일리 노트"
 * - 검색: "검색해", "AI 검색", "위키 검색", "음성 검색"
 * - 에디터: "편집", "취소", "저장", "줄 바꿔", "새 문단", "할 일", "되돌려"
 * - 문서 관리: "공개해", "비공개", "삭제", "히스토리", "AI 열어"
 * - 녹음: "받아쓰기", "중지", "일시정지", "녹취 시작", "녹취 종료"
 * - AI 트리거: "요약해줘", "정리해줘", "핵심 뽑아줘", "비교해줘", "개인정보 가려", "코칭해줘"
 */
export default function VoiceCommandProvider() {
    const { user } = useAuth();
    const { getHandlers } = useVoiceCommandContext();

    // 검색 모달 오픈 (Ctrl+K 이벤트 디스패치)
    const dispatchSearchOpen = useCallback(() => {
        const event = new KeyboardEvent('keydown', {
            key: 'k',
            ctrlKey: true,
            bubbles: true,
        });
        document.dispatchEvent(event);
    }, []);

    useVoiceCommand({
        enabled: !!user,

        // ── 검색 (전역) ──
        onOpenSearch: dispatchSearchOpen,
        onOpenAISearch: dispatchSearchOpen,   // 검색 모달 열고 AI 모드 선택
        onOpenWikiSearch: dispatchSearchOpen,  // 검색 모달 열고 일반 모드
        onOpenVoiceSearch: dispatchSearchOpen,

        // ── 에디터 명령 (컨텍스트에서 위임) ──
        onEditStart: () => getHandlers().onEditStart?.(),
        onEditCancel: () => getHandlers().onEditCancel?.(),
        onSaveDocument: () => getHandlers().onSaveDocument?.(),
        onInsertNewline: () => getHandlers().onInsertNewline?.(),
        onInsertParagraph: () => getHandlers().onInsertParagraph?.(),
        onInsertChecklist: () => getHandlers().onInsertChecklist?.(),
        onUndo: () => getHandlers().onUndo?.(),

        // ── 문서 관리 (컨텍스트에서 위임) ──
        onSetPublic: () => getHandlers().onSetPublic?.(),
        onSetPrivate: () => getHandlers().onSetPrivate?.(),
        onDelete: () => getHandlers().onDelete?.(),
        onOpenHistory: () => getHandlers().onOpenHistory?.(),
        onOpenAIPanel: () => getHandlers().onOpenAIPanel?.(),

        // ── 녹음 제어 (컨텍스트에서 위임) ──
        onDictationStart: () => getHandlers().onDictationStart?.(),
        onDictationStop: () => getHandlers().onDictationStop?.(),
        onDictationPause: () => getHandlers().onDictationPause?.(),
        onConsentRecordStart: () => getHandlers().onConsentRecordStart?.(),
        onConsentRecordEnd: () => getHandlers().onConsentRecordEnd?.(),

        // ── AI 트리거 (컨텍스트에서 위임) ──
        onAISummarize: () => getHandlers().onAISummarize?.(),
        onAIStructure: () => getHandlers().onAIStructure?.(),
        onAIExtract: () => getHandlers().onAIExtract?.(),
        onAICompare: () => getHandlers().onAICompare?.(),
        onAIMask: () => getHandlers().onAIMask?.(),
        onAICoach: () => getHandlers().onAICoach?.(),
    });

    // 렌더링 없는 순수 훅 컨테이너
    return null;
}
