#!/bin/bash
# install-fastrouter.sh
# Configures FastRouter as a custom provider in Hermes Agent
# and sets fastrouter/auto as the default model
#
# Usage: ./install-fastrouter.sh <YOUR_FASTROUTER_API_KEY>

set -e

if [ -z "$1" ]; then
  echo "Usage: $0 <fastrouter-api-key>"
  echo "Example: $0 sk-v1-...3d16"
  exit 1
fi

API_KEY="$1"

# Guard against redacted / empty keys (writing [REDACTED] silently
# breaks the provider with auth errors that look unrelated).
case "$API_KEY" in
  *REDACTED*|*redacted*|\*\*\*|"")
    echo "❌ API key looks redacted or empty — aborting."
    echo "   If Hermes' secret redaction is on, run the hermes config"
    echo "   set commands directly in your shell so the key bypasses it."
    exit 1
    ;;
esac

echo "🔧 Configuring FastRouter in Hermes..."

# Set up the custom provider
hermes config set providers.fastrouter.base_url https://api.fastrouter.ai/api/v1
hermes config set providers.fastrouter.api_key "$API_KEY"

# Make fastrouter/auto the default model.
# NOTE: custom providers are always referenced as "custom:<name>" —
# both from --provider flags and from model.provider in config.
hermes config set model.provider custom:fastrouter
hermes config set model.default fastrouter/auto

# Enable cost display
hermes config set display.show_cost true

# Verify the provider block was written correctly
echo ""
echo "🔍 Verifying configuration..."
hermes config | grep -A 3 fastrouter || {
  echo "⚠️  Could not find fastrouter block in config — please check manually."
}

echo ""
echo "✅ FastRouter installed and configured!"
echo "   Provider: custom:fastrouter"
echo "   Default model: fastrouter/auto"
echo "   Base URL: https://api.fastrouter.ai/api/v1"
echo "   Key: ${API_KEY:0:8}...${API_KEY: -4}"
echo ""
echo -e "\033[1m⚠️  Note: if you're already in an active hermes chat session when you run this,\033[0m"
echo -e "\033[1m   you'll need to /reset or restart for the new default to take effect.\033[0m"
echo ""
echo "You can now start chatting with:"
echo "  hermes chat"
echo ""
echo "Or explicitly use other models:"
echo "  hermes chat --provider custom:fastrouter -m <model>"
echo ""
echo "Check your spend at: https://fastrouter.ai"
