@echo off
REM 后端环境变量配置脚本 (Windows版本)
REM 用于设置不同环境下的后端应用配置

setlocal enabledelayedexpansion

REM 默认环境变量配置
if not defined DB_HOST set DB_HOST=anxin-mysql
if not defined DB_PORT set DB_PORT=3306
if not defined DB_NAME set DB_NAME=anxin
if not defined DB_USER set DB_USER=anxin_user
if not defined DB_PASSWORD set DB_PASSWORD=anxin_password

REM 应用端口和网络配置
if not defined SERVER_PORT set SERVER_PORT=8080
if not defined SPRING_PROFILES_ACTIVE set SPRING_PROFILES_ACTIVE=docker

REM 日志配置
if not defined LOG_LEVEL set LOG_LEVEL=INFO
if not defined LOG_PATH set LOG_PATH=/app/logs
if not defined LOG_MAX_SIZE set LOG_MAX_SIZE=100MB
if not defined LOG_MAX_FILES set LOG_MAX_FILES=10

REM 监控配置
if not defined MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE set MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=health,info,metrics

REM JVM配置
if not defined JAVA_OPTS set JAVA_OPTS=-Xms512m -Xmx1024m -Djava.security.egd=file:/dev/./urandom

REM 安全配置
if not defined JWT_SECRET set JWT_SECRET=abcdefghijklmnopqrstuvwxyz
if not defined JWT_EXPIRE_TIME set JWT_EXPIRE_TIME=30

REM Druid监控配置
if not defined DRUID_USERNAME set DRUID_USERNAME=admin
if not defined DRUID_PASSWORD set DRUID_PASSWORD=admin123

REM 文件上传路径
if not defined UPLOAD_PATH set UPLOAD_PATH=/app/uploadPath

REM Swagger配置
if not defined SWAGGER_ENABLED set SWAGGER_ENABLED=false

REM 时区配置
if not defined TZ set TZ=Asia/Shanghai

goto :main

:validate_required_env
echo 验证必需的环境变量...
set missing_vars=
if not defined DB_HOST set missing_vars=!missing_vars! DB_HOST
if not defined DB_PORT set missing_vars=!missing_vars! DB_PORT
if not defined DB_NAME set missing_vars=!missing_vars! DB_NAME
if not defined DB_USER set missing_vars=!missing_vars! DB_USER
if not defined DB_PASSWORD set missing_vars=!missing_vars! DB_PASSWORD

if not "!missing_vars!"=="" (
    echo 错误: 以下必需的环境变量未设置:
    echo !missing_vars!
    exit /b 1
)

echo 所有必需的环境变量已设置
exit /b 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 ==========================
exit /b 0

:load_environment
set env_type=%1
if "%env_type%"=="" set env_type=development

if "%env_type%"=="production" (
    set LOG_LEVEL=WARN
    set SWAGGER_ENABLED=false
    set JAVA_OPTS=-Xms1024m -Xmx2048m -Djava.security.egd=file:/dev/./urandom -XX:+UseG1GC
    echo 已加载生产环境配置
) else if "%env_type%"=="staging" (
    set LOG_LEVEL=INFO
    set SWAGGER_ENABLED=true
    set JAVA_OPTS=-Xms512m -Xmx1024m -Djava.security.egd=file:/dev/./urandom
    echo 已加载预发布环境配置
) else if "%env_type%"=="development" (
    set LOG_LEVEL=DEBUG
    set SWAGGER_ENABLED=true
    set JAVA_OPTS=-Xms256m -Xmx512m -Djava.security.egd=file:/dev/./urandom
    echo 已加载开发环境配置
) else (
    echo 警告: 未知的环境类型 '%env_type%'，使用默认配置
)
exit /b 0

:main
set command=%1
if "%command%"=="" set command=show

if "%command%"=="validate" (
    call :validate_required_env
) else if "%command%"=="show" (
    call :show_config
) else if "%command%"=="load" (
    call :load_environment %2
    call :show_config
) else (
    echo 用法: %0 {validate^|show^|load [environment]}
    echo   validate  - 验证必需的环境变量
    echo   show      - 显示当前环境配置
    echo   load      - 加载指定环境配置 ^(development^|staging^|production^)
    exit /b 1
)

endlocal