#!/bin/bash

BASE_URL="http://localhost:8080"
TEST_RESULTS=()
PASS_COUNT=0
FAIL_COUNT=0

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

log_test() {
    local test_name=$1
    local result=$2
    local message=$3
    
    if [ "$result" == "PASS" ]; then
        echo -e "${GREEN}[PASS]${NC} $test_name"
        PASS_COUNT=$((PASS_COUNT + 1))
    else
        echo -e "${RED}[FAIL]${NC} $test_name - $message"
        FAIL_COUNT=$((FAIL_COUNT + 1))
    fi
}

generate_unique_name() {
    echo "test_$(date +%s)_$RANDOM"
}

echo "========================================="
echo "开始全面业务流程测试"
echo "========================================="
echo ""

echo "========== 1. 用户认证流程测试 =========="
echo ""

echo "1.1 用户登录测试"
LOGIN_RESPONSE=$(curl -s -X POST "$BASE_URL/api/auth/login" \
    -H "Content-Type: application/json" \
    -d '{"username":"admin","password":"Test@123"}')

if echo "$LOGIN_RESPONSE" | grep -q "token"; then
    TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"token":"[^"]*"' | cut -d'"' -f4)
    log_test "用户登录" "PASS"
else
    log_test "用户登录" "FAIL" "无法获取token"
    exit 1
fi

echo ""
echo "1.2 Token验证测试"
USER_INFO=$(curl -s -X GET "$BASE_URL/api/users/1" \
    -H "Authorization: Bearer $TOKEN")

if echo "$USER_INFO" | grep -q "admin"; then
    log_test "Token验证" "PASS"
else
    log_test "Token验证" "FAIL" "Token无效"
fi

echo ""
echo "========== 2. 用户管理流程测试 =========="
echo ""

echo "2.1 获取用户列表测试"
USERS_LIST=$(curl -s -X GET "$BASE_URL/api/users" \
    -H "Authorization: Bearer $TOKEN")

if echo "$USERS_LIST" | grep -q "admin"; then
    log_test "获取用户列表" "PASS"
else
    log_test "获取用户列表" "FAIL" "无法获取用户列表"
fi

echo ""
echo "2.2 创建用户测试"
UNIQUE_USERNAME=$(generate_unique_name)
CREATE_USER_RESPONSE=$(curl -s -X POST "$BASE_URL/api/users" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"username\": \"$UNIQUE_USERNAME\",
        \"password\": \"Test@123\",
        \"email\": \"$UNIQUE_USERNAME@example.com\",
        \"phone\": \"13900139000\",
        \"nickname\": \"测试用户\",
        \"status\": 1
    }")

if echo "$CREATE_USER_RESPONSE" | grep -q "id"; then
    NEW_USER_ID=$(echo "$CREATE_USER_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建用户" "PASS"
else
    log_test "创建用户" "FAIL" "无法创建用户: $CREATE_USER_RESPONSE"
fi

echo ""
echo "2.3 更新用户测试"
if [ -n "$NEW_USER_ID" ]; then
    UPDATE_USER_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/users/$NEW_USER_ID" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "nickname": "更新后的用户",
            "phone": "13900139001"
        }')
    
    if echo "$UPDATE_USER_RESPONSE" | grep -q "更新后的用户"; then
        log_test "更新用户" "PASS"
    else
        log_test "更新用户" "FAIL" "无法更新用户"
    fi
fi

echo ""
echo "2.4 删除用户测试"
if [ -n "$NEW_USER_ID" ]; then
    DELETE_RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/users/$NEW_USER_ID" \
        -H "Authorization: Bearer $TOKEN")
    
    if [ -z "$DELETE_RESPONSE" ] || echo "$DELETE_RESPONSE" | grep -q "success"; then
        log_test "删除用户" "PASS"
    else
        log_test "删除用户" "FAIL" "无法删除用户"
    fi
fi

echo ""
echo "========== 3. 角色管理流程测试 =========="
echo ""

echo "3.1 获取角色列表测试"
ROLES_LIST=$(curl -s -X GET "$BASE_URL/api/roles" \
    -H "Authorization: Bearer $TOKEN")

if echo "$ROLES_LIST" | grep -q "admin"; then
    log_test "获取角色列表" "PASS"
else
    log_test "获取角色列表" "FAIL" "无法获取角色列表"
fi

echo ""
echo "3.2 创建角色测试"
UNIQUE_ROLE_KEY=$(generate_unique_name)
CREATE_ROLE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/roles" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"roleName\": \"测试角色_$UNIQUE_ROLE_KEY\",
        \"roleKey\": \"$UNIQUE_ROLE_KEY\",
        \"roleSort\": 99,
        \"status\": 1
    }")

if echo "$CREATE_ROLE_RESPONSE" | grep -q "id"; then
    NEW_ROLE_ID=$(echo "$CREATE_ROLE_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建角色" "PASS"
else
    log_test "创建角色" "FAIL" "无法创建角色: $CREATE_ROLE_RESPONSE"
fi

echo ""
echo "3.3 更新角色测试"
if [ -n "$NEW_ROLE_ID" ]; then
    UPDATE_ROLE_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/roles/$NEW_ROLE_ID" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "roleName": "更新后的角色"
        }')
    
    if echo "$UPDATE_ROLE_RESPONSE" | grep -q "更新后的角色"; then
        log_test "更新角色" "PASS"
    else
        log_test "更新角色" "FAIL" "无法更新角色"
    fi
fi

echo ""
echo "3.4 删除角色测试"
if [ -n "$NEW_ROLE_ID" ]; then
    DELETE_ROLE_RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/roles/$NEW_ROLE_ID" \
        -H "Authorization: Bearer $TOKEN")
    
    if [ -z "$DELETE_ROLE_RESPONSE" ] || echo "$DELETE_ROLE_RESPONSE" | grep -q "success"; then
        log_test "删除角色" "PASS"
    else
        log_test "删除角色" "FAIL" "无法删除角色"
    fi
fi

echo ""
echo "========== 4. 菜单管理流程测试 =========="
echo ""

echo "4.1 获取菜单列表测试"
MENUS_LIST=$(curl -s -X GET "$BASE_URL/api/menus" \
    -H "Authorization: Bearer $TOKEN")

if echo "$MENUS_LIST" | grep -q "系统管理"; then
    log_test "获取菜单列表" "PASS"
else
    log_test "获取菜单列表" "FAIL" "无法获取菜单列表"
fi

echo ""
echo "4.2 创建菜单测试"
UNIQUE_MENU_NAME=$(generate_unique_name)
CREATE_MENU_RESPONSE=$(curl -s -X POST "$BASE_URL/api/menus" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"menuName\": \"测试菜单_$UNIQUE_MENU_NAME\",
        \"parentId\": 0,
        \"orderNum\": 99,
        \"menuType\": \"M\",
        \"status\": \"1\"
    }")

if echo "$CREATE_MENU_RESPONSE" | grep -q "id"; then
    NEW_MENU_ID=$(echo "$CREATE_MENU_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建菜单" "PASS"
else
    log_test "创建菜单" "FAIL" "无法创建菜单: $CREATE_MENU_RESPONSE"
fi

echo ""
echo "4.3 更新菜单测试"
if [ -n "$NEW_MENU_ID" ]; then
    UPDATE_MENU_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/menus/$NEW_MENU_ID" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "menuName": "更新后的菜单"
        }')
    
    if echo "$UPDATE_MENU_RESPONSE" | grep -q "更新后的菜单"; then
        log_test "更新菜单" "PASS"
    else
        log_test "更新菜单" "FAIL" "无法更新菜单"
    fi
fi

echo ""
echo "4.4 删除菜单测试"
if [ -n "$NEW_MENU_ID" ]; then
    DELETE_MENU_RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/menus/$NEW_MENU_ID" \
        -H "Authorization: Bearer $TOKEN")
    
    if [ -z "$DELETE_MENU_RESPONSE" ] || echo "$DELETE_MENU_RESPONSE" | grep -q "success"; then
        log_test "删除菜单" "PASS"
    else
        log_test "删除菜单" "FAIL" "无法删除菜单"
    fi
fi

echo ""
echo "========== 5. 权限管理流程测试 =========="
echo ""

echo "5.1 获取权限列表测试"
PERMISSIONS_LIST=$(curl -s -X GET "$BASE_URL/api/permissions" \
    -H "Authorization: Bearer $TOKEN")

if echo "$PERMISSIONS_LIST" | grep -q "system:manage"; then
    log_test "获取权限列表" "PASS"
else
    log_test "获取权限列表" "FAIL" "无法获取权限列表: $PERMISSIONS_LIST"
fi

echo ""
echo "5.2 创建权限测试"
UNIQUE_PERM_KEY=$(generate_unique_name)
CREATE_PERMISSION_RESPONSE=$(curl -s -X POST "$BASE_URL/api/permissions" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"permissionName\": \"测试权限_$UNIQUE_PERM_KEY\",
        \"permissionCode\": \"$UNIQUE_PERM_KEY\",
        \"permissionType\": \"button\",
        \"parentId\": 0,
        \"status\": 1
    }")

if echo "$CREATE_PERMISSION_RESPONSE" | grep -q "id"; then
    NEW_PERMISSION_ID=$(echo "$CREATE_PERMISSION_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建权限" "PASS"
else
    log_test "创建权限" "FAIL" "无法创建权限: $CREATE_PERMISSION_RESPONSE"
fi

echo ""
echo "5.3 更新权限测试"
if [ -n "$NEW_PERMISSION_ID" ]; then
    UPDATE_PERMISSION_RESPONSE=$(curl -s -X PUT "$BASE_URL/api/permissions/$NEW_PERMISSION_ID" \
        -H "Authorization: Bearer $TOKEN" \
        -H "Content-Type: application/json" \
        -d '{
            "permissionName": "更新后的权限"
        }')
    
    if echo "$UPDATE_PERMISSION_RESPONSE" | grep -q "更新后的权限"; then
        log_test "更新权限" "PASS"
    else
        log_test "更新权限" "FAIL" "无法更新权限"
    fi
fi

echo ""
echo "5.4 删除权限测试"
if [ -n "$NEW_PERMISSION_ID" ]; then
    DELETE_PERMISSION_RESPONSE=$(curl -s -X DELETE "$BASE_URL/api/permissions/$NEW_PERMISSION_ID" \
        -H "Authorization: Bearer $TOKEN")
    
    if [ -z "$DELETE_PERMISSION_RESPONSE" ] || echo "$DELETE_PERMISSION_RESPONSE" | grep -q "success"; then
        log_test "删除权限" "PASS"
    else
        log_test "删除权限" "FAIL" "无法删除权限"
    fi
fi

echo ""
echo "========== 6. 字典管理流程测试 =========="
echo ""

echo "6.1 获取字典类型列表测试"
DICT_TYPES_LIST=$(curl -s -X GET "$BASE_URL/api/dict/types" \
    -H "Authorization: Bearer $TOKEN")

if echo "$DICT_TYPES_LIST" | grep -q "user_status"; then
    log_test "获取字典类型列表" "PASS"
else
    log_test "获取字典类型列表" "FAIL" "无法获取字典类型列表"
fi

echo ""
echo "6.2 创建字典类型测试"
UNIQUE_DICT_TYPE=$(generate_unique_name)
CREATE_DICT_TYPE_RESPONSE=$(curl -s -X POST "$BASE_URL/api/dict/types" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"dictName\": \"测试字典_$UNIQUE_DICT_TYPE\",
        \"dictType\": \"$UNIQUE_DICT_TYPE\",
        \"status\": \"0\"
    }")

if echo "$CREATE_DICT_TYPE_RESPONSE" | grep -q "id"; then
    NEW_DICT_TYPE_ID=$(echo "$CREATE_DICT_TYPE_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建字典类型" "PASS"
else
    log_test "创建字典类型" "FAIL" "无法创建字典类型: $CREATE_DICT_TYPE_RESPONSE"
fi

echo ""
echo "6.3 获取字典数据列表测试"
DICT_DATA_LIST=$(curl -s -X GET "$BASE_URL/api/dict/data" \
    -H "Authorization: Bearer $TOKEN")

if echo "$DICT_DATA_LIST" | grep -q "正常"; then
    log_test "获取字典数据列表" "PASS"
else
    log_test "获取字典数据列表" "FAIL" "无法获取字典数据列表"
fi

echo ""
echo "========== 7. 系统配置管理流程测试 =========="
echo ""

echo "7.1 获取系统配置列表测试"
CONFIG_LIST=$(curl -s -X GET "$BASE_URL/api/config" \
    -H "Authorization: Bearer $TOKEN")

if echo "$CONFIG_LIST" | grep -q "sys.user.initPassword"; then
    log_test "获取系统配置列表" "PASS"
else
    log_test "获取系统配置列表" "FAIL" "无法获取系统配置列表"
fi

echo ""
echo "7.2 创建系统配置测试"
UNIQUE_CONFIG_KEY=$(generate_unique_name)
CREATE_CONFIG_RESPONSE=$(curl -s -X POST "$BASE_URL/api/config" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d "{
        \"configName\": \"测试配置_$UNIQUE_CONFIG_KEY\",
        \"configKey\": \"$UNIQUE_CONFIG_KEY\",
        \"configValue\": \"test_value\",
        \"configType\": \"Y\"
    }")

if echo "$CREATE_CONFIG_RESPONSE" | grep -q "id"; then
    NEW_CONFIG_ID=$(echo "$CREATE_CONFIG_RESPONSE" | grep -o '"id":[0-9]*' | cut -d':' -f2)
    log_test "创建系统配置" "PASS"
else
    log_test "创建系统配置" "FAIL" "无法创建系统配置: $CREATE_CONFIG_RESPONSE"
fi

echo ""
echo "========== 8. 日志管理流程测试 =========="
echo ""

echo "8.1 获取登录日志列表测试"
LOGIN_LOG_LIST=$(curl -s -X GET "$BASE_URL/api/logs/login" \
    -H "Authorization: Bearer $TOKEN")

if [ -n "$LOGIN_LOG_LIST" ]; then
    log_test "获取登录日志列表" "PASS"
else
    log_test "获取登录日志列表" "FAIL" "无法获取登录日志列表"
fi

echo ""
echo "8.2 获取操作日志列表测试"
OPERATION_LOG_LIST=$(curl -s -X GET "$BASE_URL/api/logs/operation" \
    -H "Authorization: Bearer $TOKEN")

if [ -n "$OPERATION_LOG_LIST" ]; then
    log_test "获取操作日志列表" "PASS"
else
    log_test "获取操作日志列表" "FAIL" "无法获取操作日志列表"
fi

echo ""
echo "========== 9. 统计数据测试 =========="
echo ""

echo "9.1 获取系统概览统计测试"
STATS_OVERVIEW=$(curl -s -X GET "$BASE_URL/api/stats/overview" \
    -H "Authorization: Bearer $TOKEN")

if echo "$STATS_OVERVIEW" | grep -q "userCount\|roleCount\|menuCount"; then
    log_test "获取系统概览统计" "PASS"
else
    log_test "获取系统概览统计" "FAIL" "无法获取系统概览统计"
fi

echo ""
echo "========================================="
echo "测试执行完成"
echo "========================================="
echo ""
echo -e "${GREEN}通过测试: $PASS_COUNT${NC}"
echo -e "${RED}失败测试: $FAIL_COUNT${NC}"
echo -e "总计测试: $((PASS_COUNT + FAIL_COUNT))"
echo ""

if [ $FAIL_COUNT -eq 0 ]; then
    echo -e "${GREEN}所有测试通过！${NC}"
    exit 0
else
    echo -e "${RED}存在失败的测试！${NC}"
    exit 1
fi
