#!/bin/bash

set -e

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --help|-h)
            echo "Usage: bash lint [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --help   Show this help message"
            echo ""
            echo "This script runs comprehensive linting for Python, C++, and JavaScript."
            echo "JavaScript linting: FAST ONLY (skips if not available)."
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

# Unset VIRTUAL_ENV to avoid warnings about mismatched paths
unset VIRTUAL_ENV

echo "Linting tests/ for temporary cmake files"
set +e
rm -r tests/*.vcxproj
rm -r tests/*.vcxproj.filters
rm -r tests/*.sln
set -e

echo "🚀 Running FastLED Comprehensive Linting Suite"
echo "=============================================="

# Python linting
echo ""
echo "📝 PYTHON LINTING"
echo "------------------"
echo "Running ruff check (linting)"
uv run ruff check --fix test.py
uv run ruff check --fix ci --exclude ci/tmp/ --exclude ci/wasm/
echo "Running ruff format (formatting + import sorting)"
uv run ruff format test.py
uv run ruff format ci --exclude ci/tmp/ --exclude ci/wasm/
echo "Running pyright (all configured files)"
uv run pyright

export CLANG_FORMAT_STYLE="{SortIncludes: false}"

# C++ linting
echo ""
echo "🔧 C++ LINTING"
echo "---------------"
folders=(
    #"src/lib8tion"
    #"src/platforms/stub"
    #"src/platforms/apollo3"  # clang-format breaks apollo3
    #"src/platforms/esp/8266"  # clang-format breaks esp8266
    #"src/platforms/arm" # clang-format breaks arm
    #"src/fx"
    #"src/fl"
    #"src/platforms/wasm"
)

for folder in "${folders[@]}"; do
  echo "Running clang-format on $folder"
  uv run ci/run-clang-format.py -i -r "$folder" || uv run ci/run-clang-format.py -i -r "$folder"
done

# JavaScript linting and enhanced checking (now included by default)
echo ""
echo "🌐 JAVASCRIPT LINTING & TYPE CHECKING"
echo "--------------------------------------"

# Colors for JavaScript linting output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Check if fast linting is available
ESLINT_EXE=""
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then
    ESLINT_EXE=".cache/js-tools/node_modules/.bin/eslint.cmd"
else
    ESLINT_EXE=".cache/js-tools/node_modules/.bin/eslint"
fi

if [ -f ".cache/js-tools/lint-js-fast" ] && [ -f "$ESLINT_EXE" ]; then
    echo -e "${BLUE}🚀 Using fast JavaScript linting (Node.js + ESLint)${NC}"
    if ! bash .cache/js-tools/lint-js-fast; then
        echo -e "${RED}❌ Fast JavaScript linting failed${NC}"
        exit 1
    fi
else
    echo -e "${BLUE}⚠️  Fast JavaScript linting not available. Skipping JavaScript linting.${NC}"
    echo -e "${BLUE}💡 To enable fast JavaScript linting, run: uv run ci/setup-js-linting-fast.py${NC}"
fi

echo ""
echo "🎉 All linting completed!"
echo "========================="
echo ""
echo "💡 FOR AI AGENTS:"
echo "  - Use 'bash lint' for comprehensive linting (Python, C++, and JavaScript)"
echo "  - JavaScript linting: FAST ONLY (no slow fallback)"
echo "  - To enable fast JavaScript linting: uv run ci/setup-js-linting-fast.py"
echo "  - Use 'bash lint --help' for usage information"
