#!/bin/bash

# StackOverflowError 修复测试脚本
# 使用方法: ./test_fix.sh

echo "======================================"
echo "  Monisuo StackOverflowError 修复测试"
echo "======================================"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查 Java 环境
echo "1️⃣  检查 Java 环境..."
if ! command -v java &> /dev/null; then
    echo -e "${RED}❌ 未找到 Java 环境${NC}"
    echo "请先安装 Java: https://www.java.com/download/"
    exit 1
fi

JAVA_VERSION=$(java -version 2>&1 | head -n 1)
echo -e "${GREEN}✅ Java 环境: $JAVA_VERSION${NC}"
echo ""

# 编译项目
echo "2️⃣  编译项目..."
cd "$(dirname "$0")"

if [ ! -f "pom.xml" ]; then
    echo -e "${RED}❌ 未找到 pom.xml${NC}"
    exit 1
fi

mvn clean package -DskipTests
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ 编译失败${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 编译成功${NC}"
echo ""

# 检查是否有旧的进程在运行
echo "3️⃣  检查旧进程..."
OLD_PID=$(ps aux | grep 'monisuo-1.0.jar' | grep -v grep | awk '{print $2}')
if [ ! -z "$OLD_PID" ]; then
    echo -e "${YELLOW}⚠️  发现旧进程 (PID: $OLD_PID)，正在停止...${NC}"
    kill -9 $OLD_PID
    sleep 2
fi
echo -e "${GREEN}✅ 没有旧进程${NC}"
echo ""

# 启动服务
echo "4️⃣  启动服务..."
nohup java -jar target/monisuo-1.0.jar > app.log 2>&1 &
APP_PID=$!
echo -e "${GREEN}✅ 服务已启动 (PID: $APP_PID)${NC}"
echo "⏳ 等待服务启动..."
sleep 10
echo ""

# 检查服务状态
echo "5️⃣  检查服务状态..."
if ps -p $APP_PID > /dev/null; then
    echo -e "${GREEN}✅ 服务正在运行${NC}"
else
    echo -e "${RED}❌ 服务启动失败${NC}"
    echo "查看日志: tail -100 app.log"
    exit 1
fi
echo ""

# 检查启动日志
echo "6️⃣  检查启动日志..."
if tail -50 app.log | grep -q "Started SpcCloudApplication"; then
    echo -e "${GREEN}✅ 服务启动成功${NC}"
elif tail -50 app.log | grep -q "Error\|Exception"; then
    echo -e "${RED}❌ 启动过程中发现错误${NC}"
    echo "错误日志:"
    tail -50 app.log | grep -A 5 "Error\|Exception"
    exit 1
else
    echo -e "${YELLOW}⚠️  未找到启动成功标志，请手动检查${NC}"
fi
echo ""

# 测试接口
echo "7️⃣  测试核心接口（需要登录 Token）..."
echo "注意: 以下测试需要有效的 Authorization Token"
echo ""

read -p "是否要测试接口？(y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "跳过接口测试"
    echo ""
    echo "======================================"
    echo -e "${GREEN}✅ 修复验证完成${NC}"
    echo "服务正在运行，PID: $APP_PID"
    echo "日志文件: app.log"
    echo "======================================"
    exit 0
fi

read -p "请输入 Authorization Token: " TOKEN

if [ -z "$TOKEN" ]; then
    echo -e "${YELLOW}⚠️  未提供 Token，跳过接口测试${NC}"
else
    echo ""
    echo "测试 /api/asset/overview..."
    RESPONSE=$(curl -s -w "\n%{http_code}" -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/asset/overview)
    HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
    BODY=$(echo "$RESPONSE" | head -n -1)
    
    if [ "$HTTP_CODE" = "200" ]; then
        echo -e "${GREEN}✅ /api/asset/overview - 成功${NC}"
        echo "响应: $BODY"
    else
        echo -e "${RED}❌ /api/asset/overview - 失败 (HTTP $HTTP_CODE)${NC}"
        echo "响应: $BODY"
    fi
    echo ""
    
    echo "测试 /api/asset/trade..."
    RESPONSE=$(curl -s -w "\n%{http_code}" -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/asset/trade)
    HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
    BODY=$(echo "$RESPONSE" | head -n -1)
    
    if [ "$HTTP_CODE" = "200" ]; then
        echo -e "${GREEN}✅ /api/asset/trade - 成功${NC}"
        echo "响应: $BODY"
    else
        echo -e "${RED}❌ /api/asset/trade - 失败 (HTTP $HTTP_CODE)${NC}"
        echo "响应: $BODY"
    fi
    echo ""
    
    echo "测试 /api/asset/fund..."
    RESPONSE=$(curl -s -w "\n%{http_code}" -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/asset/fund)
    HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
    BODY=$(echo "$RESPONSE" | head -n -1)
    
    if [ "$HTTP_CODE" = "200" ]; then
        echo -e "${GREEN}✅ /api/asset/fund - 成功${NC}"
        echo "响应: $BODY"
    else
        echo -e "${RED}❌ /api/asset/fund - 失败 (HTTP $HTTP_CODE)${NC}"
        echo "响应: $BODY"
    fi
    echo ""
fi

echo "======================================"
echo -e "${GREEN}✅ 所有测试完成${NC}"
echo "服务正在运行，PID: $APP_PID"
echo "停止服务: kill $APP_PID"
echo "查看日志: tail -f app.log"
echo "======================================"
