#!/bin/bash
# Fix Tavus Tool Callbacks - Add webhook URLs to LLM tools

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 "Fixing Tavus tool callbacks for persona $PERSONA_ID..."

# Update persona with tools that include callback URLs
curl -X PATCH "https://tavusapi.com/v2/personas/$PERSONA_ID" \
  -H "x-api-key: $TAVUS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "layers": {
      "llm": {
        "tools": [
          {
            "type": "function",
            "function": {
              "name": "generate_custom_deck",
              "description": "Generate a customized slide deck based on customer discovery insights. Call this ONCE after 1-2 minutes of discovery (or immediately if prospect asks to see the demo). This creates a personalized presentation showing how BuzzTrail'\''s AI avatars solve the prospect'\''s specific challenges.",
              "parameters": {
                "type": "object",
                "properties": {
                  "company_name": {
                    "type": "string",
                    "description": "The prospect'\''s company name (e.g., '\''Acme Corp'\'')"
                  },
                  "pain_points": {
                    "type": "array",
                    "items": {"type": "string"},
                    "minItems": 1,
                    "description": "Array of specific pain points or challenges mentioned by the prospect"
                  },
                  "interested_features": {
                    "type": "array",
                    "items": {"type": "string"},
                    "minItems": 1,
                    "description": "Array of features or use cases the prospect is interested in"
                  },
                  "budget_range": {
                    "type": "string",
                    "default": "not specified",
                    "description": "Budget range if mentioned (e.g., '\''enterprise'\'', '\''startup'\'')"
                  },
                  "team_size": {
                    "type": "string",
                    "default": "not specified",
                    "description": "Team size if mentioned (e.g., '\''10-50'\'', '\''200+'\'')"
                  },
                  "industry": {
                    "type": "string",
                    "default": "B2B",
                    "description": "Industry or business type"
                  }
                },
                "required": ["company_name", "pain_points", "interested_features"]
              },
              "callback": {
                "url": "'"$WEBHOOK_URL"'",
                "method": "POST"
              }
            }
          },
          {
            "type": "function",
            "function": {
              "name": "control_slide",
              "description": "Navigate through the presentation slides. Use this to advance to the next slide, go back to a previous slide, or jump to a specific slide number. Call this during the presentation phase to control the slide deck in sync with your narration.",
              "parameters": {
                "type": "object",
                "properties": {
                  "direction": {
                    "type": "string",
                    "enum": ["next", "previous"],
                    "description": "Direction to navigate: '\''next'\'' for next slide, '\''previous'\'' for previous slide"
                  },
                  "slide_number": {
                    "type": "integer",
                    "minimum": 1,
                    "description": "Specific slide number to jump to (1-indexed)"
                  }
                },
                "oneOf": [
                  {"required": ["direction"]},
                  {"required": ["slide_number"]}
                ]
              },
              "callback": {
                "url": "'"$WEBHOOK_URL"'",
                "method": "POST"
              }
            }
          }
        ]
      }
    }
  }'

echo ""
echo "✅ Tavus persona updated with tool callbacks"
echo ""
echo "Verifying configuration..."
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}'

echo ""
echo "Done! Tools now have callback URLs configured."
echo "Create a new test conversation to verify the fix."
