#!/bin/bash

# 健康检查功能测试脚本

set -e

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

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

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

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

# 测试脚本语法
test_script_syntax() {
    log_info "测试脚本语法..."
    
    local scripts=(
        "docker/check-services.sh"
        "docker/monitor-services.sh"
        "docker/start-with-monitoring.sh"
        "docker/stop-monitoring.sh"
        "docker/queue-health-check.sh"
    )
    
    local errors=0
    
    for script in "${scripts[@]}"; do
        if [ -f "$script" ]; then
            if bash -n "$script"; then
                log_success "$script 语法正确"
            else
                log_error "$script 语法错误"
                ((errors++))
            fi
        else
            log_error "$script 文件不存在"
            ((errors++))
        fi
    done
    
    if [ $errors -eq 0 ]; then
        log_success "所有脚本语法检查通过"
        return 0
    else
        log_error "发现 $errors 个语法错误"
        return 1
    fi
}

# 测试脚本权限
test_script_permissions() {
    log_info "测试脚本执行权限..."
    
    local scripts=(
        "docker/check-services.sh"
        "docker/monitor-services.sh"
        "docker/start-with-monitoring.sh"
        "docker/stop-monitoring.sh"
        "docker/queue-health-check.sh"
    )
    
    local errors=0
    
    for script in "${scripts[@]}"; do
        if [ -f "$script" ]; then
            if [ -x "$script" ]; then
                log_success "$script 有执行权限"
            else
                log_error "$script 没有执行权限"
                ((errors++))
            fi
        else
            log_error "$script 文件不存在"
            ((errors++))
        fi
    done
    
    if [ $errors -eq 0 ]; then
        log_success "所有脚本权限检查通过"
        return 0
    else
        log_error "发现 $errors 个权限问题"
        return 1
    fi
}

# 测试Docker配置文件
test_docker_configs() {
    log_info "测试Docker配置文件..."
    
    local configs=(
        "docker-compose.yml"
        "Dockerfile"
        "docker/php/php.ini"
        "docker/supervisor/supervisord.conf"
        "docker/redis/redis.conf"
        "docker/mysql/my.cnf"
        "docker/supervisor/supervisord.conf"
    )
    
    local errors=0
    
    for config in "${configs[@]}"; do
        if [ -f "$config" ]; then
            log_success "$config 存在"
        else
            log_error "$config 不存在"
            ((errors++))
        fi
    done
    
    # 测试docker-compose语法
    if command -v docker-compose >/dev/null 2>&1; then
        if docker-compose config >/dev/null 2>&1; then
            log_success "docker-compose.yml 语法正确"
        else
            log_error "docker-compose.yml 语法错误"
            ((errors++))
        fi
    else
        log_warning "docker-compose 未安装，跳过语法检查"
    fi
    
    if [ $errors -eq 0 ]; then
        log_success "所有配置文件检查通过"
        return 0
    else
        log_error "发现 $errors 个配置问题"
        return 1
    fi
}

# 测试健康检查端点配置
test_healthcheck_configs() {
    log_info "测试健康检查配置..."
    
    local errors=0
    
    # 检查docker-compose中的健康检查配置
    if grep -q "healthcheck:" docker-compose.yml; then
        log_success "docker-compose.yml 包含健康检查配置"
    else
        log_error "docker-compose.yml 缺少健康检查配置"
        ((errors++))
    fi
    
    # 检查Laravel健康检查路由
    if grep -q "/health" routes/web.php; then
        log_success "Laravel 健康检查路由已配置"
    else
        log_error "Laravel 健康检查路由未配置"
        ((errors++))
    fi
    
    # 检查队列健康检查脚本
    if [ -f "docker/queue-health-check.sh" ] && [ -x "docker/queue-health-check.sh" ]; then
        log_success "队列健康检查脚本存在且可执行"
    else
        log_error "队列健康检查脚本问题"
        ((errors++))
    fi
    
    if [ $errors -eq 0 ]; then
        log_success "健康检查配置检查通过"
        return 0
    else
        log_error "发现 $errors 个健康检查配置问题"
        return 1
    fi
}

# 测试存储目录结构
test_storage_structure() {
    log_info "测试存储目录结构..."
    
    local required_dirs=(
        "./storage"
        "./storage/logs"
    )
    
    local errors=0
    
    for dir in "${required_dirs[@]}"; do
        if [ -d "$dir" ]; then
            log_success "目录存在: $dir"
        else
            log_warning "目录不存在，将创建: $dir"
            mkdir -p "$dir"
            if [ -d "$dir" ]; then
                log_success "成功创建目录: $dir"
            else
                log_error "无法创建目录: $dir"
                ((errors++))
            fi
        fi
    done
    
    if [ $errors -eq 0 ]; then
        log_success "存储目录结构检查通过"
        return 0
    else
        log_error "发现 $errors 个存储目录问题"
        return 1
    fi
}

# 主测试函数
main() {
    echo "========================================"
    echo "健康检查功能测试"
    echo "时间: $(date)"
    echo "========================================"
    
    local total_tests=0
    local failed_tests=0
    
    # 执行所有测试
    tests=(
        "test_script_syntax:脚本语法测试"
        "test_script_permissions:脚本权限测试"
        "test_docker_configs:Docker配置测试"
        "test_healthcheck_configs:健康检查配置测试"
        "test_storage_structure:存储目录测试"
    )
    
    for test in "${tests[@]}"; do
        IFS=':' read -ra TEST_PARTS <<< "$test"
        local test_func="${TEST_PARTS[0]}"
        local test_name="${TEST_PARTS[1]}"
        
        ((total_tests++))
        
        echo ""
        log_info "开始 $test_name..."
        
        if $test_func; then
            log_success "$test_name 通过"
        else
            log_error "$test_name 失败"
            ((failed_tests++))
        fi
    done
    
    # 输出总结
    echo ""
    echo "========================================"
    echo "测试完成"
    echo "总测试数: $total_tests"
    echo "失败: $failed_tests"
    echo "成功: $((total_tests - failed_tests))"
    echo "========================================"
    
    if [ $failed_tests -gt 0 ]; then
        log_error "发现 $failed_tests 个问题，请检查配置"
        exit 1
    else
        log_success "所有测试通过，健康检查功能配置正确"
        exit 0
    fi
}

# 如果脚本被直接执行
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi