#!/bin/bash

# Stock Tool - 端到端测试脚本 (PERDAS 增强版)
# 用途：按照 feature_list.json 的步骤执行测试

set -e

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

# 测试结果
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 打印函数
print_test() {
    echo -e "\n${BLUE}📝 测试: $1${NC}"
}

print_pass() {
    echo -e "${GREEN}✅ $1${NC}"
    ((PASSED_TESTS++))
}

print_fail() {
    echo -e "${RED}❌ $1${NC}"
    ((FAILED_TESTS++))
}

print_info() {
    echo -e "${YELLOW}ℹ️  $1${NC}"
}

# 测试前置检查
print_test "检查服务器状态"

if ! pgrep -f "stock-tool-simple" > /dev/null; then
    echo -e "${RED}❌ 服务器未运行${NC}"
    echo "运行 ./init.sh 启动服务器"
    exit 1
fi

print_pass "服务器运行中"

# 测试所有功能
print_test "开始端到端测试"

# 创建截图目录
mkdir -p tests/screenshots

# 测试 1: 股票列表展示
print_test "feat-001 - 股票列表展示"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

if curl -s http://localhost:8080/ | grep -q "股票数据分析工具"; then
    print_pass "网页可访问"
    
    if curl -s http://localhost:8080/ | grep -q "stock-list"; then
        print_pass "股票列表容器存在"
    else
        print_fail "股票列表容器不存在"
    fi
else
    print_fail "网页不可访问"
fi

# 测试 2: 股票搜索
print_test "feat-002 - 股票搜索功能"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

# 获取一个存在的股票代码
FIRST_STOCK=$(curl -s http://localhost:8080/api/stocks 2>/dev/null | jq -r '.[0].symbol' | head -1)

if [ -n "$FIRST_STOCK" ]; then
    print_info "测试股票: $FIRST_STOCK"
    
    # 点击搜索
    RESULT=$(curl -s "http://localhost:8080/api/stock/$FIRST_STOCK" 2>/dev/null)
    
    if echo "$RESULT" | jq -e '.symbol' > /dev/null; then
        print_pass "搜索跳转成功"
        
        if echo "$RESULT" | jq -e '.symbol' | grep -q "$FIRST_STOCK"; then
            print_pass "显示正确股票代码"
        else
            print_fail "股票代码不匹配"
        fi
    else
        print_fail "搜索失败"
    fi
else
    print_fail "无可用股票数据"
fi

# 测试 3: K线图
print_test "feat-003 - K线图展示"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

if curl -s "http://localhost:8080/api/stock/$FIRST_STOCK/kline" 2>/dev/null | jq -e '.[0]' > /dev/null; then
    print_pass "K线数据可获取"
    
    COUNT=$(curl -s "http://localhost:8080/api/stock/$FIRST_STOCK/kline" 2>/dev/null | jq '. | length')
    if [ "$COUNT" -ge 100 ]; then
        print_pass "K线数据完整 ($COUNT 条)"
    else
        print_fail "K线数据不足: $COUNT 条"
    fi
else
    print_fail "K线数据获取失败"
fi

# 测试 4: 技术指标
print_test "feat-004 - 技术指标展示"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

if curl -s "http://localhost:8080/api/stock/$FIRST_STOCK/indicators" 2>/dev/null | jq -e '.[0]' > /dev/null; then
    print_pass "技术指标数据可获取"
    
    COUNT=$(curl -s "http://localhost:8080/api/stock/$FIRST_STOCK/indicators" 2>/dev/null | jq '. | length')
    if [ "$COUNT" -ge 100 ]; then
        print_pass "技术指标数据完整 ($COUNT 条)"
    else
        print_fail "技术指标数据不足: $COUNT 条"
    fi
else
    print_fail "技术指标数据获取失败"
fi

# 测试 5: 基本面数据
print_test "feat-005 - 基本面数据展示"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

if curl -s "http://localhost:8080/api/stock/$FIRST_STOCK/financials" 2>/dev/null | jq -e '.revenue' > /dev/null; then
    print_pass "基本面数据可获取"
else
    print_fail "基本面数据获取失败"
fi

# 测试 6: 数据刷新
print_test "feat-006 - 数据刷新功能"

TOTAL_TESTS=$((TOTAL_TESTS + 1))

REFRESH_RESULT=$(curl -s "http://localhost:8080/api/refresh/$FIRST_STOCK" 2>/dev/null)

if echo "$REFRESH_RESULT" | jq -e '.message' > /dev/null; then
    print_pass "刷新接口响应正常"
    
    if echo "$REFRESH_RESULT" | jq -e '.symbol' | grep -q "$FIRST_STOCK"; then
        print_pass "刷新正确股票数据"
    else
        print_fail "刷新股票不匹配"
    fi
else
    print_fail "刷新接口错误"
fi

# 测试总结
echo ""
echo -e "\n${BLUE}═══════════════════════════════════════════════${NC}"
echo -e "${BLUE}   端到端测试总结${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════${NC}"
echo ""
echo "总测试数: $TOTAL_TESTS"
echo -e "通过: ${GREEN}$PASSED_TESTS${NC}"
echo -e "失败: ${RED}$FAILED_TESTS${NC}"
echo ""

if [ $FAILED_TESTS -eq 0 ]; then
    echo -e "${GREEN}✨ 所有测试通过！${NC}"
    echo ""
    echo "💡 提示: 测试截图已保存到 tests/screenshots/"
    exit 0
else
    echo -e "${RED}⚠️  发现 $FAILED_TESTS 个失败项${NC}"
    echo ""
    echo "🔧 修复建议:"
    echo "   1. 检查服务器日志: cat logs/server.log"
    echo "   2. 验证 API 接口是否正常"
    echo "   3. 检查 feature_list.json 配置"
    echo ""
    exit 1
fi
