@echo off
setlocal EnableDelayedExpansion
title DeployPro Build

echo.
echo  =========================================
echo   DeployPro — Build / Package
echo  =========================================
echo.

:: ─── Config ────────────────────────────────────────────────────
set "ROOT=%~dp0"
set "ROOT=%ROOT:~0,-1%"

:: Read version from package.json (simple grep)
for /f "tokens=2 delims=:, " %%v in ('findstr /i "\"version\"" "%ROOT%\package.json"') do (
    set "VERSION=%%~v"
    goto :gotver
)
:gotver
set "VERSION=%VERSION:"=%"
if "%VERSION%"=="" set "VERSION=1.0.0"

set "DIST_NAME=deploypro-setup"
set "DIST_DIR=%ROOT%\dist\%DIST_NAME%"
set "ZIP_OUT=%ROOT%\dist\deploypro-setup.zip"

echo  Version       : %VERSION%
echo  Output ZIP    : %ZIP_OUT%
echo.

:: ─── Clean dist ────────────────────────────────────────────────
if exist "%DIST_DIR%" (
    echo  Cleaning previous build...
    rmdir /s /q "%DIST_DIR%"
)
mkdir "%DIST_DIR%"

:: ─── Copy source files (exclude dev/data dirs) ─────────────────
echo  Copying source files...

:: Root files
for %%f in ("%ROOT%\server.js" "%ROOT%\package.json" "%ROOT%\README.md" "%ROOT%\web.config" "%ROOT%\start.bat" "%ROOT%\install-service.bat" "%ROOT%\uninstall-service.bat" "%ROOT%\service-manager.bat") do (
    if exist "%%f" copy /y "%%f" "%DIST_DIR%\" >nul
)

:: Directories to include
for %%d in (public routes utils middleware service) do (
    if exist "%ROOT%\%%d" (
        xcopy /e /i /y /q "%ROOT%\%%d" "%DIST_DIR%\%%d" >nul
    )
)

:: Include installer folder
xcopy /e /i /y /q "%ROOT%\installer" "%DIST_DIR%\installer" >nul

:: ─── Trim dev artifacts from public copy ───────────────────────
:: (nothing to trim in public — keep all)

:: ─── Create placeholder data dirs ──────────────────────────────
for %%d in (data uploads backups logs dist) do (
    mkdir "%DIST_DIR%\%%d" >nul 2>&1
)

:: ─── Write version into package.json copy ──────────────────────
:: (already copied above, version already correct)

:: ─── Pre-install production node_modules ─────────────────────────
echo  Running npm install --production ...
pushd "%DIST_DIR%"
call npm install --production --no-audit --no-fund >nul 2>&1
if %errorlevel% NEQ 0 (
    echo  WARNING: npm install encountered errors. ZIP may be missing modules.
)
popd

:: ─── Create ZIP ────────────────────────────────────────────────
echo  Creating ZIP archive...

if exist "%ZIP_OUT%" del /q "%ZIP_OUT%"

:: Use PowerShell to create the zip
powershell -NoProfile -Command "Compress-Archive -Path '%DIST_DIR%\*' -DestinationPath '%ZIP_OUT%' -Force"

if exist "%ZIP_OUT%" (
    for %%f in ("%ZIP_OUT%") do set "ZIP_SIZE=%%~zf"
    set /a ZIP_MB=!ZIP_SIZE! / 1048576
    echo.
    echo  =========================================
    echo   Build complete!
    echo  -----------------------------------------
    echo   Output  : %ZIP_OUT%
    echo   Size    : ~!ZIP_MB! MB
    echo  =========================================
    echo.
    echo  Users can download this ZIP from:
    echo    http://your-server:7070/download/deploypro-setup.zip
    echo.
) else (
    echo  ERROR: ZIP creation failed.
    exit /b 1
)

endlocal
pause
