@echo off
title Class Generator - Diagnostic System
cd /d "%~dp0"
setlocal enabledelayedexpansion

echo.
echo ========================================
echo   CLASS GENERATOR - DIAGNOSTIC
echo ========================================
echo.
echo Running pre-flight checks...
echo.

set ERROR_COUNT=0
set WARNING_COUNT=0

:: ============================================
:: 1. SYSTEM CHECKS
:: ============================================
echo [CHECK 1/8] Windows Version...
ver | findstr /i "10.0" >nul
if %errorlevel% equ 0 (
    echo    [OK] Windows 10 detected
) else (
    echo    [WARNING] Not Windows 10, but might work
    set /a WARNING_COUNT+=1
)

:: ============================================
:: 2. PORTABLE NODE.JS
:: ============================================
echo [CHECK 2/8] Portable Node.js...
if exist "nodejs-portable\node.exe" (
    echo    [OK] Node.exe found

    :: Test if it runs
    "nodejs-portable\node.exe" --version >nul 2>&1
    if !errorlevel! equ 0 (
        for /f "tokens=*" %%v in ('"nodejs-portable\node.exe" --version') do set NODE_VERSION=%%v
        echo    [OK] Node.js !NODE_VERSION! working
    ) else (
        echo    [ERROR] Node.exe exists but doesn't run
        echo           Possible causes:
        echo           - Antivirus blocking execution
        echo           - Missing Visual C++ Runtime
        echo           - Corrupted download
        set /a ERROR_COUNT+=1
    )
) else (
    echo    [ERROR] nodejs-portable\node.exe NOT FOUND
    echo           Please run PORTABLE_SETUP.txt instructions
    set /a ERROR_COUNT+=1
)

:: ============================================
:: 3. NPM (inside portable Node)
:: ============================================
echo [CHECK 3/8] NPM availability...
if exist "nodejs-portable\node_modules\npm\bin\npm-cli.js" (
    echo    [OK] NPM found in portable Node
) else (
    echo    [ERROR] NPM not found in portable package
    echo           Re-download Node.js portable with npm included
    set /a ERROR_COUNT+=1
)

:: ============================================
:: 4. PROJECT FILES
:: ============================================
echo [CHECK 4/8] Critical project files...
set MISSING_FILES=0

if not exist "server.js" (
    echo    [ERROR] server.js missing
    set /a MISSING_FILES+=1
)
if not exist "src\Application.js" (
    echo    [ERROR] src\Application.js missing
    set /a MISSING_FILES+=1
)
if not exist "index.html" (
    echo    [ERROR] index.html missing
    set /a MISSING_FILES+=1
)

if !MISSING_FILES! equ 0 (
    echo    [OK] All critical files present
) else (
    echo    [ERROR] !MISSING_FILES! critical files missing
    set /a ERROR_COUNT+=!MISSING_FILES!
)

:: ============================================
:: 5. PORT 8080 AVAILABILITY
:: ============================================
echo [CHECK 5/8] Port 8080 availability...
netstat -ano | findstr :8080 | findstr LISTENING >nul
if %errorlevel% equ 0 (
    echo    [WARNING] Port 8080 is currently in use
    echo              Will attempt to free it on startup
    set /a WARNING_COUNT+=1

    :: Find what's using it
    for /f "tokens=5" %%a in ('netstat -ano ^| findstr :8080 ^| findstr LISTENING') do (
        set PID=%%a
        echo              Process ID: !PID!
        for /f "tokens=1" %%b in ('tasklist /fi "pid eq !PID!" /fo table /nh') do (
            echo              Program: %%b
        )
    )
) else (
    echo    [OK] Port 8080 is available
)

:: ============================================
:: 6. WRITE PERMISSIONS
:: ============================================
echo [CHECK 6/8] Write permissions...
echo test > test_write_permission.tmp 2>nul
if exist test_write_permission.tmp (
    del test_write_permission.tmp >nul 2>&1
    echo    [OK] Can write to directory
) else (
    echo    [ERROR] No write permission in current directory
    echo           Try running as Administrator
    echo           Or move folder to Documents/Desktop
    set /a ERROR_COUNT+=1
)

:: ============================================
:: 7. FIREWALL CHECK (informational)
:: ============================================
echo [CHECK 7/8] Firewall status...
netsh advfirewall show currentprofile state | findstr ON >nul
if %errorlevel% equ 0 (
    echo    [INFO] Windows Firewall is ON
    echo           You may need to allow Node.js on first run
) else (
    echo    [INFO] Windows Firewall is OFF
)

:: ============================================
:: 8. FIREFOX AVAILABILITY (CRITICAL)
:: ============================================
echo [CHECK 8/9] Firefox availability...
set FIREFOX_AVAILABLE=0

:: Check common Firefox locations
if exist "C:\Program Files\Mozilla Firefox\firefox.exe" (
    echo    [OK] Firefox found at: C:\Program Files\Mozilla Firefox\
    set FIREFOX_AVAILABLE=1
)

if !FIREFOX_AVAILABLE! equ 0 (
    if exist "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" (
        echo    [OK] Firefox found at: C:\Program Files ^(x86^)\Mozilla Firefox\
        set FIREFOX_AVAILABLE=1
    )
)

if !FIREFOX_AVAILABLE! equ 0 (
    where firefox >nul 2>&1
    if !errorlevel! equ 0 (
        echo    [OK] Firefox found in PATH
        set FIREFOX_AVAILABLE=1
    )
)

if !FIREFOX_AVAILABLE! equ 0 (
    echo    [ERROR] Firefox NOT FOUND
    echo           Firefox is REQUIRED - Edge/Chrome do not work
    echo.
    echo           SOLUTIONS:
    echo           1. Install Firefox from mozilla.org
    echo           2. Use Firefox Portable ^(no install needed^)
    echo           3. Ask IT to install Firefox
    set /a ERROR_COUNT+=1
)

:: ============================================
:: 9. ANTIVIRUS HEURISTIC CHECK
:: ============================================
echo [CHECK 9/9] Antivirus interference check...
:: Try to create and execute a simple .bat file
echo @echo ok > test_exec.bat 2>nul
if exist test_exec.bat (
    call test_exec.bat >nul 2>&1
    if !errorlevel! equ 0 (
        echo    [OK] No obvious script blocking
        del test_exec.bat >nul 2>&1
    ) else (
        echo    [WARNING] Script execution might be blocked
        echo              Check antivirus settings
        set /a WARNING_COUNT+=1
        del test_exec.bat >nul 2>&1
    )
) else (
    echo    [WARNING] Cannot create test scripts
    set /a WARNING_COUNT+=1
)

:: ============================================
:: SUMMARY
:: ============================================
echo.
echo ========================================
echo   DIAGNOSTIC SUMMARY
echo ========================================
echo.
echo   Errors:   !ERROR_COUNT!
echo   Warnings: !WARNING_COUNT!
echo.

if !ERROR_COUNT! equ 0 (
    if !WARNING_COUNT! equ 0 (
        echo [SUCCESS] System is ready to run!
        echo           You can now execute START_PORTABLE.bat
        echo.
    ) else (
        echo [CAUTION] System should work but has warnings
        echo           Proceed with START_PORTABLE.bat
        echo           Monitor for issues
        echo.
    )
) else (
    echo [FAILURE] Critical errors detected!
    echo           DO NOT run START_PORTABLE.bat yet
    echo           Fix errors above first
    echo.
)

:: ============================================
:: SAVE REPORT
:: ============================================
echo Saving diagnostic report to DIAGNOSTIC_REPORT.txt...
(
    echo CLASS GENERATOR - DIAGNOSTIC REPORT
    echo Generated: %date% %time%
    echo ========================================
    echo.
    echo Errors: !ERROR_COUNT!
    echo Warnings: !WARNING_COUNT!
    echo.
    echo For support, send this file to the developer
) > DIAGNOSTIC_REPORT.txt

echo Report saved!
echo.
pause
