@echo off
setlocal EnableDelayedExpansion
title AVM INSTALLER - STRICT COMPATIBILITY CHECK

:: Enable ANSI Escapes
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "ESC=%%b"

:: Color Palette
set "C_RESET=%ESC%[0m"
set "C_BOLD=%ESC%[1m"
set "C_CYAN=%ESC%[36m"
set "C_BLUE=%ESC%[34m"
set "C_GREEN=%ESC%[32m"
set "C_RED=%ESC%[31m"
set "C_YELLOW=%ESC%[33m"
set "C_GRAY=%ESC%[90m"
set "C_WHITE=%ESC%[97m"

cd /d "%~dp0"

:: --- CONFIGURATION ---
set "NODE_REQ=v22.11.0"
set "NODE_URL=https://nodejs.org/dist/v22.11.0/node-v22.11.0-x64.msi"
set "NODE_INSTALLER=node-v22.11.0-x64.msi"

set "PYTHON_REQ=3.10.0"
set "PYTHON_URL=https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe"
set "PYTHON_INSTALLER=python-3.10.0-amd64.exe"

set "NODE_GOOD=0"
set "PYTHON_GOOD=0"
set "NODE_STATUS=UNKNOWN"
set "PYTHON_STATUS=UNKNOWN"

:INIT
cls
echo %C_GRAY%                                                            
echo           AVM INSTALLER ^& ENVIRONMENT FIX v2.1
echo %C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%
echo  [%C_BLUE%i%C_RESET%] Checking system environment...

:: --- 1. CHECK NODE.JS ---
node -v >nul 2>&1
if %errorlevel% neq 0 (
    set "NODE_STATUS=%C_RED%NOT INSTALLED%C_RESET%"
) else (
    for /f %%i in ('node -v') do set CURRENT_NODE=%%i
    if "!CURRENT_NODE!"=="%NODE_REQ%" (
        set "NODE_STATUS=%C_GREEN%OK [!CURRENT_NODE!]%C_RESET%"
        set "NODE_GOOD=1"
    ) else (
        set "NODE_STATUS=%C_RED%MISMATCH [!CURRENT_NODE!]%C_RESET%"
    )
)

:: --- 2. CHECK PYTHON ---
python --version >nul 2>&1
if %errorlevel% neq 0 (
    set "PYTHON_STATUS=%C_RED%NOT INSTALLED%C_RESET%"
) else (
    for /f "tokens=2" %%i in ('python --version 2^>^&1') do set CURRENT_PYTHON=%%i
    if "!CURRENT_PYTHON!"=="%PYTHON_REQ%" (
        set "PYTHON_STATUS=%C_GREEN%OK [!CURRENT_PYTHON!]%C_RESET%"
        set "PYTHON_GOOD=1"
    ) else (
        set "PYTHON_STATUS=%C_RED%MISMATCH [!CURRENT_PYTHON!]%C_RESET%"
    )
)

:: --- DISPLAY STATUS TABLE ---
echo.
echo   COMPONENT      REQUIRED        CURRENT STATUS
echo   %C_BLUE%-------------  --------------  ---------------------------------%C_RESET%

echo   Node.js        %NODE_REQ%        !NODE_STATUS!
echo   Python         %PYTHON_REQ%          !PYTHON_STATUS!
echo   %C_BLUE%-------------  --------------  ---------------------------------%C_RESET%
echo.

:: --- DECISION LOGIC ---
if "!NODE_GOOD!"=="1" if "!PYTHON_GOOD!"=="1" (
    echo.
    echo  [%C_GREEN%V%C_RESET%] %C_BOLD%SUCCESS:%C_RESET% All system requirements are met.
    echo  %C_GRAY%------------------------------------------------------------%C_RESET%
    echo.
    echo  [%C_BLUE%i%C_RESET%] Proceeding with installation...
    timeout /t 2 >nul
    goto :INSTALL_PROJECT
)

:: --- USER CONSENT PROMPT ---
echo  [%C_YELLOW%*%C_RESET%] %C_BOLD%WARNING:%C_RESET% Incompatible versions detected!
echo.
echo  The Installer can perform an %C_CYAN%AUTO-FIX%C_RESET%:
echo   1. %C_RED%UNINSTALL%C_RESET% old versions (Silently)
echo   2. %C_GREEN%INSTALL%C_RESET% correct versions (Silently)
echo.
set /p "USER_CONSENT=   %C_BOLD%Proceed with AUTO-FIX? (Y/N):%C_RESET% "

if /i not "!USER_CONSENT!"=="Y" (
    echo.
    echo  [%C_RED%X%C_RESET%] %C_BOLD%ABORTED:%C_RESET% No changes were made.
    pause
    exit /b
)

:: --- AUTO-REMEDIATION START ---
cls
echo.
echo  %C_BOLD%STEP 1: Cleaning Environment%C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%

if "!NODE_GOOD!"=="0" (
    echo  [%C_YELLOW%*%C_RESET%] Removing old Node.js versions...
    wmic product where "Name like 'Node.js%%'" call uninstall /nointeractive >nul 2>&1
    echo  [%C_GREEN%V%C_RESET%] Node.js cleanup complete.
)

if "!PYTHON_GOOD!"=="0" (
    echo  [%C_YELLOW%*%C_RESET%] Removing old Python versions...
    wmic product where "Name like 'Python 3%%'" call uninstall /nointeractive >nul 2>&1
    echo  [%C_GREEN%V%C_RESET%] Python cleanup complete.
)

echo.
echo  %C_BOLD%STEP 2: Installing Dependencies%C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%

if "!NODE_GOOD!"=="0" (
    echo  [%C_BLUE%i%C_RESET%] Downloading Node.js %C_WHITE%%NODE_REQ%%C_RESET%...
    powershell -Command "Invoke-WebRequest -Uri '%NODE_URL%' -OutFile '%NODE_INSTALLER%'"
    
    if exist "%NODE_INSTALLER%" (
        echo  [%C_YELLOW%*%C_RESET%] Installing Node.js silently...
        start /wait msiexec /i %NODE_INSTALLER% /qn /norestart
        echo  [%C_GREEN%V%C_RESET%] Node.js installed successfully.
    ) else (
        echo  [%C_RED%X%C_RESET%] %C_BOLD%ERROR:%C_RESET% Failed to download Node.js installer.
    )
)

if "!PYTHON_GOOD!"=="0" (
    echo  [%C_BLUE%i%C_RESET%] Downloading Python %C_WHITE%%PYTHON_REQ%%C_RESET%...
    powershell -Command "Invoke-WebRequest -Uri '%PYTHON_URL%' -OutFile '%PYTHON_INSTALLER%'"
    
    if exist "%PYTHON_INSTALLER%" (
        echo  [%C_YELLOW%*%C_RESET%] Installing Python silently (All Users + PATH)...
        start /wait %PYTHON_INSTALLER% /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
        echo  [%C_GREEN%V%C_RESET%] Python installed successfully.
    ) else (
        echo  [%C_RED%X%C_RESET%] %C_BOLD%ERROR:%C_RESET% Failed to download Python installer.
    )
)

echo.
echo  %C_GRAY%------------------------------------------------------------%C_RESET%
echo   [%C_GREEN%V%C_RESET%] %C_BOLD%REPAIR COMPLETE%C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%
echo.
echo  [%C_BLUE%i%C_RESET%] Please restart the script to verify changes and continue installation.
pause
exit /b

:INSTALL_PROJECT
echo.
echo  %C_BOLD%STEP 3: Installing Project Dependencies%C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%

set "CURRENT_DIR=%~dp0"

:: 1. STUB
if exist "%CURRENT_DIR%stub" (
    echo  [%C_BLUE%i%C_RESET%] Installing STUB dependencies...
    cd /d "%CURRENT_DIR%stub"
    call npm i
)

:: 2. GUI
if exist "%CURRENT_DIR%gui" (
    echo  [%C_BLUE%i%C_RESET%] Installing GUI dependencies...
    cd /d "%CURRENT_DIR%gui"
    call npm install --save-dev electron-builder
)

:: 3. BUILD
if exist "%CURRENT_DIR%build" (
    echo  [%C_BLUE%i%C_RESET%] Installing BUILD dependencies...
    cd /d "%CURRENT_DIR%build"
    call npm install pkg --g
    call npm i
)

echo.
echo  %C_GRAY%------------------------------------------------------------%C_RESET%
echo   [%C_GREEN%V%C_RESET%] %C_BOLD%INSTALLATION COMPLETE%C_RESET%
echo  %C_GRAY%------------------------------------------------------------%C_RESET%
pause
