#!/bin/bash

# Monisuo API 测试脚本模板
# 用于验证 API 接口功能

set -e  # 遇到错误立即退出

# ==================== 配置 ====================
BASE_URL="${API_BASE_URL:-http://localhost:5010}"
TOKEN=""
TEST_USER_PHONE="13800138000"
TEST_USER_PASSWORD="123456"

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

# ==================== 工具函数 ====================

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

check_command() {
    if ! command -v $1 &> /dev/null; then
        log_error "需要安装 $1 命令"
        exit 1
    fi
}

# ==================== 前置检查 ====================

log_info "=== 前置检查 ==="

# 检查必要的命令
check_command curl
check_command jq

# 检查 API 服务是否运行
log_info "检查 API 服务状态..."
if ! curl -s -f "$BASE_URL/actuator/health" > /dev/null 2>&1; then
    log_error "API 服务未运行或无法访问: $BASE_URL"
    log_warning "请先启动后端服务"
    exit 1
fi

log_info "✓ API 服务正常"

# ==================== 测试用例 ====================

log_info "=== 开始 API 测试 ==="

# 测试计数器
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

run_test() {
    local test_name="$1"
    local test_command="$2"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    log_info "测试 $TOTAL_TESTS: $test_name"
    
    if eval "$test_command"; then
        PASSED_TESTS=$((PASSED_TESTS + 1))
        log_info "✓ 通过"
        return 0
    else
        FAILED_TESTS=$((FAILED_TESTS + 1))
        log_error "✗ 失败"
        return 1
    fi
}

# ==================== 测试 1: 健康检查 ====================

run_test "健康检查" "curl -s -f '$BASE_URL/actuator/health' | jq -e '.status == \"UP\"' > /dev/null"

# ==================== 测试 2: 用户登录 ====================

log_info "测试用户登录..."
RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
  -H "Content-Type: application/json" \
  -d "{\"phone\":\"$TEST_USER_PHONE\",\"password\":\"$TEST_USER_PASSWORD\"}")

if echo "$RESPONSE" | jq -e '.success == true' > /dev/null; then
    TOKEN=$(echo "$RESPONSE" | jq -r '.data.token')
    log_info "✓ 登录成功，Token: ${TOKEN:0:20}..."
    PASSED_TESTS=$((PASSED_TESTS + 1))
else
    log_error "✗ 登录失败"
    log_error "响应: $RESPONSE"
    FAILED_TESTS=$((FAILED_TESTS + 1))
fi
TOTAL_TESTS=$((TOTAL_TESTS + 1))

# ==================== 测试 3: 获取用户信息 ====================

if [ -n "$TOKEN" ]; then
    log_info "测试获取用户信息..."
    run_test "获取用户信息" "curl -s -f -X GET '$BASE_URL/api/user/info' -H 'Authorization: Bearer $TOKEN' | jq -e '.success == true' > /dev/null"
fi

# ==================== 测试 4: [根据具体功能添加更多测试] ====================

# TODO: 添加更多测试用例
# 示例：
# run_test "创建订单" "curl -s -f -X POST '$BASE_URL/api/orders' ..."
# run_test "查询订单列表" "curl -s -f -X GET '$BASE_URL/api/orders' ..."

# ==================== 测试结果汇总 ====================

echo ""
log_info "=== 测试结果汇总 ==="
log_info "总测试数: $TOTAL_TESTS"
log_info "通过: $PASSED_TESTS"
log_info "失败: $FAILED_TESTS"

if [ $FAILED_TESTS -eq 0 ]; then
    log_info "✓ 所有测试通过！"
    exit 0
else
    log_error "✗ 有 $FAILED_TESTS 个测试失败"
    exit 1
fi
