@echo off
chcp 65001 >nul 2>&1
setlocal

echo ============================================================
echo   BidPartner - Build Desktop EXE
echo ============================================================
echo.

:: ── 1. Check Python ────────────────────────────────────────────────────────
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found. Please install Python 3.9+.
    pause & exit /b 1
)

:: ── 2. Install / upgrade PyInstaller ───────────────────────────────────────
echo [Step 1/4] Installing PyInstaller...
pip install --quiet --upgrade pyinstaller
if errorlevel 1 (
    echo [ERROR] Failed to install PyInstaller.
    pause & exit /b 1
)

:: ── 3. Install project dependencies (if not already installed) ─────────────
echo [Step 2/4] Checking dependencies...
pip install --quiet -r requirements.txt
if errorlevel 1 (
    echo [ERROR] Failed to install dependencies.
    pause & exit /b 1
)

:: ── 4. Sanitize settings.json - REMOVE API KEYS before build ───────────────
echo [Step 3/4] Sanitizing settings (removing API keys from build)...
if exist "data\settings.json" (
    :: Back up real settings
    copy /y "data\settings.json" "data\settings.json.bak" >nul
)
:: Write a clean settings file with no real keys
(
    echo {
    echo   "model_provider": "deepseek",
    echo   "qwen_api_key": "sk-your-qwen-key",
    echo   "qwen_model": "qwen3.6-plus",
    echo   "openai_api_key": "sk-your-openai-key",
    echo   "openai_model": "gpt-4o",
    echo   "deepseek_api_key": "sk-your-deepseek-key",
    echo   "deepseek_model": "deepseek-chat",
    echo   "max_concurrent": 5,
    echo   "content_volume": "standard"
    echo }
) > "data\settings_clean.tmp"

:: ── 5. Build ────────────────────────────────────────────────────────────────
echo [Step 4/4] Building EXE with PyInstaller...
echo   (This may take 3-10 minutes on first run)
echo.

:: Clean previous build artifacts
if exist "build" rd /s /q "build" >nul 2>&1
if exist "dist\BidPartner" rd /s /q "dist\BidPartner" >nul 2>&1

pyinstaller bid_partner.spec --noconfirm
set BUILD_RESULT=%errorlevel%

:: ── Restore real settings ───────────────────────────────────────────────────
if exist "data\settings.json.bak" (
    copy /y "data\settings.json.bak" "data\settings.json" >nul
    del /f /q "data\settings.json.bak" >nul 2>&1
)
del /f /q "data\settings_clean.tmp" >nul 2>&1

if %BUILD_RESULT% neq 0 (
    echo.
    echo [ERROR] PyInstaller build failed. See output above for details.
    pause & exit /b 1
)

:: ── 6. Result ───────────────────────────────────────────────────────────────
echo.
echo ============================================================
echo   Build SUCCESSFUL!
echo   Output: dist\BidPartner\bid_partner.exe
echo ============================================================
echo.
echo   The 'dist\BidPartner' folder is your distributable package.
echo   Users only need this folder - no Python installation required.
echo   Each user must set their own API key in the app settings.
echo.

:: Open the output folder
explorer "dist\BidPartner" >nul 2>&1

endlocal
pause
