import type { TextAreaRef } from 'antd/es/input/TextArea';
import { useMemo, useRef } from 'react';
import { ChatConversationView } from '../../mainChatPanel';
import type { ChatConversationRequest, ChatMessage } from '../../mainChatPanel/types';

type ConversationRoomPaneProps = {
  sessionId: string;
  messages: ChatMessage[];
  requests: ChatConversationRequest[];
  isLoading: boolean;
  loadingLabel: string;
  errorMessage: string;
};

const GENERAL_REQUEST_OPTION = [
  {
    value: 'general-request',
    label: '일반 요청',
    description: '기본 채팅유형',
    disabled: true,
  },
];

const GENERAL_CODEX_MODEL_OPTION = [
  {
    value: 'gpt-5.4',
    label: 'GPT-5.4',
    description: '기본 모델',
  },
];

export function ConversationRoomPane({
  sessionId,
  messages,
  requests,
  isLoading,
  loadingLabel,
  errorMessage,
}: ConversationRoomPaneProps) {
  const normalizedSessionId = typeof sessionId === 'string' ? sessionId : '';
  const normalizedMessages = Array.isArray(messages) ? messages : [];
  const normalizedRequests = Array.isArray(requests) ? requests : [];
  const normalizedLoadingLabel = typeof loadingLabel === 'string' ? loadingLabel : '';
  const normalizedErrorMessage = typeof errorMessage === 'string' ? errorMessage : '';
  const viewportRef = useRef<HTMLDivElement | null>(null);
  const composerRef = useRef<TextAreaRef | null>(null);
  const requestStateMap = useMemo(
    () => new Map(normalizedRequests.map((request) => [request.requestId, request])),
    [normalizedRequests],
  );
  const activeSystemStatus = normalizedErrorMessage.trim() || (normalizedSessionId ? null : '대화방을 선택해 주세요.');

  return (
    <ChatConversationView
      sessionId={normalizedSessionId}
      viewportRef={viewportRef}
      composerRef={composerRef}
      visibleMessages={normalizedMessages}
      activeSystemStatus={activeSystemStatus}
      isSystemStatusPending={false}
      showScrollToBottom={false}
      copiedMessageId={null}
      draft=""
      draftVersion={0}
      composerAttachments={[]}
      lastReadResponseMessageId={null}
      requestStateMap={requestStateMap}
      isConversationLoading={isLoading}
      conversationLoadingLabel={normalizedLoadingLabel}
      hasOlderMessages={false}
      isLoadingOlderMessages={false}
      isPullToLoadArmed={false}
      pullToLoadDistance={0}
      selectedChatTypeId="general-request"
      selectedCodexModel="gpt-5.4"
      queuedRequests={[]}
      chatTypeOptions={GENERAL_REQUEST_OPTION}
      codexModelOptions={GENERAL_CODEX_MODEL_OPTION}
      previewItems={[]}
      isResourceStripOpen={false}
      isComposerDisabled={true}
      isMobileViewport={false}
      isIpadLikeViewport={false}
      isChatTypeSelectionLocked={true}
      isComposerAttachmentUploading={false}
      isSendWithoutContextEnabled={false}
      onViewportScroll={() => {}}
      onViewportTouchEnd={() => {}}
      onViewportTouchMove={() => {}}
      onViewportTouchStart={() => {}}
      onDraftChange={() => {}}
      onPickComposerFiles={async () => ({ items: [] })}
      onRemoveComposerAttachment={() => {}}
      onSelectChatType={() => {}}
      onSelectCodexModel={() => {}}
      onSend={() => 'blocked'}
      onSendImmediate={() => 'blocked'}
      onToggleSendWithoutContext={() => {}}
      isImmediateSendPinned={false}
      onToggleImmediateSendPinned={() => {}}
      onClearDraft={() => {}}
      onScrollToBottom={() => {}}
      onToggleResourceStrip={() => {}}
      onLoadOlderMessages={() => {}}
      onOpenPreview={() => {}}
      onCopyMessage={() => {}}
      onRetryMessage={() => {}}
      onRetryFailedRequest={() => {}}
      onCancelMessage={() => {}}
      onDeleteRequest={() => {}}
      onCompleteManualRequestBadge={() => {}}
      onRemoveQueuedRequest={() => {}}
      onPromoteQueuedRequest={() => {}}
      onSubmitPrompt={async () => false}
      onSubmitChildRequest={async () => false}
    />
  );
}
