
@echo off
setlocal ENABLEDELAYEDEXPANSION
set "ROOT=%~dp0"
set "GEN=%ROOT%bin\api-doc-generator-win.exe"
set "JAVASOURCE=%ROOT%samples\CServer.java"
set "OUTJSON=%ROOT%output\openapi.json"
set "HTMLDOC=%ROOT%output\integrated-xqual-doc.html"

if not exist "%GEN%" (
  echo [ERROR] Missing generator: "%GEN%"
  exit /b 1
)

if exist "%~1" (
  set "INFILE=%~1"
) else (
  set "INFILE=%JAVASOURCE%"
)

if not exist "%INFILE%" (
  echo [ERROR] Input file not found:
  echo   "%INFILE%"
  echo Usage: run-doc-api-gen.bat [path	oCServer.java]
  exit /b 1
)

echo [apigen] Input : "%INFILE%"
echo [apigen] Output: "%OUTJSON%"
"%GEN%" -i "%INFILE%" -o "%OUTJSON%"
if errorlevel 1 (
  echo [apigen] Generation failed.
  exit /b 1
)

set "DEPLOYZIP=%ROOT%output\xqual-api-doc-deploy.zip"
echo.
echo [apigen] Packing deploy kit...
powershell -NoProfile -Command "Compress-Archive -Force -Path '%HTMLDOC%','%OUTJSON%' -DestinationPath '%DEPLOYZIP%'"
if errorlevel 1 (
  echo [apigen] WARNING: could not create deploy zip. The two files in 'output' are still usable.
) else (
  echo [apigen] Deploy kit: "%DEPLOYZIP%"
)

echo.
echo ============================================================
echo  Documentation ready. To deploy:
echo.
echo    1. Upload xqual-api-doc-deploy.zip to your web server
echo    2. Unzip it inside a folder served by Tomcat / nginx / Apache
echo       (typically: $TOMCAT/webapps/xqual/api-doc/)
echo    3. Open integrated-xqual-doc.html in your browser
echo.
echo  Both files MUST stay in the same folder (HTML loads JSON
echo  via a relative fetch). See README.html for Tomcat, nginx,
echo  Apache and CORS setup.
echo ============================================================
endlocal
pause
