@echo off
:: install.bat — Set up Powernet and optionally build a standalone .exe
::
:: Usage:
::   install.bat          install dependencies only
::   install.bat --build  install deps + build a PyInstaller .exe

setlocal enabledelayedexpansion
cd /d "%~dp0"

echo === Powernet installer ===

:: ── 1. Ensure uv is available ──────────────────────────────────────────────
where uv >nul 2>&1
if errorlevel 1 (
    echo [*] uv not found -- installing via pip...
    pip install --quiet uv
    if errorlevel 1 (
        echo [!] pip also not found. Please install Python 3.12+ from https://python.org
        pause
        exit /b 1
    )
)

:: ── 2. Create venv + install dependencies ─────────────────────────────────
echo [*] Installing dependencies...
uv sync
if errorlevel 1 (
    echo [!] Dependency installation failed.
    pause
    exit /b 1
)
echo [*] Dependencies installed.

:: ── 3. Run the game (default) ─────────────────────────────────────────────
if /i not "%~1"=="--build" (
    echo.
    echo To run the game:
    echo   uv run python src\main.py
    echo.
    echo To build a standalone .exe:
    echo   install.bat --build
    echo.
    pause
    exit /b 0
)

:: ── 4. Build standalone .exe with PyInstaller ─────────────────────────────
echo [*] Installing PyInstaller...
uv pip install pyinstaller
if errorlevel 1 (
    echo [!] PyInstaller installation failed.
    pause
    exit /b 1
)

echo [*] Building standalone executable...

uv run pyinstaller ^
    --noconfirm ^
    --onedir ^
    --windowed ^
    --name "Powernet" ^
    --add-data "src\assets;assets" ^
    --paths "src" ^
    "src\main.py"

if errorlevel 1 (
    echo [!] Build failed. Check output above for errors.
    pause
    exit /b 1
)

echo.
echo === Build complete ===
echo Executable: dist\Powernet\Powernet.exe
echo.
pause
