@echo off
REM PCode Windows Dependency Verification Script
REM Checks all dependencies and generates JSON result file

setlocal EnableDelayedExpansion

REM Locate shared library for JSON escaping
set "SCRIPT_DIR=%~dp0"
set "LIB_DIR=%SCRIPT_DIR%..\lib"
set "ESCAPE_FUNC=%LIB_DIR%\escape_json_string.bat"
set "VALIDATE_FUNC=%LIB_DIR%\validate_json.bat"

REM Locate dependencies.env file
REM The env file contains version requirements auto-generated from manifest.json
set "ENV_FILE=%SCRIPT_DIR%dependencies.env"

if not exist "%ENV_FILE%" (
    echo [ERROR] dependencies.env file not found at %ENV_FILE%
    echo This file is auto-generated during build. Please ensure you have a complete package.
    endlocal
    exit /b 1
)

REM Read version requirements from dependencies.env
REM Parse KEY=VALUE format from the env file
for /f "usebackq tokens=1,2 delims==" %%a in ("%ENV_FILE%") do (
    if "%%a"=="DOTNET_MIN_VERSION" set DOTNET_MIN_VERSION=%%b
    if "%%a"=="DOTNET_RECOMMENDED_VERSION" set DOTNET_RECOMMENDED_VERSION=%%b
    if "%%a"=="NODE_MIN_VERSION" set NODE_MIN_VERSION=%%b
    if "%%a"=="NODE_RECOMMENDED_VERSION" set NODE_RECOMMENDED_VERSION=%%b
    if "%%a"=="NPM_MIN_VERSION" set NPM_MIN_VERSION=%%b
    if "%%a"=="NPM_RECOMMENDED_VERSION" set NPM_RECOMMENDED_VERSION=%%b
    if "%%a"=="CLAUDE_CODE_MIN_VERSION" set CLAUDE_CODE_MIN_VERSION=%%b
    if "%%a"=="OPENSPEC_MIN_VERSION" set OPENSPEC_MIN_VERSION=%%b
    if "%%a"=="OPENSPEC_MAX_VERSION" set OPENSPEC_MAX_VERSION=%%b
)

REM Set default values in case env file is missing some variables
if "%DOTNET_MIN_VERSION%"=="" set DOTNET_MIN_VERSION=10.0
if "%DOTNET_RECOMMENDED_VERSION%"=="" set DOTNET_RECOMMENDED_VERSION=10.0
if "%NODE_MIN_VERSION%"=="" set NODE_MIN_VERSION=18.0.0
if "%NODE_RECOMMENDED_VERSION%"=="" set NODE_RECOMMENDED_VERSION=24.12.0
if "%NPM_MIN_VERSION%"=="" set NPM_MIN_VERSION=9.0.0
if "%NPM_RECOMMENDED_VERSION%"=="" set NPM_RECOMMENDED_VERSION=10.9.0
if "%CLAUDE_CODE_MIN_VERSION%"=="" set CLAUDE_CODE_MIN_VERSION=1.0.0
if "%OPENSPEC_MIN_VERSION%"=="" set OPENSPEC_MIN_VERSION=0.23.0
if "%OPENSPEC_MAX_VERSION%"=="" set OPENSPEC_MAX_VERSION=1.0.0

echo === PCode Dependency Check ===
echo.

REM Initialize status variables
set DOTNET_STATUS="status": "not_installed", "version": null, "path": null
set NODE_STATUS="status": "not_installed", "version": null, "path": null
set NPM_STATUS="status": "not_installed", "version": null, "path": null
set CLAUDE_STATUS="status": "not_installed", "version": null, "path": null
set OPENSPEC_STATUS="status": "not_installed", "version": null, "path": null

REM Check .NET
echo Checking .NET runtime...
where dotnet >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "delims=" %%i in ('dotnet --version 2^>nul') do set DOTNET_VERSION=%%i
    for /f "delims=" %%i in ('where dotnet 2^>nul') do set DOTNET_RAW_PATH=%%i
    call "%ESCAPE_FUNC%" "!DOTNET_RAW_PATH!" DOTNET_PATH
    echo [OK] .NET runtime found: !DOTNET_VERSION!
    set DOTNET_STATUS="status": "installed", "version": "!DOTNET_VERSION!", "path": "!DOTNET_PATH!"
) else (
    echo [NOT FOUND] .NET runtime not found ^(min: %DOTNET_MIN_VERSION%^)
)

REM Check Node.js
echo Checking Node.js...
where node >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "delims=" %%i in ('node --version 2^>nul') do set NODE_VERSION=%%i
    for /f "delims=" %%i in ('where node 2^>nul') do set NODE_RAW_PATH=%%i
    call "%ESCAPE_FUNC%" "!NODE_RAW_PATH!" NODE_PATH
    echo [OK] Node.js found: !NODE_VERSION!
    set NODE_STATUS="status": "installed", "version": "!NODE_VERSION!", "path": "!NODE_PATH!"
) else (
    echo [NOT FOUND] Node.js not found ^(min: v%NODE_MIN_VERSION%^)
)

REM Check NPM
echo Checking NPM...
where npm >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "delims=" %%i in ('npm --version 2^>nul') do set NPM_VERSION=%%i
    for /f "delims=" %%i in ('where npm 2^>nul') do set NPM_RAW_PATH=%%i
    call "%ESCAPE_FUNC%" "!NPM_RAW_PATH!" NPM_PATH
    echo [OK] NPM found: v!NPM_VERSION!
    set NPM_STATUS="status": "installed", "version": "v!NPM_VERSION!", "path": "!NPM_PATH!"
) else (
    echo [NOT FOUND] NPM not found ^(min: v%NPM_MIN_VERSION%^)
)

REM Check Claude Code
echo Checking Claude Code...
where claude >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "delims=" %%i in ('claude --version 2^>nul') do set CLAUDE_VERSION=%%i
    for /f "delims=" %%i in ('where claude 2^>nul') do set CLAUDE_RAW_PATH=%%i
    call "%ESCAPE_FUNC%" "!CLAUDE_RAW_PATH!" CLAUDE_PATH
    echo [OK] Claude Code found: !CLAUDE_VERSION!
    set CLAUDE_STATUS="status": "installed", "version": "!CLAUDE_VERSION!", "path": "!CLAUDE_PATH!"
) else (
    echo [WARNING] Claude Code not found ^(optional^)
)

REM Check OpenSpec
echo Checking OpenSpec...
where openspec >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    for /f "delims=" %%i in ('openspec --version 2^>nul') do set OPENSPEC_VERSION=%%i
    for /f "delims=" %%i in ('where openspec 2^>nul') do set OPENSPEC_RAW_PATH=%%i
    call "%ESCAPE_FUNC%" "!OPENSPEC_RAW_PATH!" OPENSPEC_PATH

    REM Simple version comparison: check if version is within range [min, max)
    REM Extract just the version number (remove 'v' prefix if present)
    set "VERSION_NUM=!OPENSPEC_VERSION:v=!"

    REM Use string comparison for version checking
    if "!VERSION_NUM!" geq "%OPENSPEC_MIN_VERSION%" (
        if "!VERSION_NUM!" lss "%OPENSPEC_MAX_VERSION%" (
            echo [OK] OpenSpec found: !OPENSPEC_VERSION!
            set OPENSPEC_STATUS="status": "installed", "version": "!OPENSPEC_VERSION!", "path": "!OPENSPEC_PATH!"
        ) else (
            echo [ERROR] OpenSpec version !OPENSPEC_VERSION! is too high ^(max: %OPENSPEC_MAX_VERSION%^)
            set OPENSPEC_STATUS="status": "version_mismatch", "version": "!OPENSPEC_VERSION!", "path": "!OPENSPEC_PATH!", "error": "Version must be between %OPENSPEC_MIN_VERSION% and %OPENSPEC_MAX_VERSION%"
        )
    ) else (
        echo [ERROR] OpenSpec version !OPENSPEC_VERSION! is too low ^(min: %OPENSPEC_MIN_VERSION%^)
        set OPENSPEC_STATUS="status": "version_mismatch", "version": "!OPENSPEC_VERSION!", "path": "!OPENSPEC_PATH!", "error": "Version must be at least %OPENSPEC_MIN_VERSION%"
    )
) else (
    echo [WARNING] OpenSpec not found ^(optional^)
)

REM Generate JSON result
echo.
echo [OK] Check complete. Generating result file...

REM Get current timestamp in ISO format
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set "dt=%%a"
set TIMESTAMP=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%T%dt:~8,2%:%dt:~10,2%:%dt:~12,2%Z

(
echo {
echo   "timestamp": "%TIMESTAMP%",
echo   "platform": "windows",
echo   "dependencies": {
echo     "dotnet": { !DOTNET_STATUS!, "required": true, "minVersion": "%DOTNET_MIN_VERSION%" },
echo     "node": { !NODE_STATUS!, "required": true, "minVersion": "%NODE_MIN_VERSION%" },
echo     "npm": { !NPM_STATUS!, "required": true, "minVersion": "%NPM_MIN_VERSION%" },
echo     "claude-code": { !CLAUDE_STATUS!, "required": false, "minVersion": "%CLAUDE_CODE_MIN_VERSION%" },
echo     "openspec": { !OPENSPEC_STATUS!, "required": false, "minVersion": "%OPENSPEC_MIN_VERSION%", "maxVersion": "%OPENSPEC_MAX_VERSION%" }
echo   },
echo   "summary": {
echo     "total": 5,
echo     "installed": null,
echo     "requiredInstalled": null,
echo     "ready": null
echo   }
echo }
) > check-result.json

echo.
echo Results written to: check-result.json

REM Validate generated JSON
call "%VALIDATE_FUNC%" "check-result.json"
if %ERRORLEVEL% NEQ 0 (
    echo [WARNING] Generated JSON file may have syntax errors
)

type check-result.json | findstr /C:"status" /C:"ready"

endlocal
exit /b 0
