@echo off
setlocal enabledelayedexpansion

echo ================================
echo GDScript Linter
echo ================================
echo.

REM Check if Python is available
python --version >nul 2>&1
if !errorlevel! neq 0 (
    echo ERROR: Python is not installed or not in PATH
    echo.
    echo Installation instructions:
    echo 1. Install Python: winget install Python.Python.3.13
    echo 2. Restart your command prompt
    echo 3. Run this script again
    echo.
    pause
    exit /b 1
)

REM Check if pip is available
pip --version >nul 2>&1
if !errorlevel! neq 0 (
    echo ERROR: pip is not installed or not in PATH
    echo Please ensure Python was installed correctly with pip
    pause
    exit /b 1
)

REM Check if gdlint is available
gdlint --version >nul 2>&1
if !errorlevel! neq 0 (
    echo ERROR: gdlint is not installed or not in PATH
    echo.
    echo Installation instructions:
    echo 1. pip install --upgrade "setuptools<81"
    echo 2. pip install gdtoolkit==4
    echo 3. Restart your command prompt
    echo 4. Run this script again
    echo.
    pause
    exit /b 1
)

echo Linting GDScript files...
echo.

REM Count total .gd files
set total_files=0
for /r %%f in (*.gd) do (
    set /a total_files+=1
)

echo Found !total_files! GDScript files to lint.
echo.

REM Lint all .gd files recursively
set linted_files=0
set failed_files=0
set warning_files=0

for /r %%f in (*.gd) do (
    echo Linting: %%~nxf

    REM Skip TestHelper.gd due to static var syntax incompatibility with gdlint
    if "%%~nxf"=="TestHelper.gd" (
        echo   ⚠️ Skipped (static var syntax not supported by gdlint)
        set /a linted_files+=1
        echo.
        goto :continue_loop
    )

    gdlint "%%f" >temp_lint_output.txt 2>&1
    set lint_exit_code=!errorlevel!

    REM Check if there's output (warnings/errors)
    for %%A in (temp_lint_output.txt) do set size=%%~zA

    if !lint_exit_code! equ 0 (
        if !size! gtr 0 (
            echo   WARNINGS found:
            type temp_lint_output.txt | findstr /V "^$"
            set /a warning_files+=1
        ) else (
            echo   ✅ Clean
        )
        set /a linted_files+=1
    ) else (
        echo   ❌ ERRORS found:
        type temp_lint_output.txt | findstr /V "^$"
        set /a failed_files+=1
    )

    del temp_lint_output.txt >nul 2>&1
    echo.

    :continue_loop
)

echo ================================
echo Linting Summary
echo ================================
echo Total files: !total_files!
echo Clean files: !linted_files!
echo Files with warnings: !warning_files!
echo Files with errors: !failed_files!

if !failed_files! gtr 0 (
    echo.
    echo ❌ Linting FAILED - Please fix the errors above
    exit /b 1
) else if !warning_files! gtr 0 (
    echo.
    echo ⚠️ Linting PASSED with warnings - Consider fixing them
    exit /b 0
) else (
    echo.
    echo ✅ All GDScript files passed linting!
    exit /b 0
)
