@echo off
REM rmtPocketWatcher Windows Build Script (Batch version)
REM Creates both standalone executable and MSIX installer

echo Building rmtPocketWatcher for Windows (Release mode)
echo =============================================

REM Clean previous builds
echo Cleaning previous builds...
flutter clean
if exist "build" rmdir /s /q "build"

REM Install dependencies
echo Installing dependencies...
flutter pub get

REM Build Flutter Windows app
echo Building Flutter Windows app...
flutter build windows --release

REM Check if build was successful
if not exist "build\windows\x64\runner\Release\rmtpocketwatcher.exe" (
    echo ERROR: Build failed - executable not found
    pause
    exit /b 1
)

echo ✓ Flutter build completed successfully

REM Create standalone executable directory
echo Creating standalone executable package...
if exist "build\windows\standalone" rmdir /s /q "build\windows\standalone"
mkdir "build\windows\standalone"

REM Copy all necessary files for standalone distribution
xcopy "build\windows\x64\runner\Release\*" "build\windows\standalone\" /E /I /H /Y

REM Create version info
echo rmtPocketWatcher - Lambda Banking Conglomerate > "build\windows\standalone\README.txt"
echo Star Citizen AUEC Price Tracker >> "build\windows\standalone\README.txt"
echo. >> "build\windows\standalone\README.txt"
echo To run: Double-click rmtpocketwatcher.exe >> "build\windows\standalone\README.txt"
echo No installation required - all dependencies included. >> "build\windows\standalone\README.txt"

echo ✓ Standalone executable created at: build\windows\standalone

REM Create MSIX installer (optional)
echo Creating MSIX installer...
flutter pub run msix:create
if %errorlevel% neq 0 (
    echo WARNING: MSIX installer creation failed, continuing with standalone only...
) else (
    echo ✓ MSIX installer created
)

REM Create distribution archive
echo Creating distribution archive...
powershell -Command "Compress-Archive -Path 'build\windows\standalone\*' -DestinationPath 'build\rmtPocketWatcher-Windows-Standalone.zip' -CompressionLevel Optimal -Force"

echo.
echo 🎉 Build completed successfully!
echo =============================================
echo Standalone executable: build\windows\standalone\rmtpocketwatcher.exe
echo Distribution archive: build\rmtPocketWatcher-Windows-Standalone.zip
echo.
echo To test: cd build\windows\standalone ^&^& rmtpocketwatcher.exe
echo To distribute: Share the ZIP file
echo.
pause