@echo off
setlocal enabledelayedexpansion

echo ================================
echo GDScript Formatter
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 gdformat is available
gdformat --version >nul 2>&1
if !errorlevel! neq 0 (
    echo ERROR: gdformat 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 Formatting 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 format.
echo.

REM Format all .gd files recursively
set formatted_files=0
set failed_files=0

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

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

    gdformat "%%f"
    if !errorlevel! equ 0 (
        echo   ✅ Success
        set /a formatted_files+=1
    ) else (
        echo   ❌ FAILED: %%f
        set /a failed_files+=1
    )
    echo.

    :continue_format_loop
)

echo.
echo ================================
echo Formatting Summary
echo ================================
echo Total files: !total_files!
echo Successfully formatted: !formatted_files!
echo Failed: !failed_files!

if !failed_files! gtr 0 (
    echo.
    echo ⚠️ WARNING: Some files failed to format
    exit /b 1
) else (
    echo.
    echo ✅ All GDScript files formatted successfully!
    exit /b 0
)
