@echo off
REM 环境设置脚本 (Windows版本)
REM 用于初始化Docker部署环境和配置文件

setlocal enabledelayedexpansion

REM 设置颜色代码
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 日志函数
:log_info
echo %BLUE%[INFO]%NC% %~1
goto :eof

:log_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof

:log_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:log_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 检查必要的命令
:check_requirements
call :log_info "检查系统要求..."

docker --version >nul 2>&1
if errorlevel 1 (
    call :log_error "Docker 未安装，请先安装 Docker Desktop"
    exit /b 1
)

docker-compose --version >nul 2>&1
if errorlevel 1 (
    call :log_error "Docker Compose 未安装，请先安装 Docker Compose"
    exit /b 1
)

call :log_success "系统要求检查通过"
goto :eof

REM 创建必要的目录
:create_directories
call :log_info "创建必要的目录结构..."

REM 数据目录
if not exist "data" mkdir data
if not exist "data\mysql" mkdir data\mysql
if not exist "data\logs" mkdir data\logs
if not exist "data\configs" mkdir data\configs

if not exist "data\dev" mkdir data\dev
if not exist "data\dev\mysql" mkdir data\dev\mysql
if not exist "data\dev\logs" mkdir data\dev\logs
if not exist "data\dev\configs" mkdir data\dev\configs

if not exist "data\staging" mkdir data\staging
if not exist "data\staging\mysql" mkdir data\staging\mysql
if not exist "data\staging\logs" mkdir data\staging\logs
if not exist "data\staging\configs" mkdir data\staging\configs

if not exist "data\prod" mkdir data\prod
if not exist "data\prod\mysql" mkdir data\prod\mysql
if not exist "data\prod\logs" mkdir data\prod\logs
if not exist "data\prod\configs" mkdir data\prod\configs

REM 日志目录
if not exist "logs" mkdir logs
if not exist "logs\dev" mkdir logs\dev
if not exist "logs\staging" mkdir logs\staging
if not exist "logs\prod" mkdir logs\prod

call :log_success "目录结构创建完成"
goto :eof

REM 设置环境配置
:setup_environment
set env=%~1
if "%env%"=="" set env=development

call :log_info "设置 %env% 环境配置..."

REM 检查环境配置文件是否存在
if not exist "environments\.env.%env%" (
    call :log_error "环境配置文件 .env.%env% 不存在"
    exit /b 1
)

REM 复制环境配置文件
copy "environments\.env.%env%" ".env" >nul

REM 复制Docker Compose配置
copy "docker-compose.yml.template" "docker-compose.yml" >nul

REM 复制配置文件模板
copy "configs\nginx.conf.template" "configs\nginx.conf" >nul
copy "configs\application-docker.yml.template" "configs\application-docker.yml" >nul
copy "configs\my.cnf.template" "configs\my.cnf" >nul

call :log_success "%env% 环境配置设置完成"
goto :eof

REM 验证配置
:validate_config
call :log_info "验证配置文件..."

REM 检查 .env 文件
if not exist ".env" (
    call :log_error ".env 文件不存在，请先运行环境设置"
    exit /b 1
)

REM 检查 docker-compose.yml 文件
if not exist "docker-compose.yml" (
    call :log_error "docker-compose.yml 文件不存在"
    exit /b 1
)

REM 验证 Docker Compose 配置语法
docker-compose config >nul 2>&1
if errorlevel 1 (
    call :log_error "Docker Compose 配置文件语法错误"
    exit /b 1
)

call :log_success "配置文件验证通过"
goto :eof

REM 显示帮助信息
:show_help
echo 用法: %~nx0 [选项] [环境]
echo.
echo 选项:
echo   -h, --help     显示此帮助信息
echo   -c, --check    仅检查系统要求
echo   -v, --validate 仅验证配置
echo.
echo 环境:
echo   development    开发环境 (默认)
echo   staging        测试环境
echo   production     生产环境
echo.
echo 示例:
echo   %~nx0                    # 设置开发环境
echo   %~nx0 production         # 设置生产环境
echo   %~nx0 --check            # 仅检查系统要求
goto :eof

REM 主函数
:main
set environment=development
set check_only=false
set validate_only=false

REM 解析命令行参数
:parse_args
if "%~1"=="" goto :start_setup
if "%~1"=="-h" goto :show_help_and_exit
if "%~1"=="--help" goto :show_help_and_exit
if "%~1"=="-c" (
    set check_only=true
    shift
    goto :parse_args
)
if "%~1"=="--check" (
    set check_only=true
    shift
    goto :parse_args
)
if "%~1"=="-v" (
    set validate_only=true
    shift
    goto :parse_args
)
if "%~1"=="--validate" (
    set validate_only=true
    shift
    goto :parse_args
)
if "%~1"=="development" (
    set environment=%~1
    shift
    goto :parse_args
)
if "%~1"=="staging" (
    set environment=%~1
    shift
    goto :parse_args
)
if "%~1"=="production" (
    set environment=%~1
    shift
    goto :parse_args
)

call :log_error "未知参数: %~1"
call :show_help
exit /b 1

:show_help_and_exit
call :show_help
exit /b 0

:start_setup
REM 切换到docker目录
cd /d "%~dp0\.."

call :log_info "开始设置Docker部署环境..."

REM 检查系统要求
call :check_requirements
if errorlevel 1 exit /b 1

if "%check_only%"=="true" (
    call :log_success "系统要求检查完成"
    exit /b 0
)

if "%validate_only%"=="true" (
    call :validate_config
    if errorlevel 1 exit /b 1
    call :log_success "配置验证完成"
    exit /b 0
)

REM 创建目录结构
call :create_directories

REM 设置环境配置
call :setup_environment "%environment%"
if errorlevel 1 exit /b 1

REM 验证配置
call :validate_config
if errorlevel 1 exit /b 1

call :log_success "Docker部署环境设置完成！"
call :log_info "下一步："
call :log_info "1. 检查并修改 .env 文件中的配置"
call :log_info "2. 运行 'scripts\build.bat' 构建镜像"
call :log_info "3. 运行 'scripts\deploy.bat start' 启动服务"

goto :eof

REM 执行主函数
call :main %*