@echo off
REM 泉友米车友会 - Docker 构建脚本 (Windows)

REM ==========================================
REM 版本号配置（修改这里即可）
REM ==========================================
set VERSION_FILE=.version
set MAJOR=0
set MINOR=0

REM 读取或初始化补丁版本号
if exist %VERSION_FILE% (
    set /p PATCH=<%VERSION_FILE%
) else (
    set PATCH=1
)

REM 组合完整版本号
set VERSION=%MAJOR%.%MINOR%.%PATCH%

echo ==========================================
echo 泉友米车友会 Docker 镜像构建
echo 版本: %VERSION%
echo ==========================================

REM 检查 Docker 是否安装
docker --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker 未安装，请先安装 Docker Desktop
    pause
    exit /b 1
)

echo.
echo 开始构建 Docker 镜像...
docker build -t quanyoumi-app:%VERSION% -t quanyoumi-app:latest .

if errorlevel 1 (
    echo.
    echo 构建失败！
    pause
    exit /b 1
)

REM 构建成功后自动递增版本号
set /a NEXT_PATCH=%PATCH%+1
echo %NEXT_PATCH%>%VERSION_FILE%

echo.
echo ==========================================
echo 构建完成！版本号已更新
echo ==========================================
echo.
echo 当前版本: %VERSION%
echo 下次版本: %MAJOR%.%MINOR%.%NEXT_PATCH%
echo.
echo 镜像信息:
docker images | findstr quanyoumi-app
echo.

REM 标记镜像到阿里云仓库
echo 正在标记镜像到阿里云仓库...
docker tag quanyoumi-app:%VERSION% registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:%VERSION%
docker tag quanyoumi-app:latest registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:latest

echo.
echo 正在推送镜像到阿里云仓库...
docker push registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:%VERSION%
docker push registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:latest

if errorlevel 1 (
    echo.
    echo 推送失败！请检查是否已登录阿里云镜像仓库
    echo 登录命令: docker login --username=你的用户名 registry.cn-hangzhou.aliyuncs.com
    pause
    exit /b 1
)

echo.
echo ==========================================
echo 推送完成！
echo ==========================================
echo.
echo 当前版本: %VERSION%
echo 下次版本: %MAJOR%.%MINOR%.%NEXT_PATCH%
echo.
echo 镜像地址:
echo   registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:%VERSION%
echo   registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:latest
echo.
echo 在 NAS 上使用:
echo   docker pull registry.cn-hangzhou.aliyuncs.com/yvan329/quanyoumi-app:%VERSION%
echo   docker-compose up -d
echo.
echo 提示: 如需修改主版本号或次版本号，请编辑 build.bat 文件顶部的 MAJOR 和 MINOR 变量
echo.
pause
