@echo off
REM ============================================================================
REM  GB5 BusinessHost - Windows Build
REM  Produces a self-contained win-x64 publish with Chromium pre-baked in, so
REM  PDF report generation never needs a runtime download on the server.
REM  Output: C:\GB5BuildFile\host\businesshost
REM ============================================================================
setlocal EnableExtensions EnableDelayedExpansion

set "GB5SHARED_DIR=C:\gb5\GB5Shared"
set "BUSINESS_DIR=C:\gb5\GB5Solution\Hosts\Business"
set "OUT_DIR=C:\GB5BuildFile\host\businesshost"
set "CHROME_CACHE_SRC=%LOCALAPPDATA%\ChromiumCache"
set "CHROME_CACHE_DST=%OUT_DIR%\ChromiumCache"

echo ============================================================================
echo  GB5 BusinessHost Windows Build - starting
echo ============================================================================

REM ---------------------------------------------------------------------------
REM Step 1/3: Build GB5Shared (Debug)
REM BusinessHost references GB5Shared via a fixed HintPath, not a project
REM reference - publishing BusinessHost alone will NOT pick up GB5Shared changes.
REM ---------------------------------------------------------------------------
echo.
echo [1/3] Building GB5Shared (Debug)...
if not exist "%GB5SHARED_DIR%\GB5Shared.csproj" (
    echo.
    echo [ERROR] GB5Shared.csproj not found at %GB5SHARED_DIR%
    echo         Check that this script's paths still match your repo layout.
    goto :fail
)
cd /d "%GB5SHARED_DIR%"
dotnet build GB5Shared.csproj -c Debug
if errorlevel 1 (
    echo.
    echo [ERROR] GB5Shared build FAILED - see the dotnet output above for the exact error.
    echo         Aborting: publishing BusinessHost now would just bundle a stale/broken GB5Shared.dll.
    goto :fail
)
echo [OK] GB5Shared built successfully.

REM ---------------------------------------------------------------------------
REM Step 2/3: Publish BusinessHost for win-x64, self-contained
REM ---------------------------------------------------------------------------
echo.
echo [2/3] Publishing BusinessHost for win-x64 (self-contained)...
if not exist "%BUSINESS_DIR%\BusinessHost\BusinessHost.csproj" (
    echo.
    echo [ERROR] BusinessHost.csproj not found at %BUSINESS_DIR%\BusinessHost
    goto :fail
)
cd /d "%BUSINESS_DIR%"
dotnet publish "BusinessHost\BusinessHost.csproj" -c Release -r win-x64 --self-contained true -p:PublishReadyToRun=false -o "%OUT_DIR%"
if errorlevel 1 (
    echo.
    echo [ERROR] BusinessHost publish FAILED - see the dotnet output above for the exact error.
    goto :fail
)
if not exist "%OUT_DIR%\BusinessHost.exe" (
    echo.
    echo [ERROR] dotnet publish reported success but BusinessHost.exe is missing from %OUT_DIR%
    echo         Treating this as a failed build.
    goto :fail
)
echo [OK] BusinessHost.exe published to %OUT_DIR%

REM ---------------------------------------------------------------------------
REM Step 3/3: Seed the Chromium cache - REQUIRED for PDF generation to work.
REM Without this, the server would try to download Chromium on the first PDF
REM request, which hangs/fails on a server with no outbound internet access.
REM ---------------------------------------------------------------------------
echo.
echo [3/3] Seeding Chromium cache for PDF report generation...
if not exist "%CHROME_CACHE_SRC%\" (
    echo.
    echo [ERROR] No Chromium cache found at %CHROME_CACHE_SRC%
    echo         PDF generation needs Chromium available locally before it can be baked into this build.
    echo         FIX: generate any PDF report once on THIS machine first ^(so Puppeteer downloads Chrome
    echo         into that folder^), then re-run this script.
    goto :fail
)
if not exist "%CHROME_CACHE_DST%" mkdir "%CHROME_CACHE_DST%"
robocopy "%CHROME_CACHE_SRC%" "%CHROME_CACHE_DST%" /E /R:2 /W:2 /NFL /NDL
if %ERRORLEVEL% GEQ 8 (
    echo.
    echo [ERROR] Copying the Chromium cache failed ^(robocopy exit code %ERRORLEVEL%^).
    echo         Check disk space and write permissions on %OUT_DIR%.
    goto :fail
)
dir /s /b "%CHROME_CACHE_DST%\chrome.exe" >nul 2>&1
if errorlevel 1 (
    echo.
    echo [ERROR] chrome.exe was not found anywhere under %CHROME_CACHE_DST% after copying.
    echo         PDF generation will NOT work on this build until this is fixed.
    goto :fail
)
echo [OK] Chromium cache seeded - chrome.exe confirmed present under %CHROME_CACHE_DST%

REM ---------------------------------------------------------------------------
REM Informational only (does not fail the build): these 4 report template/
REM asset files exist only on the Linux production server today. The build
REM succeeds without them, but PDF header/footer/logo will be blank until
REM they're copied in manually.
REM ---------------------------------------------------------------------------
set "MISSING_ASSETS="
if not exist "%OUT_DIR%\Reports\default.txt"      set "MISSING_ASSETS=!MISSING_ASSETS! default.txt"
if not exist "%OUT_DIR%\Reports\headerhtml.html"  set "MISSING_ASSETS=!MISSING_ASSETS! headerhtml.html"
if not exist "%OUT_DIR%\Reports\footer.html"      set "MISSING_ASSETS=!MISSING_ASSETS! footer.html"
if not exist "%OUT_DIR%\Reports\Unisoft1.png"     set "MISSING_ASSETS=!MISSING_ASSETS! Unisoft1.png"

echo.
echo ============================================================================
echo  BUILD SUCCEEDED
echo  Output:         %OUT_DIR%
echo  BusinessHost.exe and a pre-baked Chromium are both confirmed present -
echo  PDF report generation will work with zero runtime network dependency.
echo ============================================================================
if defined MISSING_ASSETS (
    echo.
    echo  [WARNING] Report template assets not yet present ^(does not block PDFs from
    echo  generating, but header/footer/logo will be blank^):%MISSING_ASSETS%
    echo  These only exist on the Linux server ^(/root/GB5BuildFile/Reports/^) - fetch
    echo  them with pscp once you have GB5_DEPLOY_PASS. Ask if you want that folded
    echo  into this script too.
)
goto :end

:fail
echo.
echo ============================================================================
echo  BUILD FAILED - see the [ERROR] message above for the exact cause.
echo ============================================================================
endlocal
exit /b 1

:end
endlocal
exit /b 0
