#!/bin/bash
# Update Tavus Persona p7b73b74b1be with FAST MODE configuration
# This updates the existing persona with 1-min discovery and both function tools

TAVUS_API_KEY="d6e7b4726aa342b69ef4cf129cfd2016"
PERSONA_ID="p7b73b74b1be"
WEBHOOK_URL="https://slide-system-production-5813.up.railway.app/api/tavus/webhook"
echo "🚀 Updating Tavus Persona $PERSONA_ID to FAST MODE..."
echo ""

# Read FAST MODE system prompt from documentation
SYSTEM_PROMPT="You are a product demo assistant for BuzzTrail. PRIORITY: Get to slides IMMEDIATELY.

Your goal:
1. MINIMAL greeting (5-10 seconds): \"Hi! Let's dive straight into a demo.\"
2. SKIP DISCOVERY: Ask ONE question max: \"What would you like to see?\"
3. IMMEDIATELY call generate_custom_deck() with defaults/guesses
4. Present slides FAST: Auto-advance every 10-15 seconds

CRITICAL RULES:
- NO lengthy conversations before slides
- NO multiple discovery questions
- GUESS at slide content if unclear (use defaults)
- Call generate_custom_deck() within 30 seconds of conversation start
- Don't wait for perfect information - use defaults and move forward

Available functions:
- generate_custom_deck(objectives): Generate slide deck (CALL IMMEDIATELY, use defaults if needed)
- control_slide(command, slide_id?): Navigate slides (auto-advance mode)

Example flow (INTERNAL TESTING):
1. \"Hi! Let's dive straight into a demo. What would you like to see?\"
2. [Customer mentions 1-2 keywords OR says anything]
3. IMMEDIATELY call generate_custom_deck with pain_points general, interested_features overview, use_default_deck true
4. \"Here's a demo for you\"
5. Auto-advance slides every 10-15 seconds
6. Briefly narrate each slide (1-2 sentences max)
7. \"That's the demo. Any questions?\"

SPEED OPTIMIZATION:
- If customer input is vague/unclear use default deck immediately
- Don't ask clarifying questions, guess and proceed
- Slide generation should complete in 30 seconds (use pre-built deck if API slow)"

# Update persona with FAST MODE prompt and both functions
echo "📝 Updating persona with FAST MODE system prompt and functions..."
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 slide deck IMMEDIATELY - use defaults if needed for internal testing",
            "parameters": {
              "type": "object",
              "properties": {
                "pain_points": {
                  "type": "array",
                  "items": {"type": "string"},
                  "description": "Key pain points (use ['general'] if unknown)",
                  "default": ["general"]
                },
                "interested_features": {
                  "type": "array",
                  "items": {"type": "string"},
                  "description": "Features of interest (use ['overview'] if unknown)",
                  "default": ["overview"]
                },
                "use_default_deck": {
                  "type": "boolean",
                  "description": "Use pre-built default deck for speed (INTERNAL TESTING)",
                  "default": true
                }
              },
              "required": []
            },
            "callback": {
              "url": "$WEBHOOK_URL/generate-deck",
              "method": "POST",
              "timeout_ms": 90000
            }
          }
        },
        {
          "type": "function",
          "function": {
            "name": "control_slide",
            "description": "Navigate to a specific slide or move forward/backward",
            "parameters": {
              "type": "object",
              "properties": {
                "command": {
                  "type": "string",
                  "enum": ["next", "prev", "goto"],
                  "description": "Navigation command"
                },
                "slide_id": {
                  "type": "string",
                  "description": "Slide ID for 'goto' command (e.g., 'pricing', 'features')"
                }
              },
              "required": ["command"]
            },
            "callback": {
              "url": "$WEBHOOK_URL/control-slide",
              "method": "POST",
              "timeout_ms": 30000
            }
          }
        }
      ]
    }
  }
}
EOF
)"

echo ""
echo "✅ Persona updated to FAST MODE!"
echo ""
echo "📋 Verifying configuration..."
echo ""

# Verify both functions are configured
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.url, timeout: .function.callback.timeout_ms}'

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 "✅ FAST MODE Configuration complete!"
echo ""
echo "📌 What changed:"
echo "  1. ✅ System prompt: 5-10 min discovery → 1 min FAST MODE"
echo "  2. ✅ Added control_slide function"
echo "  3. ✅ Updated callback URLs for tool forwarder bot"
echo "  4. ✅ Set timeouts: 90s for deck generation, 30s for slide control"
echo ""
echo "📌 Next Steps:"
echo "1. Store persona ID in Railway: TAVUS_PERSONA_ID=p7b73b74b1be"
echo "2. Test in Tavus sandbox (verify function calls trigger)"
echo "3. Run E2E test (Task #126)"
echo ""
