@echo off
echo ========================================
echo   ZhiTu Build Script
echo ========================================
echo.

:: Check if in correct directory
if not exist "www\js\app.js" (
    echo [ERROR] Please run this script from project root!
    pause
    exit /b 1
)

echo [1/4] Sync files to www directory...
xcopy /Y js\*.js www\js\ >nul
xcopy /Y css\*.css www\css\ >nul
xcopy /Y index.html www\ >nul
echo Done: Files synced
echo.

echo [2/4] Sync to Android platform...
call npx cap sync android
if errorlevel 1 (
    echo [ERROR] Capacitor sync failed!
    pause
    exit /b 1
)
echo Done: Android sync completed
echo.

echo [3/4] Build APK...
cd android
call gradlew assembleDebug
if errorlevel 1 (
    echo [ERROR] APK build failed!
    cd ..
    pause
    exit /b 1
)
cd ..
echo Done: APK built
echo.

echo [4/4] Install to device/emulator...
adb uninstall com.zhitu.soldier >nul 2>&1
adb install android\app\build\outputs\apk\debug\app-debug.apk
if errorlevel 1 (
    echo [WARN] APK install failed, check device connection
    echo.
    echo APK location: android\app\build\outputs\apk\debug\app-debug.apk
) else (
    echo Done: APK installed
)

echo.
echo ========================================
echo   Build Complete!
echo ========================================
pause
