@echo off
setlocal
title Daneshyar Build

echo.
echo  ============================================
echo   Build: Daneshyar — Production for Apache
echo  ============================================
echo.

where node >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] Node.js not found. Install from https://nodejs.org
    pause
    exit /b 1
)

where npm >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] npm not found.
    pause
    exit /b 1
)

cd /d "%~dp0"

if exist "node_modules" (
    echo [1/2] Updating npm packages...
    call npm install
) else (
    echo [1/2] Installing npm packages...
    if exist "package-lock.json" (
        call npm ci
    ) else (
        call npm install
    )
)

if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] npm install failed.
    pause
    exit /b 1
)

echo.
echo [2/2] Building Vite production assets...
call npm run build

if %ERRORLEVEL% NEQ 0 (
    echo [ERROR] Vite build failed. Check TypeScript errors above.
    pause
    exit /b 1
)

echo.
echo  ============================================
echo   BUILD COMPLETE — public/build/ is ready
echo  ============================================
echo.
echo  Next steps:
echo.
echo  1. Upload EVERYTHING in this folder to
echo     the server's html/ directory.
echo.
echo  2. Apache DocumentRoot must point to:
echo        html/public/
echo     NOT html/  (see scripts/server_setup.py)
echo.
echo  3. On the server (SSH), run ONCE after upload:
echo.
echo       cd /var/www/html
echo       python3 scripts/server_setup.py --post-deploy
echo.
echo     This installs vendor/ and runs migrations.
echo.
echo  SSH port: 2435
echo  ssh -p 2435 root@91.107.131.171
echo.
pause
