#!/usr/bin/env bash
# Multi-Provider TypeAgent Demo
#
# This script demonstrates all four LLM providers:
# - Claude (Anthropic)
# - OpenAI (GPT)
# - Gemini (Google)
# - Ollama (Local Models)
#
# Requirements:
#   - ANTHROPIC_API_KEY for Claude
#   - OPENAI_API_KEY for OpenAI
#   - GEMINI_API_KEY or GOOGLE_API_KEY for Gemini
#   - Ollama server running (http://localhost:11434) for local models

set -e

# Colors
BOLD="\033[1m"
DIM="\033[2m"
RESET="\033[0m"
PURPLE="\033[35m"
GREEN="\033[32m"
BLUE="\033[34m"
RED="\033[31m"
YELLOW="\033[33m"

# Check if we're in the right directory (should be project root)
if [[ ! -f "Cargo.toml" ]] || [[ ! -d "crates/typedialog-agent" ]]; then
    echo -e "${RED}Error: Please run this script from the typedialog project root${RESET}"
    echo -e "${RED}Usage: ./demos/agent/run_demo.sh${RESET}"
    exit 1
fi

# Function to check API key
check_api_key() {
    local key_name=$1
    local provider=$2
    local color=$3

    if [[ -z "${!key_name}" ]]; then
        echo -e "${color}⚠️  ${provider}: ${YELLOW}${key_name} not set - skipping${RESET}"
        return 1
    else
        echo -e "${color}✓  ${provider}: ${key_name} configured${RESET}"
        return 0
    fi
}

# Banner
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}🤖  TypeAgent Multi-Provider Demo${RESET}"
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}\n"

# Check API keys and Ollama server
echo -e "${BOLD}Checking Providers:${RESET}"
CLAUDE_OK=false
OPENAI_OK=false
GEMINI_OK=false
OLLAMA_OK=false

if check_api_key "ANTHROPIC_API_KEY" "Claude  " "$PURPLE"; then
    CLAUDE_OK=true
fi

if check_api_key "OPENAI_API_KEY" "OpenAI " "$GREEN"; then
    OPENAI_OK=true
fi

if check_api_key "GEMINI_API_KEY" "Gemini " "$BLUE" || check_api_key "GOOGLE_API_KEY" "Gemini " "$BLUE"; then
    GEMINI_OK=true
fi

# Check Ollama server
OLLAMA_URL="${OLLAMA_BASE_URL:-http://localhost:11434}"
if curl -s -f "$OLLAMA_URL/api/tags" > /dev/null 2>&1; then
    echo -e "${YELLOW}✓  Ollama : Server running at $OLLAMA_URL${RESET}"
    OLLAMA_OK=true
else
    echo -e "${YELLOW}⚠️  Ollama : ${YELLOW}Server not running at $OLLAMA_URL - skipping${RESET}"
fi

echo ""

# Check if at least one provider is configured
if [[ "$CLAUDE_OK" == "false" ]] && [[ "$OPENAI_OK" == "false" ]] && [[ "$GEMINI_OK" == "false" ]] && [[ "$OLLAMA_OK" == "false" ]]; then
    echo -e "${RED}Error: No providers available. Please configure at least one:${RESET}"
    echo -e "  ${PURPLE}export ANTHROPIC_API_KEY=sk-ant-...${RESET}"
    echo -e "  ${GREEN}export OPENAI_API_KEY=sk-...${RESET}"
    echo -e "  ${BLUE}export GEMINI_API_KEY=...${RESET}"
    echo -e "  ${YELLOW}ollama serve  # Start Ollama server${RESET}"
    exit 1
fi

# Build the CLI if needed
if [[ ! -f "target/release/typedialog-ag" ]]; then
    echo -e "${BOLD}Building TypeDialog Agent CLI...${RESET}"
    cargo build --release --package typedialog-ag
    echo ""
fi

# Get theme from user
echo -e "${BOLD}Enter a theme for the haiku (default: 'autumn sunset'):${RESET}"
read -r THEME
THEME=${THEME:-"autumn sunset"}
echo ""

# Run demo for each provider
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}Running demos with theme: \"${THEME}\"${RESET}"
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}\n"

# Claude demo
if [[ "$CLAUDE_OK" == "true" ]]; then
    echo -e "${PURPLE}${BOLD}🟣 CLAUDE (claude-3-5-haiku-20241022)${RESET}"
    echo -e "${DIM}────────────────────────────────────────────────────────────────────${RESET}"
    echo "theme (String): $THEME" | cargo run --release --package typedialog-ag -- demos/agent/demo-claude.agent.mdx --yes 2>/dev/null || echo -e "${RED}Error running Claude demo${RESET}"
    echo ""
fi

# OpenAI demo
if [[ "$OPENAI_OK" == "true" ]]; then
    echo -e "${GREEN}${BOLD}🟢 OPENAI (gpt-4o-mini)${RESET}"
    echo -e "${DIM}────────────────────────────────────────────────────────────────────${RESET}"
    echo "theme (String): $THEME" | cargo run --release --package typedialog-ag -- demos/agent/demo-openai.agent.mdx --yes 2>/dev/null || echo -e "${RED}Error running OpenAI demo${RESET}"
    echo ""
fi

# Gemini demo
if [[ "$GEMINI_OK" == "true" ]]; then
    echo -e "${BLUE}${BOLD}🔵 GEMINI (gemini-2.0-flash-exp)${RESET}"
    echo -e "${DIM}────────────────────────────────────────────────────────────────────${RESET}"
    echo "theme (String): $THEME" | cargo run --release --package typedialog-ag -- demos/agent/demo-gemini.agent.mdx --yes 2>/dev/null || echo -e "${RED}Error running Gemini demo${RESET}"
    echo ""
fi

# Ollama demo
if [[ "$OLLAMA_OK" == "true" ]]; then
    echo -e "${YELLOW}${BOLD}🟡 OLLAMA (llama2)${RESET}"
    echo -e "${DIM}────────────────────────────────────────────────────────────────────${RESET}"
    echo "theme (String): $THEME" | cargo run --release --package typedialog-ag -- demos/agent/demo-ollama.agent.mdx --yes 2>/dev/null || echo -e "${RED}Error running Ollama demo${RESET}"
    echo ""
fi

# Summary
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}"
echo -e "${BOLD}📊 DEMO COMPLETE${RESET}"
echo -e "${BOLD}════════════════════════════════════════════════════════════════════${RESET}\n"

echo -e "${BOLD}All providers demonstrated:${RESET}"
[[ "$CLAUDE_OK" == "true" ]] && echo -e "  ${PURPLE}✓ Claude (Anthropic) - SSE streaming with usage tracking${RESET}"
[[ "$OPENAI_OK" == "true" ]] && echo -e "  ${GREEN}✓ OpenAI (GPT) - SSE streaming (no usage in stream)${RESET}"
[[ "$GEMINI_OK" == "true" ]] && echo -e "  ${BLUE}✓ Gemini (Google) - JSON streaming with usage tracking${RESET}"
[[ "$OLLAMA_OK" == "true" ]] && echo -e "  ${YELLOW}✓ Ollama (Local) - JSON streaming with usage tracking${RESET}"

echo -e "\n${BOLD}To run individual providers:${RESET}"
echo -e "  ${DIM}cargo run --package typedialog-ag -- demos/agent/demo-claude.agent.mdx${RESET}"
echo -e "  ${DIM}cargo run --package typedialog-ag -- demos/agent/demo-openai.agent.mdx${RESET}"
echo -e "  ${DIM}cargo run --package typedialog-ag -- demos/agent/demo-gemini.agent.mdx${RESET}"
echo -e "  ${DIM}cargo run --package typedialog-ag -- demos/agent/demo-ollama.agent.mdx${RESET}"

echo -e "\n${BOLD}To run the Rust comparison example:${RESET}"
echo -e "  ${DIM}cargo run --example provider_comparison${RESET}"
echo -e "  ${DIM}cargo run --example provider_comparison claude${RESET}"
echo -e "  ${DIM}cargo run --example provider_comparison openai${RESET}"
echo -e "  ${DIM}cargo run --example provider_comparison gemini${RESET}"
echo -e "  ${DIM}cargo run --example provider_comparison ollama${RESET}"
