// @ts-nocheck
import { debugLog } from '../common';
import { useTaskStore } from '../../../stores/task';
import { getMessageVisibility, messageStartsWork } from '../../../utils/messageVisibility';
import {
  debugNotifyLog,
  keyNotifyLog,
  jsonDebug,
  userMDebug,
  isRestoreDebugEnabled,
  restoreDebugLog,
  isSystemAutoUserMessagePayload,
  isRuntimeModeNoticePayload,
  resolveUserMessageSource,
  resolveUserMessageMetadata,
  isEmptyAssistantPlaceholderMessage,
  getOptimisticUserEchoTarget,
  findRecentMatchingUserMessage,
} from './shared';

export const compressionMethods = {
  handleCompressionState(data: any) {
    if (!data || typeof data !== 'object') {
      return;
    }
    const wasInProgress = !!this.compressionInProgress;
    this.compressionInProgress = !!data.in_progress;
    this.compressionMode = data.mode || '';
    this.compressionStage = data.stage || '';
    if (this.compressionInProgress && !wasInProgress) {
      const modeLabel = this.compressionMode === 'manual' ? '手动' : '自动';
      if (this.compressionToastId) {
        this.uiDismissToast(this.compressionToastId);
        this.compressionToastId = null;
      }
      this.compressionToastId = this.uiPushToast({
        title: '压缩中',
        message: `对话正在${modeLabel}压缩，请稍候…`,
        type: 'info',
        duration: null,
        closable: false
      });
    }
    if (!this.compressionInProgress) {
      this.compressionError = data.error || '';
      if (this.compressionToastId) {
        this.uiDismissToast(this.compressionToastId);
        this.compressionToastId = null;
      }
    }
  },
  handleShallowCompression(data: any, eventIdx?: number) {
    const count = Number(data?.compressed_count || 0);
    if (count <= 0) {
      return;
    }
    debugLog('[TaskPolling] 自动浅层压缩触发, idx:', eventIdx, data);
    this.uiPushToast({
      title: '自动浅层压缩',
      message: `已自动压缩 ${count} 条较早工具结果`,
      type: 'info',
      duration: 2500
    });
  },
  async handleCompressionFinished(data: any) {
    if (this.compressionToastId) {
      this.uiDismissToast(this.compressionToastId);
      this.compressionToastId = null;
    }
    this.compressionInProgress = false;
    this.compressionMode = '';
    this.compressionStage = '';
    this.compressionError = '';
    const newId = data?.conversation_id;
    const isInPlace = newId && newId === this.currentConversationId;
    if (newId && !isInPlace) {
      // 旧行为兼容：压缩产生了新对话 id（非 in-place），需要切换并重新加载。
      await this.loadConversation(newId, { force: true });
      this.conversationsOffset = 0;
      if (typeof this.loadConversationsList === 'function') {
        await this.loadConversationsList();
      }
      await this.refreshRunningWorkspaceTasks?.();
      await this.restoreTaskState?.();
    }
    // in-place 压缩：对话 id 不变，不重新加载对话，也不触发 restoreTaskState。
    // loadConversation 会 clearTask 停止轮询才需要 restore；in-place 跳过了
    // loadConversation，轮询仍在运行，restoreTaskState 的 rebuild 反而会
    this.uiPushToast({
      title: '压缩完成',
      message: '已压缩较早的对话内容',
      type: 'success',
      duration: 2400
    });
  },
  async restoreTaskState() {
    // 清理已处理的事件索引
    this.clearProcessedEvents();
    restoreDebugLog('restore:start', {
      currentConversationId: this.currentConversationId,
      streamingMessage: this.streamingMessage,
      taskInProgress: this.taskInProgress,
      historyLoading: this.historyLoading,
      historyLoadingFor: this.historyLoadingFor,
      messagesLen: Array.isArray(this.messages) ? this.messages.length : -1
    });

    try {
      const { useTaskStore } = await import('../../../stores/task');
      const taskStore = useTaskStore();

      // 如果已经在流式输出中，不重复恢复
      if (this.streamingMessage || this.taskInProgress) {
        restoreDebugLog('restore:skip-already-running', {
          streamingMessage: this.streamingMessage,
          taskInProgress: this.taskInProgress,
          currentConversationId: this.currentConversationId
        });
        debugLog('[TaskPolling] 任务已在进行中，跳过恢复', {
          streamingMessage: this.streamingMessage,
          taskInProgress: this.taskInProgress,
          currentConversationId: this.currentConversationId
        });
        return;
      }

      // 查找运行中的任务
      const runningTask = await taskStore.loadRunningTask(this.currentConversationId);

      if (!runningTask) {
        restoreDebugLog('restore:no-running-task', {
          currentConversationId: this.currentConversationId
        });
        debugLog('[TaskPolling] 没有运行中的任务', {
          currentConversationId: this.currentConversationId
        });
        this._restoreTaskStateWaitCount = 0;
        await this.restoreSubAgentWaitingState();
        return;
      }
      if (
        (this.versioningHostMode || this.dockerProjectMode) &&
        this.currentHostWorkspaceId &&
        runningTask?.workspace_id &&
        runningTask.workspace_id !== this.currentHostWorkspaceId
      ) {
        restoreDebugLog('restore:skip-workspace-mismatch', {
          taskId: runningTask?.task_id,
          taskWorkspaceId: runningTask?.workspace_id,
          currentHostWorkspaceId: this.currentHostWorkspaceId
        });
        taskStore.clearTask();
        await this.restoreSubAgentWaitingState();
        return;
      }

      debugLog('[TaskPolling] 发现运行中的任务，开始恢复状态', {
        taskId: runningTask?.task_id,
        status: runningTask?.status,
        conversationId: runningTask?.conversation_id
      });
      if (runningTask?.goal_mode) {
        const existingGoalProgress = this.goalProgress && typeof this.goalProgress === 'object'
          ? this.goalProgress
          : {};
        const taskGoalProgress = runningTask?.goal_progress && typeof runningTask.goal_progress === 'object'
          ? runningTask.goal_progress
          : {};
        this.goalRunning = true;
        this.goalModeArmed = false;
        this.goalProgress = {
          ...existingGoalProgress,
          ...taskGoalProgress,
          goal: taskGoalProgress?.goal || existingGoalProgress?.goal || runningTask?.message || '',
          status: 'running',
          turn_count: Number(taskGoalProgress?.turn_count ?? existingGoalProgress?.turn_count ?? 0),
          tokens_used: Number(taskGoalProgress?.tokens_used ?? existingGoalProgress?.tokens_used ?? 0),
          tool_calls: Number(taskGoalProgress?.tool_calls ?? existingGoalProgress?.tool_calls ?? 0),
          duration_seconds: Number(taskGoalProgress?.duration_seconds ?? existingGoalProgress?.duration_seconds ?? 0)
        };
      }
      restoreDebugLog('restore:running-task-found', {
        taskId: runningTask?.task_id,
        status: runningTask?.status,
        taskConversationId: runningTask?.conversation_id,
        currentConversationId: this.currentConversationId
      });

      // 检查历史是否已加载
      const hasMessages = Array.isArray(this.messages) && this.messages.length > 0;
      const historyLoadingSameConversation =
        !!this.historyLoading && this.historyLoadingFor === this.currentConversationId;

      if (!hasMessages) {
        this._restoreTaskStateWaitCount = (this._restoreTaskStateWaitCount || 0) + 1;
        const waitedTooLong = this._restoreTaskStateWaitCount >= 8; // ~4s
        restoreDebugLog('restore:history-empty', {
          waitCount: this._restoreTaskStateWaitCount,
          waitedTooLong,
          historyLoading: this.historyLoading,
          historyLoadingFor: this.historyLoadingFor,
          currentConversationId: this.currentConversationId
        });
        if (historyLoadingSameConversation && !waitedTooLong) {
          debugLog('[TaskPolling] 历史未加载，等待历史加载完成', {
            waitCount: this._restoreTaskStateWaitCount,
            historyLoading: this.historyLoading,
            historyLoadingFor: this.historyLoadingFor
          });
          setTimeout(() => {
            this.restoreTaskState();
          }, 500);
          return;
        }
        // 兜底：即使历史未成功拉取，也允许直接通过任务事件重放恢复界面，
        // 避免 show_html 流式阶段刷新后“本次请求内容完全消失”。
        debugLog('[TaskPolling] 历史为空，启用事件重放兜底恢复', {
          waitCount: this._restoreTaskStateWaitCount,
          historyLoading: this.historyLoading,
          historyLoadingFor: this.historyLoadingFor
        });
      } else {
        this._restoreTaskStateWaitCount = 0;
      }

      debugLog('[TaskPolling] 开始精细恢复', {
        hasMessages,
        historyLoading: this.historyLoading,
        historyLoadingFor: this.historyLoadingFor
      });

      // 获取任务的所有事件
      const detailResponse = await fetch(`/api/tasks/${taskStore.currentTaskId}`);
      if (!detailResponse.ok) {
        debugLog('[TaskPolling] 获取任务详情失败');
        return;
      }

      const detailResult = await detailResponse.json();
      if (!detailResult.success || !detailResult.data.events) {
        restoreDebugLog('restore:task-detail-invalid', {
          ok: !!detailResult?.success,
          hasData: !!detailResult?.data,
          hasEvents: !!detailResult?.data?.events
        });
        debugLog('[TaskPolling] 任务详情无效');
        return;
      }

      const allEvents = detailResult.data.events;
      debugLog(`[TaskPolling] 获取到 ${allEvents.length} 个事件`);
      restoreDebugLog('restore:task-detail-events', {
        total: allEvents.length,
        firstIdx: allEvents[0]?.idx,
        firstType: allEvents[0]?.type,
        lastIdx: allEvents[allEvents.length - 1]?.idx,
        lastType: allEvents[allEvents.length - 1]?.type
      });

      // 找到最后一条消息
      const lastMessage = this.messages[this.messages.length - 1];
      const isAssistantMessage = lastMessage && lastMessage.role === 'assistant';

      // console.log('[TaskPolling] 最后一条消息:', {
      //     exists: !!lastMessage,
      //     role: lastMessage?.role,
      //     actionsCount: lastMessage?.actions?.length || 0,
      //     isAssistant: isAssistantMessage
      // });

      // 先分析事件状态（用于判定是否强制重建）
      let inThinking = false;
      let inText = false;
      let hasTextChunkEvent = false;
      let hasAssistantResponseEvent = false;
      let hasAssistantContentEvent = false;

      for (let i = 0; i < allEvents.length; i++) {
        const event = allEvents[i];
        const eventType = String(event?.type || '');
        if (
          [
            'ai_message_start',
            'thinking_start',
            'thinking_chunk',
            'thinking_end',
            'text_start',
            'text_chunk',
            'text_end',
            'tool_preparing',
            'tool_start',
            'tool_update',
            'tool_complete'
          ].includes(eventType)
        ) {
          hasAssistantResponseEvent = true;
        }
        if (
          [
            'thinking_chunk',
            'text_chunk',
            'tool_preparing',
            'tool_start',
            'tool_update',
            'tool_complete'
          ].includes(eventType)
        ) {
          hasAssistantContentEvent = true;
        }
        if (event.type === 'thinking_start') {
          inThinking = true;
        } else if (event.type === 'thinking_end') {
          inThinking = false;
        }
        if (event.type === 'text_start') {
          inText = true;
        } else if (event.type === 'text_end') {
          inText = false;
        }
        if (event.type === 'text_chunk') {
          hasTextChunkEvent = true;
        }
      }

      // 检查是否需要从头重建
      // 1. 最后一条不是 assistant 消息
      // 2. 最后一条是空的 assistant 消息
      // 3. 事件数量远大于历史中的 actions 数量（说明历史不完整）
      // 4. 任务正处于文本流式阶段（刷新时易丢分段内容，强制重放全部事件）
      const forceRebuildForStreamingText = inText || hasTextChunkEvent || inThinking;

      // 检查 assistant 消息的 actions 是否有仍在进行中的
      const hasInProgressActions = isAssistantMessage && Array.isArray(lastMessage.actions) && lastMessage.actions.some((action: any) => {
        if (!action) return false;
        if (action.streaming) return true;
        if (action.type === 'tool' && action.tool) {
          const status = String(action.tool.status || '').toLowerCase();
          if (['preparing', 'running', 'pending', 'queued'].includes(status)) return true;
        }
        return false;
      });

      // 仅当确实需要时才重建，避免引导消息等场景下不必要的 assistant 消息移除和事件重放
      const needsRebuild =
        !isAssistantMessage ||
        (isAssistantMessage && (!lastMessage.actions || lastMessage.actions.length === 0)) ||
        forceRebuildForStreamingText ||
        hasInProgressActions;
      restoreDebugLog('restore:rebuild-decision', {
        needsRebuild,
        isAssistantMessage,
        hasAssistantResponseEvent,
        hasAssistantContentEvent,
        inText,
        hasTextChunkEvent,
        inThinking,
        forceRebuildForStreamingText,
        hasInProgressActions
      });

      if (needsRebuild) {
        debugLog('[TaskPolling] 需要从头重建 assistant 响应');

        // 关键修复：不要 pop assistant 消息，而是清空其 actions 并设置标记，
        // 让 handleAiMessageStart 检测到 isRefreshRestore 并复用现有消息容器。
        // 这样避免视觉闪回（消息不会突然消失）+ 新事件通过轮询增量追加，不会一次性同步处理几百个事件导致卡死。
        if (isAssistantMessage) {
          debugLog('[TaskPolling] 复用现有 assistant 消息容器，清空 actions 等待事件重建', {
            hasAssistantContentEvent
          });
          lastMessage.actions = [];
          // 如果事件流中已经有内容事件（tool/thinking/text），说明回复已经开始过，
          // 重放事件会恢复内容，不显示等待提示。
          // 如果还没有内容事件，说明回复还没开始，应该显示等待提示。
          lastMessage.awaitingFirstContent = !hasAssistantContentEvent;
          lastMessage.generatingLabel = lastMessage.generatingLabel || '思考中...';
          // 清理旧的流式标记，确保新事件能正确设置
          if (typeof lastMessage.streaming === 'boolean') {
            lastMessage.streaming = true;
          }
        }

        this.streamingMessage = true;
        this.taskInProgress = true;
        if (!hasAssistantContentEvent && !isAssistantMessage) {
          this.ensureRunningAssistantPlaceholder?.(runningTask, 'restore:no-visible-content-yet');
        }
        this.$forceUpdate();

        // 重置偏移量为 0，从头获取所有事件来重建 assistant 消息
        taskStore.lastEventIndex = 0;
        debugLog('[TaskPolling] 重置偏移量为 0，从头开始轮询');

        // 标记正在从头重建，用于后续处理
        this._rebuildingFromScratch = true;
        this._rebuildingEventCount = allEvents.length; // 记录当前事件总数

        (window as any).__taskEventHandler = (event: any) => {
          this.handleTaskEvent(event);
        };

        taskStore.startPolling((event: any) => {
          this.handleTaskEvent(event);
        });
        restoreDebugLog('restore:rebuild-polling-started', {
          lastEventIndex: taskStore.lastEventIndex,
          currentConversationId: this.currentConversationId
        });

        // 延迟清除重建标记，确保所有历史事件都处理完毕
        setTimeout(() => {
          debugLog('[TaskPolling] 历史事件处理完毕，清除重建标记');
          this._rebuildingFromScratch = false;
          this._rebuildingEventCount = 0;
        }, 2000);

        return;
      }

      // console.log('[TaskPolling] 分析结果:', {
      //     inThinking,
      //     inText,
      //     totalEvents: allEvents.length,
      //     lastEventType: allEvents[allEvents.length - 1]?.type,
      //     lastEventIdx: allEvents[allEvents.length - 1]?.idx
      // });

      // 恢复思考块状态
      if (lastMessage.actions) {
        // console.log('[TaskPolling] 历史中的 actions 详情:', lastMessage.actions.map((a, idx) => ({
        //     index: idx,
        //     type: a.type,
        //     id: a.id,
        //     hasContent: !!a.content,
        //     contentLength: a.content?.length || 0,
        //     toolName: a.tool?.name,
        //     hasBlockId: !!a.blockId,
        //     blockId: a.blockId,
        //     collapsed: a.collapsed,
        //     streaming: a.streaming
        // })));

        const thinkingActions = lastMessage.actions.filter((a) => a.type === 'thinking');
        // console.log('[TaskPolling] 思考块数量:', thinkingActions.length);

        if (inThinking && thinkingActions.length > 0) {
          // 正在思考中，检查最后一个思考块是否正在流式输出
          const lastThinking = thinkingActions[thinkingActions.length - 1];

          // 只有当思考块正在流式输出时才设置锁定状态（但不展开）
          if (lastThinking.streaming && lastThinking.blockId) {
            // console.log('[TaskPolling] 找到正在流式输出的思考块，设置锁定状态:', lastThinking.blockId);
            this.$nextTick(() => {
              this.chatSetThinkingLock(lastThinking.blockId, true);
            });
          }
        }

        // 确保所有思考块都是折叠状态
        for (const thinking of thinkingActions) {
          if (thinking.blockId) {
            thinking.collapsed = true;
          }
        }

        // 检查思考块状态（在设置之后）（已禁用）
        // thinkingActions.forEach((thinking, idx) => {
        //     console.log(`[TaskPolling] 思考块 ${idx} (设置后):`, {
        //         hasBlockId: !!thinking.blockId,
        //         blockId: thinking.blockId,
        //         collapsed: thinking.collapsed,
        //         contentLength: thinking.content?.length || 0
        //     });
        // });

        // 恢复文本块状态
        const textActions = lastMessage.actions.filter((a) => a.type === 'text');
        // console.log('[TaskPolling] 文本块数量:', textActions.length);

        if (inText && textActions.length > 0) {
          const lastText = textActions[textActions.length - 1];
          // console.log('[TaskPolling] 标记文本块为流式状态');
          lastText.streaming = true;
        }

        // 注册历史中的工具块到 toolActionIndex
        // 这样后续的 update_action 事件可以找到对应的块进行状态更新
        const toolActions = lastMessage.actions.filter((a) => a.type === 'tool');
        // console.log('[TaskPolling] 工具块数量:', toolActions.length);

        for (const toolAction of toolActions) {
          if (toolAction.tool && toolAction.tool.id) {
            // console.log('[TaskPolling] 注册工具块:', {
            //     id: toolAction.tool.id,
            //     name: toolAction.tool.name,
            //     status: toolAction.tool.status
            // });
            // 注册到 toolActionIndex
            this.toolRegisterAction(toolAction, toolAction.tool.id);
            // 如果有 executionId，也注册
            if (toolAction.tool.executionId) {
              this.toolRegisterAction(toolAction, toolAction.tool.executionId);
            }
            // 追踪工具调用
            if (toolAction.tool.name) {
              this.toolTrackAction(toolAction.tool.name, toolAction);
            }
          }
        }
      }

      // 标记状态为进行中
      this.streamingMessage = true;
      this.taskInProgress = true;

      // 设置 currentMessageIndex 指向最后一条 assistant 消息
      // 这样后续添加的 action 会添加到正确的消息中
      const lastMessageIndex = this.messages.length - 1;
      if (lastMessage && lastMessage.role === 'assistant') {
        this.currentMessageIndex = lastMessageIndex;
        // console.log('[TaskPolling] 设置 currentMessageIndex 为:', lastMessageIndex);
      }

      // 强制更新界面
      this.$forceUpdate();

      // 滚动到底部
      this.$nextTick(() => {
        this.conditionalScrollToBottom();
      });

      // 注册事件处理器到全局
      (window as any).__taskEventHandler = (event: any) => {
        this.handleTaskEvent(event);
      };

      // 启动轮询（从当前偏移量开始，只处理新事件）
      debugLog('[TaskPolling] 启动轮询，起始偏移量:', taskStore.lastEventIndex);

      taskStore.startPolling((event: any) => {
        this.handleTaskEvent(event);
      });
      restoreDebugLog('restore:polling-started-follow', {
        lastEventIndex: taskStore.lastEventIndex,
        currentConversationId: this.currentConversationId
      });

      this.uiPushToast({
        title: '任务恢复',
        message: '检测到进行中的任务，已恢复连接',
        type: 'info',
        duration: 3000
      });
    } catch (error) {
      restoreDebugLog('restore:error', {
        message: error?.message || String(error)
      });
      console.error('[TaskPolling] 恢复任务状态失败:', error);
    }
  }
};
