#!/bin/bash

set -e

# Parse command line arguments
JS_ONLY=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --help|-h)
            echo "Usage: bash lint [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --help   Show this help message"
            echo "  --js     Run JavaScript linting only"
            echo ""
            echo "This script runs comprehensive linting for Python, C++, and JavaScript."
            echo "JavaScript linting: FAST ONLY (skips if not available)."
            exit 0
            ;;
        --js)
            JS_ONLY=true
            shift
            ;;
        *)
            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"
# Remove Visual Studio project files if they exist
find tests/ -name "*.vcxproj" -exec rm {} \; 2>/dev/null || true
find tests/ -name "*.vcxproj.filters" -exec rm {} \; 2>/dev/null || true
find tests/ -name "*.sln" -exec rm {} \; 2>/dev/null || true

if [ "$JS_ONLY" = true ]; then
    echo "🌐 Running JavaScript Linting Only"
    echo "==================================="
else
    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
fi

# 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 "ci/lint-js-fast" ] && [ -f "$ESLINT_EXE" ]; then
    echo -e "${BLUE}🚀 Using fast JavaScript linting (Node.js + ESLint)${NC}"
    if ! bash ci/lint-js-fast; then
        echo -e "${RED}❌ Fast JavaScript linting failed${NC}"
        exit 1
    fi
else
    echo -e "${BLUE}⚠️  Fast JavaScript linting not available. Setting up now...${NC}"
    # Auto-setup JavaScript linting
    if uv run ci/setup-js-linting-fast.py; then
        echo -e "${GREEN}✅ JavaScript linting setup complete${NC}"
        # Now run the linting
        if [ -f "ci/lint-js-fast" ]; then
            echo -e "${BLUE}🚀 Running JavaScript linting...${NC}"
            if ! bash ci/lint-js-fast; then
                echo -e "${RED}❌ JavaScript linting failed${NC}"
                exit 1
            fi
        else
            echo -e "${RED}❌ Failed to create lint script${NC}"
            exit 1
        fi
    else
        echo -e "${RED}❌ Failed to setup JavaScript linting${NC}"
        echo -e "${BLUE}💡 You can manually run: uv run ci/setup-js-linting-fast.py${NC}"
        # Don't exit with error, just skip JS linting
    fi
fi

echo ""
if [ "$JS_ONLY" = true ]; then
    echo "🎉 JavaScript linting completed!"
else
    echo "🎉 All linting completed!"
fi
echo "========================="
echo ""
echo "💡 FOR AI AGENTS:"
echo "  - Use 'bash lint' for comprehensive linting (Python, C++, and JavaScript)"
echo "  - Use 'bash lint --js' for JavaScript linting only"
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"
