#!/bin/bash

# 后端环境变量配置脚本
# 用于设置不同环境下的后端应用配置

set -e

# 默认环境变量配置
export DB_HOST=${DB_HOST:-"anxin-mysql"}
export DB_PORT=${DB_PORT:-"3306"}
export DB_NAME=${DB_NAME:-"anxin_prod"}
export DB_USER=${DB_USER:-"anxin_prod"}
export DB_PASSWORD=${DB_PASSWORD:-"prod_password"}

# 应用端口和网络配置
export SERVER_PORT=${SERVER_PORT:-"8080"}
export SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE:-"docker"}

# 日志配置
export LOG_LEVEL=${LOG_LEVEL:-"INFO"}
export LOG_PATH=${LOG_PATH:-"/app/logs"}
export LOG_MAX_SIZE=${LOG_MAX_SIZE:-"100MB"}
export LOG_MAX_FILES=${LOG_MAX_FILES:-"10"}

# 监控配置
export MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=${MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE:-"health,info,metrics"}

# JVM配置
export JAVA_OPTS=${JAVA_OPTS:-"-Xms512m -Xmx1024m -Djava.security.egd=file:/dev/./urandom"}

# 安全配置
export JWT_SECRET=${JWT_SECRET:-"abcdefghijklmnopqrstuvwxyz"}
export JWT_EXPIRE_TIME=${JWT_EXPIRE_TIME:-"30"}

# Druid监控配置
export DRUID_USERNAME=${DRUID_USERNAME:-"admin"}
export DRUID_PASSWORD=${DRUID_PASSWORD:-"admin123"}

# 文件上传路径
export UPLOAD_PATH=${UPLOAD_PATH:-"/app/uploadPath"}

# Swagger配置
export SWAGGER_ENABLED=${SWAGGER_ENABLED:-"false"}

# 时区配置
export TZ=${TZ:-"Asia/Shanghai"}

# 函数：验证必需的环境变量
validate_required_env() {
    local required_vars=("DB_HOST" "DB_PORT" "DB_NAME" "DB_USER" "DB_PASSWORD")
    local missing_vars=()
    
    for var in "${required_vars[@]}"; do
        if [[ -z "${!var}" ]]; then
            missing_vars+=("$var")
        fi
    done
    
    if [[ ${#missing_vars[@]} -gt 0 ]]; then
        echo "错误: 以下必需的环境变量未设置:"
        printf '%s\n' "${missing_vars[@]}"
        return 1
    fi
    
    echo "所有必需的环境变量已设置"
    return 0
}

# 函数：显示当前环境配置
show_config() {
    echo "=== 后端应用环境配置 ==="
    echo "数据库配置:"
    echo "  DB_HOST: $DB_HOST"
    echo "  DB_PORT: $DB_PORT"
    echo "  DB_NAME: $DB_NAME"
    echo "  DB_USER: $DB_USER"
    echo "  DB_PASSWORD: [HIDDEN]"
    echo ""
    echo "应用配置:"
    echo "  SERVER_PORT: $SERVER_PORT"
    echo "  SPRING_PROFILES_ACTIVE: $SPRING_PROFILES_ACTIVE"
    echo ""
    echo "日志配置:"
    echo "  LOG_LEVEL: $LOG_LEVEL"
    echo "  LOG_PATH: $LOG_PATH"
    echo "  LOG_MAX_SIZE: $LOG_MAX_SIZE"
    echo "  LOG_MAX_FILES: $LOG_MAX_FILES"
    echo ""
    echo "JVM配置:"
    echo "  JAVA_OPTS: $JAVA_OPTS"
    echo ""
    echo "其他配置:"
    echo "  TZ: $TZ"
    echo "  UPLOAD_PATH: $UPLOAD_PATH"
    echo "  SWAGGER_ENABLED: $SWAGGER_ENABLED"
    echo "=========================="
}

# 函数：根据环境类型加载配置
load_environment() {
    local env_type=${1:-"development"}
    
    case $env_type in
        "production")
            export LOG_LEVEL="WARN"
            export SWAGGER_ENABLED="false"
            export JAVA_OPTS="-Xms1024m -Xmx2048m -Djava.security.egd=file:/dev/./urandom -XX:+UseG1GC"
            echo "已加载生产环境配置"
            ;;
        "staging")
            export LOG_LEVEL="INFO"
            export SWAGGER_ENABLED="true"
            export JAVA_OPTS="-Xms512m -Xmx1024m -Djava.security.egd=file:/dev/./urandom"
            echo "已加载预发布环境配置"
            ;;
        "development"|"dev")
            export LOG_LEVEL="DEBUG"
            export SWAGGER_ENABLED="true"
            export JAVA_OPTS="-Xms256m -Xmx512m -Djava.security.egd=file:/dev/./urandom"
            echo "已加载开发环境配置"
            ;;
        *)
            echo "警告: 未知的环境类型 '$env_type'，使用默认配置"
            ;;
    esac
}

# 自动加载当前环境配置
if [[ -n "$SPRING_PROFILES_ACTIVE" ]]; then
    load_environment "$SPRING_PROFILES_ACTIVE"
fi

# 主函数
main() {
    local command=${1:-"show"}
    
    case $command in
        "validate")
            validate_required_env
            ;;
        "show")
            show_config
            ;;
        "load")
            local env_type=${2:-"development"}
            load_environment "$env_type"
            show_config
            ;;
        *)
            echo "用法: $0 {validate|show|load [environment]}"
            echo "  validate  - 验证必需的环境变量"
            echo "  show      - 显示当前环境配置"
            echo "  load      - 加载指定环境配置 (development|staging|production)"
            exit 1
            ;;
    esac
}

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