#!/bin/bash
# Fix broken Gemini model configuration

echo "🔧 Butler Intelligence Fix Script"
echo "=================================="
echo ""

# Backup current config
echo "📦 Backing up current .gemini.current.env..."
cp .gemini.current.env .gemini.current.env.backup.$(date +%Y%m%d_%H%M%S)

# Show current config
echo ""
echo "Current configuration:"
grep "GEMINI_MODEL" .gemini.current.env
echo ""

# Option 1: Try gemini-1.5-flash (fastest, most reliable for function calling)
echo "Option 1: gemini-1.5-flash (Recommended - fast, cheap, reliable tool calling)"
echo "Option 2: gemini-1.5-pro (Slower but smarter reasoning)"
echo "Option 3: Remove proxy and use direct Google API"
echo ""

read -p "Choose option (1/2/3): " choice

case $choice in
  1)
    echo "✅ Setting model to gemini-1.5-flash..."
    sed -i 's/^GEMINI_MODEL=.*/GEMINI_MODEL=gemini-1.5-flash/' .gemini.current.env
    ;;
  2)
    echo "✅ Setting model to gemini-1.5-pro..."
    sed -i 's/^GEMINI_MODEL=.*/GEMINI_MODEL=gemini-1.5-pro/' .gemini.current.env
    ;;
  3)
    echo "✅ Removing proxy (switching to direct Google API)..."
    sed -i 's/^GEMINI_BASE_URL=/#GEMINI_BASE_URL=/' .gemini.current.env
    sed -i 's/^GEMINI_MODEL=.*/GEMINI_MODEL=gemini-1.5-flash/' .gemini.current.env
    echo "⚠️  Make sure GEMINI_API_KEY is a valid Google AI API key (not proxy key)"
    ;;
  *)
    echo "❌ Invalid choice. Exiting."
    exit 1
    ;;
esac

echo ""
echo "New configuration:"
grep "GEMINI" .gemini.current.env
echo ""

# Run test
echo "🧪 Running intelligence test to verify fix..."
echo ""
python experiments/test_llm_intelligence.py

echo ""
echo "=================================="
echo "✅ Fix script complete!"
echo ""
echo "If tests still fail:"
echo "1. Check proxy logs (if using proxy)"
echo "2. Verify API key validity: https://aistudio.google.com/app/apikey"
echo "3. Try option 3 (direct Google API) if proxy is broken"
echo ""
echo "If tests pass (>80% success):"
echo "- You can now REMOVE the SAFETY OVERRIDE from dispatcher.py"
echo "- Lines 97-313 can be deleted"
echo "- Trust the LLM to handle tool calling"
