@echo off
echo ========================================
echo DMS Sales App - Android APK Builder
echo ========================================
echo.

:menu
echo Pilih opsi build:
echo 1. Build APK Debug (untuk testing)
echo 2. Build APK Release (untuk production)
echo 3. Build dan Buka Android Studio
echo 4. Sync Capacitor saja
echo 5. Exit
echo.
set /p choice="Masukkan pilihan (1-5): "

if "%choice%"=="1" goto debug
if "%choice%"=="2" goto release
if "%choice%"=="3" goto studio
if "%choice%"=="4" goto sync
if "%choice%"=="5" goto end
echo Pilihan tidak valid!
goto menu

:debug
echo.
echo [1/3] Building React app...
call npm run build
if errorlevel 1 (
    echo Error: Build React app gagal!
    pause
    goto menu
)

echo.
echo [2/3] Syncing dengan Android...
call npx cap sync android
if errorlevel 1 (
    echo Error: Sync Capacitor gagal!
    pause
    goto menu
)

echo.
echo [3/3] Building APK Debug...
cd android
call gradlew assembleDebug
if errorlevel 1 (
    echo Error: Build APK gagal!
    cd ..
    pause
    goto menu
)
cd ..

echo.
echo ========================================
echo APK Debug berhasil dibuat!
echo Lokasi: android\app\build\outputs\apk\debug\app-debug.apk
echo ========================================
echo.
pause
goto menu

:release
echo.
echo [1/3] Building React app...
call npm run build
if errorlevel 1 (
    echo Error: Build React app gagal!
    pause
    goto menu
)

echo.
echo [2/3] Syncing dengan Android...
call npx cap sync android
if errorlevel 1 (
    echo Error: Sync Capacitor gagal!
    pause
    goto menu
)

echo.
echo [3/3] Building APK Release...
cd android
call gradlew assembleRelease
if errorlevel 1 (
    echo Error: Build APK gagal!
    cd ..
    pause
    goto menu
)
cd ..

echo.
echo ========================================
echo APK Release berhasil dibuat!
echo Lokasi: android\app\build\outputs\apk\release\app-release.apk
echo ========================================
echo.
echo CATATAN: Untuk production, APK harus di-sign dengan keystore!
echo Lihat BUILD_ANDROID.md untuk panduan signing.
echo.
pause
goto menu

:studio
echo.
echo [1/2] Building dan Syncing...
call npm run android:build
if errorlevel 1 (
    echo Error: Build gagal!
    pause
    goto menu
)

echo.
echo [2/2] Membuka Android Studio...
call npx cap open android

echo.
echo Android Studio dibuka. Build APK dari menu:
echo Build ^> Build Bundle(s) / APK(s) ^> Build APK(s)
echo.
pause
goto menu

:sync
echo.
echo Syncing Capacitor dengan Android...
call npx cap sync android
if errorlevel 1 (
    echo Error: Sync gagal!
    pause
    goto menu
)

echo.
echo Sync berhasil!
echo.
pause
goto menu

:end
echo.
echo Terima kasih!
exit
