@echo off
setlocal EnableExtensions EnableDelayedExpansion
cd /d "%~dp0"

set "SELF_CONTAINED=false"
if /I "%~1"=="--self-contained" (
    set "SELF_CONTAINED=true"
    shift
)

set "OUTPUT_ROOT=%~f1"
if "%~1"=="" set "OUTPUT_ROOT=%~dp0dist"
set "PROJECT=%~dp0Zcbot.WindowsNode\Zcbot.WindowsNode.csproj"
set "RUNTIME_ID=win-x64"

if not exist "%PROJECT%" (
    echo [ERR] Windows Node project was not found: %PROJECT%
    goto :failed
)
where.exe dotnet.exe >nul 2>&1
if errorlevel 1 (
    echo [ERR] .NET SDK was not found in PATH.
    goto :failed
)
where.exe tar.exe >nul 2>&1
if errorlevel 1 (
    echo [ERR] Windows tar.exe was not found.
    goto :failed
)
where.exe certutil.exe >nul 2>&1
if errorlevel 1 (
    echo [ERR] Windows certutil.exe was not found.
    goto :failed
)

set "VERSION="
for /f "usebackq tokens=*" %%V in (`dotnet msbuild "%PROJECT%" -nologo -getProperty:Version`) do (
    if not "%%V"=="" set "VERSION=%%V"
)
if not defined VERSION (
    echo [ERR] Could not read the Windows Node version.
    goto :failed
)

set "PACKAGE_NAME=zcbot-windows-node-!VERSION!-!RUNTIME_ID!"
set "PUBLISH_DIR=!OUTPUT_ROOT!\!PACKAGE_NAME!"
set "ARCHIVE_PATH=!OUTPUT_ROOT!\!PACKAGE_NAME!.zip"
set "HASH_PATH=!ARCHIVE_PATH!.sha256"

if not exist "!OUTPUT_ROOT!" mkdir "!OUTPUT_ROOT!"
if errorlevel 1 goto :failed
if exist "!PUBLISH_DIR!" rmdir /s /q "!PUBLISH_DIR!"
if exist "!ARCHIVE_PATH!" del /f /q "!ARCHIVE_PATH!"
if exist "!HASH_PATH!" del /f /q "!HASH_PATH!"

echo [INFO] Publishing !PACKAGE_NAME! (self-contained=!SELF_CONTAINED!)
dotnet publish "%PROJECT%" --configuration Release --runtime !RUNTIME_ID! --self-contained !SELF_CONTAINED! --output "!PUBLISH_DIR!"
if errorlevel 1 (
    echo [ERR] dotnet publish failed.
    goto :failed
)

for %%F in (
    "Zcbot.WindowsNode.exe"
) do (
    if not exist "!PUBLISH_DIR!\%%~F" (
        echo [ERR] Published package is incomplete: %%~F
        goto :failed
    )
)
set "ADAPTER_COUNT=0"
for /r "%PUBLISH_DIR%\adapters" %%F in (adapter.json) do if exist "%%~fF" set /a ADAPTER_COUNT+=1
if !ADAPTER_COUNT! LSS 1 (
    echo [ERR] Published package contains no adapters.
    goto :failed
)
set "ADAPTER_CONTRACT_ERROR=0"
for /r "%PUBLISH_DIR%\adapters" %%F in (adapter.json) do if exist "%%~fF" (
    set "ADAPTER_DIR=%%~dpF"
    set "ADAPTER_CONTRACT_COUNT=0"
    for %%C in ("!ADAPTER_DIR!*.json") do (
        if exist "%%~fC" if /I not "%%~nxC"=="adapter.json" set /a ADAPTER_CONTRACT_COUNT+=1
    )
    if !ADAPTER_CONTRACT_COUNT! LSS 1 (
        echo [ERR] Published adapter has no colocated contract: %%F
        set "ADAPTER_CONTRACT_ERROR=1"
    )
)
if !ADAPTER_CONTRACT_ERROR! NEQ 0 goto :failed

tar.exe -a -c -f "!ARCHIVE_PATH!" -C "!OUTPUT_ROOT!" "!PACKAGE_NAME!"
if errorlevel 1 (
    echo [ERR] Failed to create the ZIP package.
    goto :failed
)

set "SHA256="
for /f "tokens=*" %%H in ('certutil.exe -hashfile "!ARCHIVE_PATH!" SHA256 ^| findstr.exe /R /C:"^[0-9A-Fa-f][0-9A-Fa-f ]*[0-9A-Fa-f]$"') do (
    set "SHA256=%%H"
)
set "SHA256=!SHA256: =!"
if not defined SHA256 (
    echo [ERR] Failed to calculate the ZIP SHA-256.
    goto :failed
)
>"!HASH_PATH!" echo !SHA256!  !PACKAGE_NAME!.zip

echo [OK] Package: !ARCHIVE_PATH!
echo [OK] SHA256: !SHA256!
echo [INFO] Extract the ZIP and start Zcbot.WindowsNode.exe.
goto :finish_ok

:failed
echo.
echo [ERR] Windows Node packaging failed.
echo [INFO] Press any key to close this window.
pause >nul
exit /b 1

:finish_ok
echo.
echo [INFO] Press any key to close this window.
pause >nul
exit /b 0
