@echo off
REM =====================================================================
REM Kiwy Signage Player - Windows Build Script
REM =====================================================================
REM This script builds a standalone Windows .exe using PyInstaller.
REM
REM Prerequisites:
REM   1. Python 3.10+ installed (with "Add to PATH" checked)
REM   2. Visual C++ Redistributable (for ffpyplayer)
REM   3. FFmpeg binaries in PATH (optional, for video codec support)
REM
REM Steps:
REM   1. Run this script from the project root or the windows\ folder
REM   2. The .exe will be created in windows\dist\KiwySignagePlayer\
REM =====================================================================

setlocal enabledelayedexpansion

cd /d "%~dp0"

echo ============================================
echo  Kiwy Signage Player - Windows Build
echo ============================================
echo.

REM ---- Check Python ----
where python >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Python not found! Please install Python 3.10+ and add it to PATH.
    pause
    exit /b 1
)

echo [INFO] Using Python:
python --version

REM ---- Create virtual environment (if not exists) ----
if not exist "venv\Scripts\python.exe" (
    echo.
    echo [STEP] Creating virtual environment...
    python -m venv venv
    if %ERRORLEVEL% neq 0 (
        echo [ERROR] Failed to create virtual environment.
        pause
        exit /b 1
    )
) else (
    echo [INFO] Virtual environment already exists.
)

REM ---- Activate virtual environment ----
call venv\Scripts\activate.bat

REM ---- Install/upgrade pip ----
echo.
echo [STEP] Upgrading pip...
python -m pip install --upgrade pip

REM ---- Install dependencies ----
echo.
echo [STEP] Installing Windows dependencies...
pip install -r requirements_win.txt
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Failed to install dependencies.
    pause
    exit /b 1
)

REM ---- Verify Kivy installation ----
echo.
echo [STEP] Verifying Kivy installation...
python -c "import kivy; print(f'Kivy {kivy.__version__}')" 2>&1
if %ERRORLEVEL% neq 0 (
    echo [WARNING] Kivy check failed. Build may still work but test carefully.
)

REM ---- Check PyInstaller ----
echo.
echo [STEP] Verifying PyInstaller...
python -c "import PyInstaller; print(f'PyInstaller {PyInstaller.__version__}')" 2>&1
if %ERRORLEVEL% neq 0 (
    echo [ERROR] PyInstaller not found.
    pause
    exit /b 1
)

REM ---- Create app icon (from PNG if possible) ----
echo.
echo [STEP] Checking for app icon...
if not exist "..\config\resources\app_icon.ico" (
    echo [INFO] No .ico icon found. Will use default PyInstaller icon.
    echo [INFO] To add a custom icon, place app_icon.ico in config\resources\
)

REM ---- Run PyInstaller ----
echo.
echo [STEP] Building executable with PyInstaller...
echo This may take several minutes. Please wait...
echo.

pyinstaller build.spec --clean --noconfirm
if %ERRORLEVEL% neq 0 (
    echo.
    echo [ERROR] PyInstaller build failed!
    echo Check the output above for error details.
    pause
    exit /b 1
)

REM ---- Success ----
echo.
echo ============================================
echo  BUILD COMPLETE!
echo ============================================
echo.
echo Output: %~dp0dist\KiwySignagePlayer\
echo.
echo The executable is:
echo   %~dp0dist\KiwySignagePlayer\KiwySignagePlayer.exe
echo.
echo To run: Double-click KiwySignagePlayer.exe
echo.
echo Note: The first run may take a while as Windows Defender
echo scans the executable. This is normal.
echo.
pause
