'use client';

import { useCallback } from 'react';
import { Chat } from './Chat';
import type { ChatEngine, DeepseekModel, ChatMode } from './types';

// Migrated from src/components/project/ChatPanel.tsx.

// ============================================
// ChatPanel - Simplified Chat panel without header and sidebar
// ============================================

interface ChatPanelProps {
  tabId: string;
  cwd?: string;
  sessionId?: string;
  engine?: ChatEngine;
  ollamaModel?: string;
  onOllamaModelChange?: (tabId: string, model: string) => void;
  deepseekModel?: DeepseekModel;
  onDeepseekModelChange?: (tabId: string, model: DeepseekModel) => void;
  chatMode?: ChatMode;
  onChatModeChange?: (tabId: string, chatMode: ChatMode) => void;
  planMode?: boolean;
  onPlanModeChange?: (tabId: string, planMode: boolean) => void;
  isActive?: boolean;
  onStateChange: (tabId: string, updates: { isLoading?: boolean; sessionId?: string; title?: string }) => void;
  onShowGitStatus?: () => void;
  onOpenNote?: () => void;
  onCreateScheduledTask?: (params: {
    cwd: string;
    tabId: string;
    sessionId: string;
    engine?: string;
    model?: string;
    message: string;
    type: 'once' | 'interval' | 'cron';
    delayMinutes?: number;
    intervalMinutes?: number;
    activeFrom?: string;
    activeTo?: string;
    cron?: string;
  }) => void;
  onOpenSession?: (sessionId: string, title?: string) => void;
  onContentSearch?: (query: string) => void;
}

export function ChatPanel({ tabId, cwd, sessionId, engine, ollamaModel, onOllamaModelChange, deepseekModel, onDeepseekModelChange, chatMode, onChatModeChange, planMode, onPlanModeChange, isActive, onStateChange, onShowGitStatus, onOpenNote, onCreateScheduledTask, onOpenSession, onContentSearch }: ChatPanelProps) {
  const handleLoadingChange = useCallback((isLoading: boolean) => {
    onStateChange(tabId, { isLoading });
  }, [tabId, onStateChange]);

  const handleSessionIdChange = useCallback((newSessionId: string) => {
    onStateChange(tabId, { sessionId: newSessionId });
  }, [tabId, onStateChange]);

  const handleTitleChange = useCallback((title: string) => {
    onStateChange(tabId, { title });
  }, [tabId, onStateChange]);

  const handleOllamaModelChange = useCallback((model: string) => {
    onOllamaModelChange?.(tabId, model);
  }, [tabId, onOllamaModelChange]);

  const handleDeepseekModelChange = useCallback((model: DeepseekModel) => {
    onDeepseekModelChange?.(tabId, model);
  }, [tabId, onDeepseekModelChange]);

  const handleChatModeChange = useCallback((m: ChatMode) => {
    onChatModeChange?.(tabId, m);
  }, [tabId, onChatModeChange]);

  const handlePlanModeChange = useCallback((p: boolean) => {
    onPlanModeChange?.(tabId, p);
  }, [tabId, onPlanModeChange]);

  return (
    <Chat
      tabId={tabId}
      initialCwd={cwd}
      initialSessionId={sessionId}
      engine={engine}
      ollamaModel={ollamaModel}
      onOllamaModelChange={handleOllamaModelChange}
      deepseekModel={deepseekModel}
      onDeepseekModelChange={handleDeepseekModelChange}
      chatMode={chatMode}
      onChatModeChange={handleChatModeChange}
      planMode={planMode}
      onPlanModeChange={handlePlanModeChange}
      hideHeader
      hideSidebar
      isActive={isActive}
      onLoadingChange={handleLoadingChange}
      onSessionIdChange={handleSessionIdChange}
      onTitleChange={handleTitleChange}
      onShowGitStatus={onShowGitStatus}
      onOpenNote={onOpenNote}
      onCreateScheduledTask={onCreateScheduledTask}
      onOpenSession={onOpenSession}
      onContentSearch={onContentSearch}
    />
  );
}
