#!/bin/bash
# Bluera Knowledge Doctor - Comprehensive Diagnostics
# Run with: /bluera-knowledge:doctor

PLUGIN_ROOT="${CLAUDE_PLUGIN_ROOT:-$(dirname "$(dirname "$0")")}"

echo "═══════════════════════════════════════════════════════════"
echo "  Bluera Knowledge Doctor"
echo "═══════════════════════════════════════════════════════════"
echo ""

ISSUES=0

# Check 1: Build tools
echo "Checking build tools..."
if command -v make &>/dev/null; then
    echo "   [OK] make found"
else
    echo "   [FAIL] make NOT found - REQUIRED for native modules"
    echo ""
    echo "   FIX: Install build tools:"
    echo "     Debian/Ubuntu: sudo apt install build-essential"
    echo "     Fedora/RHEL:   sudo dnf groupinstall 'Development Tools'"
    echo "     macOS:         xcode-select --install"
    echo ""
    ISSUES=$((ISSUES + 1))
fi

# Check 2: Node.js
echo "Checking Node.js..."
if command -v node &>/dev/null; then
    NODE_VERSION=$(node --version)
    NODE_MAJOR=$(echo "$NODE_VERSION" | cut -d. -f1 | tr -d 'v')
    if [ "$NODE_MAJOR" -ge 24 ]; then
        echo "   [WARN] Node.js $NODE_VERSION - v24+ may have native module issues"
        echo ""
        echo "   Native modules (tree-sitter, lancedb) may not compile on Node.js v24+"
        echo "   due to V8 API changes. Recommended: Use Node.js v20.x or v22.x (LTS)"
        echo ""
    else
        echo "   [OK] Node.js $NODE_VERSION"
    fi
else
    echo "   [FAIL] Node.js NOT found"
    ISSUES=$((ISSUES + 1))
fi

# Check 3: node_modules
echo "Checking plugin dependencies..."
if [ -d "$PLUGIN_ROOT/node_modules" ]; then
    echo "   [OK] node_modules installed"
else
    echo "   [FAIL] node_modules missing"
    echo ""
    echo "   FIX: Run setup manually:"
    echo "     $PLUGIN_ROOT/scripts/setup.sh"
    echo ""
    ISSUES=$((ISSUES + 1))
fi

# Check 4: MCP wrapper
WRAPPER_PATH="$HOME/.local/bin/bluera-knowledge-mcp"
echo "Checking MCP wrapper..."
if [ -f "$WRAPPER_PATH" ]; then
    echo "   [OK] MCP wrapper installed at $WRAPPER_PATH"
else
    echo "   [FAIL] MCP wrapper NOT installed"
    echo ""
    echo "   FIX: Run setup manually:"
    echo "     $PLUGIN_ROOT/scripts/setup.sh"
    echo ""
    ISSUES=$((ISSUES + 1))
fi

# Check 5: Python 3
echo "Checking Python 3..."
if command -v python3 &>/dev/null; then
    PY_VERSION=$(python3 --version 2>&1)
    echo "   [OK] $PY_VERSION"
else
    echo "   [WARN] Python 3 not found (optional, needed for embeddings)"
fi

# Check 6: Playwright
PLAYWRIGHT_PATH="${PLAYWRIGHT_BROWSERS_PATH:-$HOME/.cache/ms-playwright}"
echo "Checking Playwright browser..."
if ls "$PLAYWRIGHT_PATH"/chromium-* 1>/dev/null 2>&1; then
    echo "   [OK] Playwright Chromium installed"
else
    echo "   [WARN] Playwright Chromium not found (optional, needed for web crawling)"
    echo "   FIX: npx playwright install chromium"
fi

echo ""
echo "═══════════════════════════════════════════════════════════"
if [ $ISSUES -eq 0 ]; then
    echo "  All required checks passed!"
    echo ""
    echo "  If MCP is still failing, restart Claude Code."
else
    echo "  Found $ISSUES issue(s) - see FIX instructions above"
fi
echo "═══════════════════════════════════════════════════════════"
