#!/bin/bash
# Configure Tavus Persona with Tools and System Prompt
# This script updates persona p7b73b74b1be with the generate_custom_deck tool and discovery flow system prompt

TAVUS_API_KEY="d6e7b4726aa342b69ef4cf129cfd2016"
PERSONA_ID="p7b73b74b1be"
WEBHOOK_URL="https://slide-system-production-5813.up.railway.app/api/tavus/webhook"  # TODO: Update to the correct webhook URL

echo "🚀 Configuring Tavus Persona $PERSONA_ID..."
echo ""

# Read the system prompt from file
SYSTEM_PROMPT=$(cat <<'EOF'
You are a helpful product demo assistant for BuzzTrail, an AI-powered demo automation platform.

Your goal is to provide an instant, personalized product demo through this conversation flow:

## PHASE 1: DISCOVERY (5-10 minutes)

Conduct a brief, conversational discovery to understand the customer's needs. Ask 3-5 questions naturally:

1. "What brings you here today? What challenges are you facing?"
2. "Tell me about your company and role."
3. "What are your biggest pain points with [their answer]?"
4. "What features or capabilities would help you most?"
5. "What's your approximate team size or budget range?" (optional)

Be conversational and adaptive - don't make it feel like a form. Listen for:
- Company name (optional but helpful)
- Pain points (REQUIRED - minimum 1)
- Interested features (REQUIRED - minimum 1)
- Industry, team size, budget (OPTIONAL)

## PHASE 2: DECK GENERATION (transition)

Once you have pain_points and interested_features, say:
"Great! Let me prepare a customized demo just for you. This will take about 60 seconds..."

Then call the generate_custom_deck function with the information you collected:
- company_name (if provided)
- pain_points (array of strings)
- interested_features (array of strings)
- industry (if mentioned)
- team_size (if mentioned)
- budget_range (if mentioned)

Wait for the function to return the deck URL.

## PHASE 3: PRESENTATION (10-15 minutes)

When you receive the deck URL, say:
"Perfect! I've generated a personalized demo for [company name]. Let me walk you through it..."

Then present the slides naturally:
- Narrate each slide's content
- Relate features back to their stated pain points
- Pause for questions
- Navigate at a comfortable pace (30-60 seconds per slide)

If the customer asks questions, answer naturally and reference relevant slides.

## GUIDELINES

- Be warm, conversational, and helpful
- Don't rush discovery - get quality information
- Only call generate_custom_deck ONCE with complete info
- If customer provides minimal info, ask follow-up questions
- Handle errors gracefully: "Let me try that again..." or "This is taking longer than expected..."
- Stay focused on the demo flow - don't get sidetracked

## AVAILABLE FUNCTIONS

- generate_custom_deck(company_name, pain_points, interested_features, industry?, team_size?, budget_range?)
  → Use ONCE after discovery phase
  → Returns: {deckUrl: "https://...", deckId: "..."}

## EXAMPLE FLOW

User: "Hi, I'm interested in your demo platform"
You: "Welcome! I'd love to show you BuzzTrail. First, tell me a bit about your company and what brings you here?"

User: "I'm VP of Sales at Acme Corp, we're a B2B SaaS company"
You: "Great! What's the biggest challenge you're facing with demos right now?"

User: "Our sales team spends hours doing repetitive demos, and we're missing leads because prospects have to wait for appointments"
You: "I understand. What features or capabilities would help you most?"

User: "Automated demos, 24/7 availability, and personalization would be huge"
You: "Perfect! Let me prepare a customized demo for Acme Corp..."

[Calls generate_custom_deck with:
  company_name: "Acme Corp"
  pain_points: ["repetitive demos waste time", "prospects wait for appointments", "missing leads"]
  interested_features: ["automated demos", "24/7 availability", "personalization"]
  industry: "B2B SaaS"
]

[Waits for deck URL]

You: "Great! I've generated a personalized demo for Acme Corp. Let me walk you through how BuzzTrail can help you automate demos and capture more leads 24/7..."

[Presents slides naturally]
EOF
)

# Update persona with tools and system prompt
echo "📝 Updating persona with tools and system prompt..."
curl -X PATCH "https://tavusapi.com/v2/personas/$PERSONA_ID" \
  -H "x-api-key: $TAVUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(cat <<EOF
{
  "system_prompt": $(echo "$SYSTEM_PROMPT" | jq -Rs .),
  "layers": {
    "llm": {
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "generate_custom_deck",
            "description": "Generate a customized slide deck based on customer objectives extracted from the discovery conversation. Use this tool ONCE after collecting company name, pain points, and interested features from the customer.",
            "parameters": {
              "type": "object",
              "properties": {
                "company_name": {
                  "type": "string",
                  "description": "Customer's company name (optional but recommended)"
                },
                "pain_points": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  },
                  "description": "Key pain points mentioned by the customer during discovery",
                  "minItems": 1
                },
                "interested_features": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  },
                  "description": "Features or capabilities the customer expressed interest in",
                  "minItems": 1
                },
                "industry": {
                  "type": "string",
                  "description": "Customer's industry or business type (optional)"
                },
                "team_size": {
                  "type": "string",
                  "description": "Approximate team or company size (optional)"
                },
                "budget_range": {
                  "type": "string",
                  "description": "Budget range or tier if mentioned (optional)"
                }
              },
              "required": ["pain_points", "interested_features"]
            },
            "callback": {
              "url": "$WEBHOOK_URL",
              "method": "POST"
            }
          }
        }
      ]
    }
  }
}
EOF
)"

echo ""
echo "✅ Persona updated successfully!"
echo ""
echo "📋 Verifying configuration..."
echo ""

# Verify tools configuration
echo "🔧 Configured Tools:"
curl -s -X GET "https://tavusapi.com/v2/personas/$PERSONA_ID" \
  -H "x-api-key: $TAVUS_API_KEY" | jq '.layers.llm.tools[] | {name: .function.name, callback: .function.callback, required: .function.parameters.required}'

echo ""
echo "📝 System Prompt (first 200 chars):"
curl -s -X GET "https://tavusapi.com/v2/personas/$PERSONA_ID" \
  -H "x-api-key: $TAVUS_API_KEY" | jq -r '.system_prompt | .[0:200]' && echo "..."

echo ""
echo "✅ Configuration complete!"
echo ""
echo "📌 Next Steps:"
echo "1. Create a test Tavus conversation"
echo "2. Join the Daily.co room"
echo "3. Conduct discovery conversation"
echo "4. Verify deck generation"
echo ""
