#!/bin/bash
# Proxy Switcher - 快速切换代理配置

set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_ROOT"

case "$1" in
  local)
    ln -sf .gemini.local.env .gemini.current.env
    echo "✅ 切换到本地代理"
    echo "   Base URL: http://127.0.0.1:8045"
    echo "   Model: gemini-3-pro-high"
    echo "   Tool Mode: light (6 tools)"
    echo "   ⚠️  注意: Function calling 不可用，依赖 SAFETY OVERRIDE"
    ;;

  openrouter)
    ln -sf .gemini.openrouter.env .gemini.current.env
    echo "✅ 切换到 OpenRouter"
    echo "   Base URL: https://openrouter.ai/api"
    echo "   Model: google/gemini-3-flash-preview"
    echo "   Tool Mode: all (21 tools)"
    echo "   ✅ Function calling 完全可用"
    ;;

  status)
    if [ -L .gemini.current.env ]; then
      TARGET=$(readlink .gemini.current.env)
      echo "当前配置: $TARGET"
      echo ""
      cat .gemini.current.env
    else
      echo "未找到配置链接"
    fi
    exit 0
    ;;

  *)
    echo "用法: $0 {local|openrouter|status}"
    echo ""
    echo "  local      - 切换到本地代理 (免费，功能受限)"
    echo "  openrouter - 切换到 OpenRouter (¥10-14/月，完整功能)"
    echo "  status     - 查看当前配置"
    exit 1
    ;;
esac

# 重启 Bot
echo ""
echo "正在重启 Slack Bot..."

if command -v supervisorctl &> /dev/null; then
    supervisorctl restart slack_bot
    echo "✅ Bot 已重启"
else
    echo "⚠️  请手动重启 Bot"
    echo "   如果使用 supervisor: supervisorctl restart slack_bot"
    echo "   如果手动运行: kill <pid> && python slack_bot/main.py"
fi

echo ""
echo "切换完成！"
