@echo off
setlocal EnableExtensions EnableDelayedExpansion

REM ==========================================
REM  Configuration
REM ==========================================
chcp 65001 >nul

REM Move to project root
cd /d "%~dp0.."
set "PROJECT_DIR=%CD%\"

set "DIST_DIR=%PROJECT_DIR%docs\.vuepress\dist"
set "CACHE_DIR=%PROJECT_DIR%docs\.vuepress\.cache"
set "TEMP_DIR=%PROJECT_DIR%docs\.vuepress\.temp"
set "PUBLISH_DIR=%PROJECT_DIR%_publish"
set "REMOTE_URL=https://gitea.simengweb.com/si-meng-spec/build_notes_simengweb.git"
set "BRANCH=main"

REM High memory limit for better performance/stability (8GB)
set "NODE_OPTIONS=--max-old-space-size=8192"

REM Disable Git SSL Verification for this session
git config --global http.sslVerify false
git config --global http.sslBackend openssl

REM Args
set "COMMIT_MSG=%~1"
if "%COMMIT_MSG%"=="" set "COMMIT_MSG=Build: %DATE% %TIME%"

echo ==========================================
echo  Start Build and Publish
echo  Project Dir: %PROJECT_DIR%
echo  Memory Limit: 8192 MB
echo ==========================================

REM ==========================================
REM  1. Cleanup
REM ==========================================
echo [1/5] Cleaning up old artifacts...

if exist "%DIST_DIR%" (
    echo   - Removing %DIST_DIR%
    rd /s /q "%DIST_DIR%"
)
if exist "%CACHE_DIR%" (
    echo   - Removing %CACHE_DIR%
    rd /s /q "%CACHE_DIR%"
)
if exist "%TEMP_DIR%" (
    echo   - Removing %TEMP_DIR%
    rd /s /q "%TEMP_DIR%"
)
REM Clean node_modules for a fresh install every time (Ensures clean state)
if exist "%PROJECT_DIR%node_modules" (
    echo   - Removing node_modules (Fresh Install Mode)
    rd /s /q "%PROJECT_DIR%node_modules"
)

REM ==========================================
REM  2. Install Dependencies
REM ==========================================
echo [2/5] Installing dependencies (npm ci)...
call npm ci
if %ERRORLEVEL% NEQ 0 (
    echo [Error] npm ci failed.
    exit /b 1
)

REM ==========================================
REM  3. Build
REM ==========================================
echo [3/5] Building docs...
call npm run docs:build
if %ERRORLEVEL% NEQ 0 (
    echo [Error] Build failed.
    exit /b 1
)

if not exist "%DIST_DIR%" (
    echo [Error] Dist directory not found after build.
    exit /b 1
)

REM ==========================================
REM  4. Prepare Publish Directory
REM ==========================================
echo [4/5] Preparing publish directory...

if not exist "%PUBLISH_DIR%\.git" (
    echo   - Cloning publish repo...
    if exist "%PUBLISH_DIR%" rd /s /q "%PUBLISH_DIR%"
    git clone "%REMOTE_URL%" "%PUBLISH_DIR%"
    if !ERRORLEVEL! NEQ 0 exit /b 1
) else (
    echo   - Updating publish repo...
    pushd "%PUBLISH_DIR%"
    git fetch --all
    git checkout "%BRANCH%"
    git reset --hard "origin/%BRANCH%"
    popd
)

echo   - Cleaning publish directory content (preserving .git)...
pushd "%PUBLISH_DIR%"
for /f "delims=" %%F in ('dir /a /b') do (
  if /I not "%%F"==".git" (
    if exist "%%F" (
      attrib -R "%%F" >nul 2>&1
      rd /s /q "%%F" 2>nul
      del /f /q "%%F" 2>nul
    )
  )
)
popd

echo   - Copying new build artifacts...
robocopy "%DIST_DIR%" "%PUBLISH_DIR%" *.* /E /NFL /NDL /NP /NJH /NJS >nul
if %ERRORLEVEL% GEQ 8 (
    echo [Error] Robocopy failed.
    exit /b 1
)

REM ==========================================
REM  5. Push to Remote
REM ==========================================
echo [5/5] Pushing changes...
pushd "%PUBLISH_DIR%"

if defined GIT_USERNAME git config user.name "%GIT_USERNAME%"
if defined GIT_EMAIL git config user.email "%GIT_EMAIL%"

git add -A
git diff --cached --quiet
if %ERRORLEVEL% EQU 0 (
    echo [Info] No changes to commit.
) else (
    git commit -m "%COMMIT_MSG%"
    echo   - Pushing to %BRANCH%...
    git push origin "%BRANCH%"
    if !ERRORLEVEL! NEQ 0 (
        echo [Error] Git push failed.
        popd
        exit /b 1
    )
    echo [Success] Pushed successfully.
)
popd

echo ==========================================
echo  All Done!
echo ==========================================
exit /b 0
