#!/bin/bash

# Test script for Tavus Tool Forwarder Bot
#
# This script helps test the bot locally before deployment

set -e

echo "🤖 Tavus Tool Forwarder Bot - Test Script"
echo "=========================================="
echo ""

# Check if virtual environment exists
if [ ! -d "venv" ]; then
    echo "📦 Creating virtual environment..."
    python3 -m venv venv
fi

# Activate virtual environment
echo "🔧 Activating virtual environment..."
source venv/bin/activate

# Install dependencies
echo "📥 Installing dependencies..."
pip install -q -r requirements.txt

# Check for .env file
if [ ! -f ".env" ]; then
    echo "⚠️  No .env file found. Creating from .env.example..."
    cp .env.example .env
    echo ""
    echo "⚠️  Please edit .env with your credentials:"
    echo "   - DAILY_API_KEY"
    echo "   - API_BASE_URL (optional)"
    echo ""
    exit 1
fi

# Load environment variables
export $(cat .env | grep -v '^#' | xargs)

# Check for required environment variables
if [ -z "$DAILY_API_KEY" ]; then
    echo "❌ Error: DAILY_API_KEY not set in .env"
    exit 1
fi

# Get room URL
ROOM_URL=${1:-$TAVUS_ROOM_URL}

if [ -z "$ROOM_URL" ]; then
    echo "❌ Error: Room URL required"
    echo ""
    echo "Usage:"
    echo "  ./test_bot.sh https://your-domain.daily.co/room-name"
    echo ""
    echo "Or set TAVUS_ROOM_URL in .env"
    exit 1
fi

echo ""
echo "🎬 Starting bot..."
echo "   Room: $ROOM_URL"
echo "   Backend: ${API_BASE_URL:-https://slide-system-production-5813.up.railway.app}"
echo ""

# Run the bot
python bot.py "$ROOM_URL"
