@echo off
REM 环境管理脚本 - Windows版本
REM 用于切换不同环境的Docker Compose配置
REM Requirements: 5.3, 6.5

setlocal enabledelayedexpansion

REM 设置脚本目录
set "SCRIPT_DIR=%~dp0"
set "DOCKER_DIR=%SCRIPT_DIR%.."
set "PROJECT_ROOT=%DOCKER_DIR%\.."

REM 颜色定义 (Windows不支持颜色，使用文本标识)
set "INFO=[INFO]"
set "WARN=[WARN]"
set "ERROR=[ERROR]"
set "DEBUG=[DEBUG]"

REM 显示帮助信息
:show_help
echo 环境管理脚本 - 若依框架Docker部署 (Windows版本)
echo.
echo 用法: %~nx0 [命令] [环境]
echo.
echo 命令:
echo     deploy      部署指定环境
echo     stop        停止指定环境
echo     restart     重启指定环境
echo     status      查看指定环境状态
echo     logs        查看指定环境日志
echo     clean       清理指定环境
echo     backup      备份指定环境数据
echo     list        列出所有可用环境
echo     help        显示此帮助信息
echo.
echo 环境:
echo     development (dev)    开发环境
echo     staging     (stage)  测试环境
echo     production  (prod)   生产环境
echo.
echo 示例:
echo     %~nx0 deploy development    # 部署开发环境
echo     %~nx0 stop prod            # 停止生产环境
echo     %~nx0 logs staging         # 查看测试环境日志
echo     %~nx0 status dev           # 查看开发环境状态
echo.
goto :eof

REM 验证环境参数
:validate_environment
set "input_env=%1"
if "%input_env%"=="development" set "validated_env=development" & goto :eof
if "%input_env%"=="dev" set "validated_env=development" & goto :eof
if "%input_env%"=="staging" set "validated_env=staging" & goto :eof
if "%input_env%"=="stage" set "validated_env=staging" & goto :eof
if "%input_env%"=="production" set "validated_env=production" & goto :eof
if "%input_env%"=="prod" set "validated_env=production" & goto :eof

echo %ERROR% 无效的环境: %input_env%
echo %INFO% 支持的环境: development, staging, production
exit /b 1

REM 获取compose文件路径
:get_compose_file
set "compose_file=%DOCKER_DIR%\docker-compose.%1.yml"
goto :eof

REM 获取环境配置文件路径
:get_env_file
set "env_file=%DOCKER_DIR%\environments\.env.%1"
goto :eof

REM 检查文件是否存在
:check_files
call :get_compose_file %1
call :get_env_file %1

if not exist "%compose_file%" (
    echo %ERROR% Compose文件不存在: %compose_file%
    exit /b 1
)

if not exist "%env_file%" (
    echo %ERROR% 环境配置文件不存在: %env_file%
    exit /b 1
)

echo %DEBUG% 文件检查通过: %compose_file%, %env_file%
goto :eof

REM 创建数据目录
:create_directories
set "env=%1"
echo %INFO% 创建数据目录...

REM 创建基础目录结构
if not exist "%DOCKER_DIR%\data\%env%" mkdir "%DOCKER_DIR%\data\%env%"
if not exist "%DOCKER_DIR%\data\%env%\mysql" mkdir "%DOCKER_DIR%\data\%env%\mysql"
if not exist "%DOCKER_DIR%\data\%env%\mysql-logs" mkdir "%DOCKER_DIR%\data\%env%\mysql-logs"
if not exist "%DOCKER_DIR%\data\%env%\backend-logs" mkdir "%DOCKER_DIR%\data\%env%\backend-logs"
if not exist "%DOCKER_DIR%\data\%env%\uploads" mkdir "%DOCKER_DIR%\data\%env%\uploads"
if not exist "%DOCKER_DIR%\data\%env%\nginx-logs" mkdir "%DOCKER_DIR%\data\%env%\nginx-logs"

if not "%env%"=="development" (
    if not exist "%DOCKER_DIR%\data\%env%\backups" mkdir "%DOCKER_DIR%\data\%env%\backups"
)

goto :eof

REM 部署环境
:deploy_environment
set "env=%1"
echo %INFO% 开始部署 %env% 环境...

call :check_files %env%
if errorlevel 1 exit /b 1

call :create_directories %env%

echo %INFO% 启动Docker Compose服务...
cd /d "%DOCKER_DIR%"
docker-compose -f "%compose_file%" --env-file "%env_file%" up -d

if errorlevel 1 (
    echo %ERROR% Docker Compose启动失败
    exit /b 1
)

echo %INFO% 等待服务启动...
timeout /t 10 /nobreak >nul

call :check_services %env%

echo %INFO% %env% 环境部署完成!
goto :eof

REM 停止环境
:stop_environment
set "env=%1"
echo %INFO% 停止 %env% 环境...

call :get_compose_file %env%
call :get_env_file %env%

cd /d "%DOCKER_DIR%"
docker-compose -f "%compose_file%" --env-file "%env_file%" down

echo %INFO% %env% 环境已停止
goto :eof

REM 重启环境
:restart_environment
set "env=%1"
echo %INFO% 重启 %env% 环境...

call :stop_environment %env%
timeout /t 5 /nobreak >nul
call :deploy_environment %env%
goto :eof

REM 查看环境状态
:check_services
set "env=%1"
echo %INFO% 检查 %env% 环境服务状态...

call :get_compose_file %env%
call :get_env_file %env%

cd /d "%DOCKER_DIR%"
docker-compose -f "%compose_file%" --env-file "%env_file%" ps

echo %INFO% 执行健康检查...
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | findstr anxin-%env%
goto :eof

REM 查看日志
:view_logs
set "env=%1"
set "service=%2"

call :get_compose_file %env%
call :get_env_file %env%

cd /d "%DOCKER_DIR%"

if "%service%"=="" (
    echo %INFO% 查看 %env% 环境所有服务日志...
    docker-compose -f "%compose_file%" --env-file "%env_file%" logs -f
) else (
    echo %INFO% 查看 %env% 环境 %service% 服务日志...
    docker-compose -f "%compose_file%" --env-file "%env_file%" logs -f %service%
)
goto :eof

REM 清理环境
:clean_environment
set "env=%1"
echo %WARN% 即将清理 %env% 环境 (包括数据卷)...
set /p "confirm=确认继续? (y/N): "

if /i "%confirm%"=="y" (
    echo %INFO% 清理 %env% 环境...
    call :get_compose_file %env%
    call :get_env_file %env%
    cd /d "%DOCKER_DIR%"
    docker-compose -f "%compose_file%" --env-file "%env_file%" down -v --remove-orphans
    docker system prune -f
    echo %INFO% %env% 环境清理完成
) else (
    echo %INFO% 取消清理操作
)
goto :eof

REM 备份环境数据
:backup_environment
set "env=%1"
echo %INFO% 备份 %env% 环境数据...

if "%env%"=="production" (
    docker exec anxin-mysql-prod /backup.sh
) else if "%env%"=="staging" (
    docker exec anxin-mysql-staging /backup.sh
) else if "%env%"=="development" (
    echo %WARN% 开发环境通常不需要备份
)

echo %INFO% %env% 环境数据备份完成
goto :eof

REM 列出所有环境
:list_environments
echo %INFO% 可用环境:
echo   development (dev)  - 开发环境
echo   staging     (stage) - 测试环境
echo   production  (prod)  - 生产环境
echo.

echo %INFO% 当前运行的容器:
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | findstr anxin
if errorlevel 1 echo   无运行中的容器
goto :eof

REM 检查Docker是否安装
:check_docker
docker --version >nul 2>&1
if errorlevel 1 (
    echo %ERROR% Docker未安装或不在PATH中
    exit /b 1
)

docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo %ERROR% Docker Compose未安装或不在PATH中
    exit /b 1
)
goto :eof

REM 主函数
:main
call :check_docker
if errorlevel 1 exit /b 1

set "command=%1"
set "environment=%2"

if "%command%"=="deploy" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :deploy_environment !validated_env!
) else if "%command%"=="stop" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :stop_environment !validated_env!
) else if "%command%"=="restart" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :restart_environment !validated_env!
) else if "%command%"=="status" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :check_services !validated_env!
) else if "%command%"=="logs" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :view_logs !validated_env! %3
) else if "%command%"=="clean" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :clean_environment !validated_env!
) else if "%command%"=="backup" (
    if "%environment%"=="" (
        echo %ERROR% 请指定环境
        call :show_help
        exit /b 1
    )
    call :validate_environment %environment%
    if errorlevel 1 exit /b 1
    call :backup_environment !validated_env!
) else if "%command%"=="list" (
    call :list_environments
) else if "%command%"=="help" (
    call :show_help
) else if "%command%"=="--help" (
    call :show_help
) else if "%command%"=="-h" (
    call :show_help
) else (
    echo %ERROR% 未知命令: %command%
    call :show_help
    exit /b 1
)

goto :eof

REM 执行主函数
call :main %*