import { describe, it, expect, beforeEach, vi } from 'vitest';

// 使用可变的引用对象来绕过 hoisting 问题
const mockState = {
  execute: vi.fn(),
};

// Mock agent registry 和 AgentExecutor
vi.mock('../../../../src/agent/index.js', () => {
  // 在 mock 工厂内部定义类
  return {
    agentRegistry: {
      listSubagents: vi.fn(() => [
        { name: 'explore', description: '代码探索', mode: 'subagent' },
        { name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
      ]),
      get: vi.fn(),
    },
    AgentExecutor: class {
      execute(...args: any[]) {
        return mockState.execute(...args);
      }
    },
  };
});

// Mock tool registry
vi.mock('../../../../src/tools/registry.js', () => ({
  toolRegistry: {},
}));

// Mock session manager
vi.mock('../../../../src/session/index.js', () => ({
  SessionManager: vi.fn(),
}));

import { taskTool, initTaskContext, updateTaskDescription } from '../../../../src/tools/task/task.js';
import { agentRegistry } from '../../../../src/agent/index.js';

describe('taskTool - Task 工具', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    // 重置上下文为 null
    initTaskContext(null as any, null as any);
    // 重置 mock
    mockState.execute.mockResolvedValue({
      success: true,
      text: '任务完成',
      steps: 3,
    });
  });

  describe('工具定义', () => {
    it('有正确的名称', () => {
      expect(taskTool.name).toBe('task');
    });

    it('有正确的元数据', () => {
      expect(taskTool.metadata.category).toBe('agent');
      expect(taskTool.metadata.keywords).toContain('task');
      expect(taskTool.metadata.keywords).toContain('subagent');
    });

    it('定义了必需的参数', () => {
      expect(taskTool.parameters.description.required).toBe(true);
      expect(taskTool.parameters.prompt.required).toBe(true);
      expect(taskTool.parameters.subagent_type.required).toBe(true);
    });
  });

  describe('initTaskContext - 初始化上下文', () => {
    it('设置上下文不报错', () => {
      const mockConfig = { model: 'test' };
      const mockSession = {
        getSessionId: vi.fn(() => 'session-id'),
        createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
        saveChildSession: vi.fn(),
      };

      expect(() => initTaskContext(mockConfig as any, mockSession as any)).not.toThrow();
    });
  });

  describe('updateTaskDescription - 更新描述', () => {
    it('更新工具描述', () => {
      vi.mocked(agentRegistry.listSubagents).mockReturnValue([
        { name: 'explore', description: '代码探索', mode: 'subagent' },
        { name: 'code-reviewer', description: '代码审查', mode: 'subagent' },
      ]);

      updateTaskDescription();

      expect(taskTool.description).toContain('explore');
      expect(taskTool.description).toContain('code-reviewer');
    });

    it('无子 Agent 时显示提示', () => {
      vi.mocked(agentRegistry.listSubagents).mockReturnValue([]);

      updateTaskDescription();

      expect(taskTool.description).toContain('没有可用');
    });
  });

  describe('execute - 执行', () => {
    it('未初始化上下文时返回错误', async () => {
      // 确保上下文为 null
      initTaskContext(null as any, null as any);
      vi.mocked(agentRegistry.get).mockReturnValue(undefined);

      const result = await taskTool.execute({
        description: 'test task',
        prompt: 'do something',
        subagent_type: 'explore',
      });

      expect(result.success).toBe(false);
      // 可能是未初始化或未找到 Agent
      expect(result.error).toBeDefined();
    });

    it('成功执行子任务', async () => {
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({
          id: 'child-session',
          messages: [],
        })),
        saveChildSession: vi.fn(),
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue({
        name: 'explore',
        description: '探索 Agent',
        mode: 'subagent',
        prompt: '你是探索助手',
      });

      const result = await taskTool.execute({
        description: 'search code',
        prompt: 'find all API routes',
        subagent_type: 'explore',
      });

      expect(result.success).toBe(true);
      expect(result.output).toContain('任务完成');
    });

    it('未找到 Agent 时返回错误', async () => {
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
        saveChildSession: vi.fn(),
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue(undefined);
      vi.mocked(agentRegistry.listSubagents).mockReturnValue([
        { name: 'explore', description: '探索', mode: 'subagent' },
      ]);

      const result = await taskTool.execute({
        description: 'test',
        prompt: 'test',
        subagent_type: 'nonexistent',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('未找到 Agent');
    });

    it('primary 模式 Agent 不能作为子任务', async () => {
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
        saveChildSession: vi.fn(),
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue({
        name: 'primary-agent',
        description: '主 Agent',
        mode: 'primary',
        prompt: '你是主助手',
      });

      const result = await taskTool.execute({
        description: 'test',
        prompt: 'test',
        subagent_type: 'primary-agent',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('primary 模式');
    });

    it('子任务失败时返回错误', async () => {
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({ id: 'child', messages: [] })),
        saveChildSession: vi.fn(),
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue({
        name: 'explore',
        description: '探索 Agent',
        mode: 'subagent',
        prompt: '你是探索助手',
      });

      mockState.execute.mockResolvedValue({
        success: false,
        text: '',
        error: '执行失败',
        steps: 1,
      });

      const result = await taskTool.execute({
        description: 'test',
        prompt: 'test',
        subagent_type: 'explore',
      });

      expect(result.success).toBe(false);
      expect(result.error).toContain('执行失败');
    });

    it('返回元数据', async () => {
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({
          id: 'child-session-123',
          messages: [],
        })),
        saveChildSession: vi.fn(),
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue({
        name: 'explore',
        description: '探索 Agent',
        mode: 'subagent',
        prompt: '你是探索助手',
      });

      mockState.execute.mockResolvedValue({
        success: true,
        text: '完成',
        steps: 5,
      });

      const result = await taskTool.execute({
        description: 'test',
        prompt: 'test',
        subagent_type: 'explore',
      });

      expect(result.metadata).toBeDefined();
      expect(result.metadata?.agent).toBe('explore');
      expect(result.metadata?.sessionId).toBe('child-session-123');
      expect(result.metadata?.steps).toBe(5);
    });

    it('保存子会话', async () => {
      const saveChildSession = vi.fn();
      const mockSession = {
        getSessionId: vi.fn(() => 'parent-session'),
        createChildSession: vi.fn(() => ({
          id: 'child-session',
          messages: [],
        })),
        saveChildSession,
      };

      initTaskContext({ model: 'test' } as any, mockSession as any);

      vi.mocked(agentRegistry.get).mockReturnValue({
        name: 'explore',
        description: '探索 Agent',
        mode: 'subagent',
        prompt: '你是探索助手',
      });

      mockState.execute.mockResolvedValue({
        success: true,
        text: '任务结果',
        steps: 2,
      });

      await taskTool.execute({
        description: 'test',
        prompt: 'test prompt',
        subagent_type: 'explore',
      });

      expect(saveChildSession).toHaveBeenCalled();
    });
  });
});
