@echo off
setlocal enabledelayedexpansion

REM =============================================================
REM Genex Frontend Flutter Build Script (Windows)
REM 用法: build.bat <app> <mode> [platform]
REM
REM   app:      mobile | admin-app
REM   mode:     debug | release | clean | run
REM   platform: apk | appbundle | ios (默认 apk)
REM =============================================================

set SCRIPT_DIR=%~dp0
set OUTPUT_DIR=%SCRIPT_DIR%build-output

if "%~2"=="" (
    echo.
    echo 用法: %~nx0 ^<app^> ^<mode^> [platform]
    echo.
    echo   app:      mobile ^| admin-app ^| all
    echo   mode:     debug ^| release ^| clean ^| run
    echo   platform: apk ^| appbundle ^| ios (默认 apk)
    echo.
    echo 示例:
    echo   %~nx0 mobile debug              # debug APK
    echo   %~nx0 mobile release            # release APK
    echo   %~nx0 admin-app release         # release APK
    echo   %~nx0 mobile run                # 连接设备运行 debug
    echo   %~nx0 mobile clean              # 清理缓存
    echo   %~nx0 all debug                 # 构建全部
    echo.
    exit /b 1
)

set APP=%~1
set MODE=%~2
set PLATFORM=%~3
if "%PLATFORM%"=="" set PLATFORM=apk

REM 检查 Flutter
where flutter >nul 2>nul
if errorlevel 1 (
    echo [ERROR] Flutter SDK 未安装或不在 PATH 中
    exit /b 1
)
echo [BUILD] Flutter SDK 已就绪

if "%MODE%"=="clean" goto :do_clean
if "%MODE%"=="run" goto :do_run
if "%MODE%"=="debug" goto :do_build
if "%MODE%"=="release" goto :do_build

echo [ERROR] 未知模式: %MODE%
exit /b 1

:do_clean
if "%APP%"=="all" (
    call :clean_app mobile
    call :clean_app admin-app
) else (
    call :clean_app %APP%
)
goto :eof

:clean_app
echo [BUILD] 清理 %1 ...
cd "%SCRIPT_DIR%%1"
flutter clean
echo [OK] 清理完成
cd "%SCRIPT_DIR%"
goto :eof

:do_run
if "%APP%"=="all" (
    echo [ERROR] 不能同时运行两个 App
    exit /b 1
)
cd "%SCRIPT_DIR%%APP%"
flutter pub get
flutter run
goto :eof

:do_build
if "%APP%"=="all" (
    call :build_app mobile %MODE% %PLATFORM%
    call :build_app admin-app %MODE% %PLATFORM%
    echo.
    echo [OK] =========================================
    echo [OK] 全部构建完成! 产物目录: %OUTPUT_DIR%\
    echo [OK] =========================================
    dir "%OUTPUT_DIR%\" 2>nul
) else (
    call :build_app %APP% %MODE% %PLATFORM%
)
goto :eof

:build_app
set B_APP=%1
set B_MODE=%2
set B_PLATFORM=%3

echo.
echo [BUILD] =========================================
echo [BUILD] 构建: %B_APP%
echo [BUILD] 模式: %B_MODE% ^| 平台: %B_PLATFORM%
echo [BUILD] =========================================

cd "%SCRIPT_DIR%%B_APP%"

echo [BUILD] 获取依赖...
flutter pub get

if "%B_PLATFORM%"=="apk" (
    echo [BUILD] flutter build apk --%B_MODE%
    flutter build apk --%B_MODE%

    if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
    set SRC_APK=build\app\outputs\flutter-apk\app-%B_MODE%.apk
    set DST_APK=%OUTPUT_DIR%\%B_APP%-%B_MODE%.apk

    if exist "!SRC_APK!" (
        copy /Y "!SRC_APK!" "!DST_APK!" >nul
        echo [OK] 构建成功!
        echo [OK] 产物: !DST_APK!
    ) else (
        echo [OK] 构建成功!
    )
) else if "%B_PLATFORM%"=="appbundle" (
    echo [BUILD] flutter build appbundle --%B_MODE%
    flutter build appbundle --%B_MODE%
    echo [OK] 构建成功!
) else (
    echo [ERROR] 未知平台: %B_PLATFORM%
)

cd "%SCRIPT_DIR%"
goto :eof
