import React, { useState } from 'react';
import { 
  ArrowLeft,
  Play,
  CheckCircle,
  XCircle,
  Clock,
  MessageCircle
} from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import { 
  testConnection, 
  testConfig, 
  testSimpleQuery, 
  testContextRetention,
  runAllTests 
} from '../../utils/testChatFunction';

/**
 * 聊天功能测试页面
 * 用于验证 RAG Grounding 服务和聊天界面是否正常工作
 */
const ChatTestPage: React.FC = () => {
  const navigate = useNavigate();
  const [testResults, setTestResults] = useState<Record<string, 'idle' | 'running' | 'success' | 'error'>>({});
  const [testOutputs, setTestOutputs] = useState<Record<string, string>>({});

  // 运行单个测试
  const runTest = async (testName: string, testFn: () => Promise<boolean>) => {
    setTestResults(prev => ({ ...prev, [testName]: 'running' }));
    setTestOutputs(prev => ({ ...prev, [testName]: '正在运行测试...' }));

    try {
      const success = await testFn();
      setTestResults(prev => ({ ...prev, [testName]: success ? 'success' : 'error' }));
      setTestOutputs(prev => ({ 
        ...prev, 
        [testName]: success ? '测试通过' : '测试失败，请查看控制台获取详细信息' 
      }));
    } catch (error) {
      setTestResults(prev => ({ ...prev, [testName]: 'error' }));
      setTestOutputs(prev => ({ 
        ...prev, 
        [testName]: `测试异常: ${error instanceof Error ? error.message : '未知错误'}` 
      }));
    }
  };

  // 运行所有测试
  const runAllTestsHandler = async () => {
    const tests = ['connection', 'config', 'simpleQuery', 'contextRetention'];
    tests.forEach(test => {
      setTestResults(prev => ({ ...prev, [test]: 'running' }));
      setTestOutputs(prev => ({ ...prev, [test]: '正在运行测试...' }));
    });

    try {
      await runAllTests();
      // 这里我们假设所有测试都成功了，实际应该根据 runAllTests 的返回值来判断
      tests.forEach(test => {
        setTestResults(prev => ({ ...prev, [test]: 'success' }));
        setTestOutputs(prev => ({ ...prev, [test]: '测试完成，请查看控制台获取详细结果' }));
      });
    } catch (error) {
      tests.forEach(test => {
        setTestResults(prev => ({ ...prev, [test]: 'error' }));
        setTestOutputs(prev => ({ ...prev, [test]: '测试失败，请查看控制台获取详细信息' }));
      });
    }
  };

  const tests = [
    {
      id: 'connection',
      name: '连接测试',
      description: '测试与 RAG Grounding 服务的连接',
      fn: testConnection
    },
    {
      id: 'config',
      name: '配置测试',
      description: '获取和验证服务配置信息',
      fn: testConfig
    },
    {
      id: 'simpleQuery',
      name: '简单查询测试',
      description: '测试基本的问答功能',
      fn: testSimpleQuery
    },
    {
      id: 'contextRetention',
      name: '上下文保持测试',
      description: '测试对话上下文是否正确保持',
      fn: testContextRetention
    }
  ];

  const getStatusIcon = (status: string) => {
    switch (status) {
      case 'running':
        return <Clock className="w-4 h-4 text-yellow-500 animate-spin" />;
      case 'success':
        return <CheckCircle className="w-4 h-4 text-green-500" />;
      case 'error':
        return <XCircle className="w-4 h-4 text-red-500" />;
      default:
        return <div className="w-4 h-4 border-2 border-gray-300 rounded-full" />;
    }
  };

  const getStatusColor = (status: string) => {
    switch (status) {
      case 'running':
        return 'border-yellow-200 bg-yellow-50';
      case 'success':
        return 'border-green-200 bg-green-50';
      case 'error':
        return 'border-red-200 bg-red-50';
      default:
        return 'border-gray-200 bg-white';
    }
  };

  return (
    <div className="h-full flex flex-col animate-fade-in">
      {/* 页面头部 */}
      <div className="flex items-center justify-between p-6 border-b border-gray-200 bg-white">
        <div className="flex items-center gap-4">
          <button
            onClick={() => navigate('/tools')}
            className="p-2 text-gray-500 hover:text-gray-700 hover:bg-gray-100 rounded-lg transition-all duration-200"
            title="返回工具列表"
          >
            <ArrowLeft className="w-5 h-5" />
          </button>
          
          <div className="flex items-center gap-3">
            <div className="w-12 h-12 bg-gradient-to-br from-green-500 to-blue-600 rounded-xl flex items-center justify-center shadow-lg">
              <MessageCircle className="w-6 h-6 text-white" />
            </div>
            <div>
              <h1 className="text-2xl font-bold text-gray-900">聊天功能测试</h1>
              <p className="text-gray-600">验证 AI 聊天功能是否正常工作</p>
            </div>
          </div>
        </div>

        <button
          onClick={runAllTestsHandler}
          className="flex items-center gap-2 px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors duration-200"
        >
          <Play className="w-4 h-4" />
          运行所有测试
        </button>
      </div>

      {/* 测试列表 */}
      <div className="flex-1 p-6 space-y-4">
        <div className="mb-6 p-4 bg-blue-50 border border-blue-200 rounded-lg">
          <h3 className="font-medium text-blue-800 mb-2">测试说明</h3>
          <p className="text-sm text-blue-700">
            这些测试将验证 AI 聊天功能的各个方面。请确保网络连接正常，并查看浏览器控制台获取详细的测试输出。
          </p>
        </div>

        {tests.map((test) => (
          <div
            key={test.id}
            className={`p-4 border rounded-lg transition-all duration-200 ${getStatusColor(testResults[test.id] || 'idle')}`}
          >
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-3">
                {getStatusIcon(testResults[test.id] || 'idle')}
                <div>
                  <h3 className="font-medium text-gray-900">{test.name}</h3>
                  <p className="text-sm text-gray-600">{test.description}</p>
                </div>
              </div>
              
              <button
                onClick={() => runTest(test.id, test.fn)}
                disabled={testResults[test.id] === 'running'}
                className="px-3 py-1 text-sm bg-blue-500 text-white rounded hover:bg-blue-600 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors duration-200"
              >
                {testResults[test.id] === 'running' ? '运行中...' : '运行测试'}
              </button>
            </div>
            
            {testOutputs[test.id] && (
              <div className="mt-3 p-3 bg-gray-100 rounded text-sm text-gray-700">
                {testOutputs[test.id]}
              </div>
            )}
          </div>
        ))}

        <div className="mt-8 p-4 bg-gray-50 border border-gray-200 rounded-lg">
          <h3 className="font-medium text-gray-800 mb-2">手动测试</h3>
          <p className="text-sm text-gray-600 mb-3">
            您也可以直接访问 AI 聊天工具进行手动测试：
          </p>
          <button
            onClick={() => navigate('/tools/ai-chat')}
            className="px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 transition-colors duration-200"
          >
            打开 AI 聊天工具
          </button>
        </div>
      </div>
    </div>
  );
};

export default ChatTestPage;
