@echo off
chcp 65001 >nul
title WorkBuddy Monitor Build
set "ROOT=%~dp0.."
cd /d "%ROOT%"

echo =============================================
echo   WorkBuddy Monitor - Build Standalone Exe
echo =============================================
echo.

:: Check Python
where python >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo [Error] Python not found, please install Python 3.9+
    pause
    exit /b 1
)

:: Check PyInstaller
python -m PyInstaller --version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo [Step 1/3] Installing PyInstaller...
    pip install pyinstaller
)

echo [Step 1/3] Installing dependencies...
pip install -r "%ROOT%\config\requirements.txt"

echo.
echo [Step 2/3] Building standalone exe (this may take 2-5 min)...
echo.

:: Clean old builds
if exist "artifacts\dist\web" rmdir /s /q "artifacts\dist\web" 2>nul
if exist "build_tmp" rmdir /s /q "build_tmp" 2>nul

:: Build wb-monitor-web.exe
echo [Build] Web dashboard + MITM proxy...
python -m PyInstaller ^
    --onefile ^
    --name "wb-monitor-web" ^
    --distpath "artifacts\dist\web" ^
    --workpath "build_tmp" ^
    --specpath "build_tmp" ^
    --hidden-import llm_proxy ^
    --hidden-import flask ^
    --hidden-import cryptography ^
    --hidden-import cryptography.hazmat.backends.openssl ^
    --hidden-import cryptography.hazmat.primitives ^
    --hidden-import cryptography.x509 ^
    --hidden-import email ^
    --hidden-import email.mime ^
    --hidden-import email.mime.multipart ^
    --hidden-import email.mime.text ^
    --collect-all flask ^
    --collect-all cryptography ^
    run.py

:: Clean up
if exist "build_tmp" rmdir /s /q "build_tmp"
if exist "*.spec" del /q "*.spec" 2>nul

echo.
echo [Step 3/3] Build complete!
echo.
echo ================ Output ================
echo.
echo   artifacts\dist\web\wb-monitor-web.exe
echo     - Full dashboard + MITM proxy
echo     - Double-click to run
echo     - No Python installation needed
echo.
echo ============ How to deploy ============
echo.
echo   1. Copy wb-monitor-web.exe to any Windows PC
echo   2. Double-click to start
echo   3. Open http://127.0.0.1:5910 in browser
echo.
pause
