#!/bin/bash
# 最终修复 vnpy.app 模块问题

echo "🚀 最终修复 vnpy.app 模块问题..."
echo "============================================================"

# 1. 检查当前状态
echo "1. 检查当前状态..."
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy python3 -c \"
import sys
print('Python路径:', sys.executable)

try:
    import vnpy
    print('✅ vnpy版本:', getattr(vnpy, '__version__', '未知'))
except ImportError as e:
    print(f'❌ vnpy未安装: {e}')

try:
    from vnpy.app.cta_strategy import CtaTemplate
    print('✅ vnpy.app.cta_strategy 已存在')
except ImportError as e:
    print(f'❌ vnpy.app.cta_strategy 缺失: {e}')
\"" 2>&1 | grep -v "DeprecationWarning"

# 2. 创建兼容性模块并测试
echo -e "\n2. 创建兼容性模块..."
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy python3 /app/scripts/vnpy_app_compatibility.py"

# 3. 修改 test_server_fixed.py 添加兼容性导入
echo -e "\n3. 修改服务代码..."
cat > /tmp/test_server_with_compatibility.py << 'EOF'
#!/usr/bin/env python3
"""
修复后的 test_server.py - 包含 vnpy.app 兼容性
"""

import sys
import os

# 首先加载兼容性模块
sys.path.insert(0, '/app/scripts')
try:
    import vnpy_app_compatibility
    print("✅ vnpy.app 兼容性模块已加载")
except ImportError as e:
    print(f"❌ 无法加载兼容性模块: {e}")

# 现在导入其他模块
import traceback
import zmq
import time

# 测试导入
try:
    from vnpy.app.cta_strategy import CtaTemplate
    print("✅ 成功导入 CtaTemplate (从 vnpy.app.cta_strategy)")
except ImportError as e:
    print(f"❌ 无法导入 CtaTemplate: {e}")
    # 尝试直接导入
    try:
        from vnpy_ctastrategy import CtaTemplate
        print("✅ 成功导入 CtaTemplate (从 vnpy_ctastrategy)")
    except ImportError as e2:
        print(f"❌ 完全无法导入 CtaTemplate: {e2}")
        sys.exit(1)

def run_strategy_backtest(strategy_code: str, symbol: str, interval: str, start: int, end: int, **kwargs):
    """RPC方法：运行策略回测"""
    try:
        print(f"开始回测: {symbol} {start}-{end}")
        
        # 动态加载策略
        local_vars = {}
        exec(strategy_code, globals(), local_vars)
        
        # 查找CtaTemplate子类
        from vnpy_ctastrategy import CtaTemplate as CtaTemplateReal
        strategy_classes = [v for k, v in local_vars.items() 
                          if isinstance(v, type) and issubclass(v, CtaTemplateReal) and v != CtaTemplateReal]
        
        if not strategy_classes:
            return {"error": "策略代码中未找到CtaTemplate子类"}
        
        StrategyClass = strategy_classes[0]
        print(f"找到策略类: {StrategyClass.__name__}")
        
        # 创建事件引擎
        from vnpy.event import EventEngine
        event_engine = EventEngine()
        
        # 创建主引擎
        from vnpy.trader.engine import MainEngine
        main_engine = MainEngine(event_engine)
        
        # 添加回测引擎
        from vnpy_ctabacktester import BacktesterEngine
        backtester_engine = main_engine.add_app(BacktesterEngine, "backtester")
        
        # 设置回测参数
        setting = {
            "vt_symbol": symbol,
            "interval": interval,
            "start": start,
            "end": end,
            "rate": kwargs.get("rate", 0.00003),
            "slippage": kwargs.get("slippage", 0.2),
            "size": kwargs.get("size", 1),
            "pricetick": kwargs.get("pricetick", 0.2),
            "capital": kwargs.get("capital", 1000000),
        }
        
        print(f"回测参数: {setting}")
        
        # 运行回测
        backtester_engine.run_backtesting(
            strategy_class=StrategyClass,
            setting=setting
        )
        
        # 获取结果
        result = {
            "statistics": backtester_engine.get_statistics(),
            "result_df": backtester_engine.get_result_df().to_dict(orient="records"),
            "trades": backtester_engine.get_all_trades()
        }
        
        print(f"回测完成，统计项数: {len(result['statistics'])}")
        return result
        
    except Exception as e:
        error_info = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        print(f"回测错误: {error_info}")
        return error_info

def main():
    """主函数"""
    print('🚀 启动兼容性修复后的 WebTrader RPC 服务...')
    
    # 创建ZMQ REP socket
    context = zmq.Context()
    rep_socket = context.socket(zmq.REP)
    rep_socket.bind("tcp://0.0.0.0:8001")
    
    print('✅ RPC服务已启动')
    print('   REP地址: tcp://0.0.0.0:8001')
    print('   外部访问: tcp://192.168.2.154:8001')
    print('   等待请求...')
    
    # 处理请求
    while True:
        try:
            # 接收请求
            req = rep_socket.recv_pyobj()
            print(f"收到请求: {req.get('function')}")
            
            function_name = req.get("function")
            args = req.get("args", [])
            kwargs = req.get("kwargs", {})
            
            if function_name == "run_strategy_backtest":
                result = run_strategy_backtest(*args, **kwargs)
            else:
                result = {"error": f"未知函数: {function_name}"}
            
            # 发送响应
            rep_socket.send_pyobj(result)
            print(f"请求处理完成")
            
        except Exception as e:
            error_result = {
                "error": str(e),
                "traceback": traceback.format_exc()
            }
            rep_socket.send_pyobj(error_result)
            print(f"处理请求时出错: {e}")

if __name__ == '__main__':
    main()
EOF

# 4. 复制到容器
echo -e "\n4. 更新服务代码..."
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c 'cat > /app/scripts/test_server_with_compatibility.py' " < /tmp/test_server_with_compatibility.py

# 5. 重启服务
echo -e "\n5. 重启服务..."
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy bash -c '
    echo \"停止旧服务...\"
    pkill -f test_server 2>/dev/null
    pkill -f backtest_api 2>/dev/null
    sleep 2
    
    echo \"启动兼容性RPC服务...\"
    python3 /app/scripts/test_server_with_compatibility.py &
    RPC_PID=\$!
    sleep 3
    
    echo \"启动API服务...\"
    python3 /app/scripts/backtest_api_fixed.py &
    API_PID=\$!
    sleep 3
    
    echo \"检查服务状态...\"
    if ps -p \$RPC_PID >/dev/null 2>&1; then
        echo \"✅ RPC服务正在运行 (PID: \$RPC_PID)\"
    else
        echo \"❌ RPC服务启动失败\"
    fi
    
    if ps -p \$API_PID >/dev/null 2>&1; then
        echo \"✅ API服务正在运行 (PID: \$API_PID)\"
    else
        echo \"❌ API服务启动失败\"
    fi
'" 2>&1

# 6. 验证修复
echo -e "\n6. 验证修复..."
sleep 3

echo -n "API服务状态: "
curl -s -I http://192.168.2.154:8088/docs 2>&1 | grep -q "200 OK" && echo "✅ 正常" || echo "❌ 异常"

echo -n "ZMQ服务状态: "
timeout 2 bash -c "echo >/dev/tcp/192.168.2.154/8001" 2>/dev/null && echo "✅ 正常" || echo "❌ 异常"

# 7. 最终测试
echo -e "\n7. 最终测试..."
cat > /tmp/final_test.py << 'EOF'
#!/usr/bin/env python3
"""
最终测试 - 验证 vnpy.app 模块修复
"""

import sys

print("测试 vnpy.app 模块修复...")
print("="*60)

# 测试1: 直接导入
print("1. 测试直接导入...")
try:
    from vnpy.app.cta_strategy import CtaTemplate
    print("  ✅ from vnpy.app.cta_strategy import CtaTemplate")
except ImportError as e:
    print(f"  ❌ 导入失败: {e}")

# 测试2: 通过兼容性模块
print("\n2. 测试兼容性模块...")
try:
    import sys
    sys.path.insert(0, '/app/scripts')
    import vnpy_app_compatibility
    print("  ✅ vnpy_app_compatibility 导入成功")
except ImportError as e:
    print(f"  ❌ 兼容性模块失败: {e}")

# 测试3: 再次尝试导入
print("\n3. 再次尝试导入 vnpy.app...")
try:
    from vnpy.app.cta_strategy import CtaTemplate
    print("  ✅ 成功导入 CtaTemplate")
    
    # 测试创建实例
    print("  ✅ 可以正常使用 vnpy.app.cta_strategy 模块")
except Exception as e:
    print(f"  ❌ 最终失败: {e}")

print("\n" + "="*60)
print("测试完成")
EOF

echo "运行最终测试..."
ssh admin@192.168.2.154 "export PATH=\$PATH:/var/packages/Docker/target/usr/bin && docker exec sanguo_vnpy python3 -c \"$(cat /tmp/final_test.py | sed 's/"/\\\"/g')\"" 2>&1

# 8. 清理
rm -f /tmp/test_server_with_compatibility.py /tmp/final_test.py

echo -e "\n============================================================"
echo "修复完成！"
echo "请通知司马懿将军运行测试："
echo "cd pangtong-value/research/task-20260329-strategy-backtest/simayi"
echo "python3 backtest_workflow.py"
echo ""
echo "如果还有问题，请检查："
echo "1. 策略代码是否使用 'from vnpy.app.cta_strategy import CtaTemplate'"
echo "2. 如果使用了，现在应该可以正常导入了"
echo "3. 如果没有，请更新导入为 'from vnpy_ctastrategy import CtaTemplate'"
echo "============================================================"