import { CodeBlock } from '../views/docs/CodeBlock';
import { CodeTabs } from '../views/docs/CodeTabs';
import { Callout, Prose, PropTable, GetStartedCTA } from '../views/docs/prose';

## WebRTC Widget

Embed a Guava voice agent on your website so visitors can start a voice conversation directly from the browser. The WebRTC audio widget is a drop-in `<script>` tag that handles all WebRTC signaling, UI, and state management — no additional CSS, JS, or dependencies required.

### Integration

Add a single `<script>` tag to your page. The only required attribute is `data-webrtc-code`, which is the agent code (starts with `grtc-`) obtained from the Guava dashboard.

<CodeBlock
  filename="index.html"
  language="html"
  code={`<script
  src="https://app.goguava.ai/static/build/webrtc-widgets/guava-widget-audio-orb.js"
  data-webrtc-code="grtc-YOUR_AGENT_CODE_HERE"
></script>`}
/>

### Complete standalone page

If you don't have a page yet, here's a full HTML file you can use as a starting point:

<CodeBlock
  filename="index.html"
  code={`<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My Voice Agent</title>
  </head>
  <body>
    <script
      src="https://app.goguava.ai/static/build/webrtc-widgets/guava-widget-audio-orb.js"
      data-webrtc-code="grtc-YOUR_AGENT_CODE_HERE"
    ></script>
  </body>
</html>`}
/>

### Script tag attributes

<PropTable rows={[
  { name: "src", type: "URL", desc: "URL to the hosted widget JS file." },
  { name: "data-webrtc-code", type: "string", desc: "The WebRTC agent code (e.g. grtc-nB9oE4...). Obtained from the Guava dashboard." },
]} />

### Widget states

<div className="my-4 overflow-x-auto rounded-lg border border-border">
  <table className="w-full text-sm font-mono">
    <thead>
      <tr className="border-b border-border bg-card/50">
        <th className="text-left px-4 py-3 text-muted-foreground font-semibold">State</th>
        <th className="text-left px-4 py-3 text-muted-foreground font-semibold">Behavior</th>
        <th className="text-left px-4 py-3 text-muted-foreground font-semibold">User action</th>
      </tr>
    </thead>
    <tbody>
      <tr className="bg-transparent">
        <td className="px-4 py-3 text-primary">idle</td>
        <td className="px-4 py-3 text-muted-foreground">Ready to call — green indicator</td>
        <td className="px-4 py-3 text-muted-foreground">Click to call</td>
      </tr>
      <tr className="bg-card/20">
        <td className="px-4 py-3 text-primary">connecting</td>
        <td className="px-4 py-3 text-muted-foreground">Pulsing animation, clicks disabled</td>
        <td className="px-4 py-3 text-muted-foreground">Wait</td>
      </tr>
      <tr className="bg-transparent">
        <td className="px-4 py-3 text-primary">active</td>
        <td className="px-4 py-3 text-muted-foreground">Call in progress — audio-reactive visuals</td>
        <td className="px-4 py-3 text-muted-foreground">Click to hang up</td>
      </tr>
      <tr className="bg-card/20">
        <td className="px-4 py-3 text-primary">error</td>
        <td className="px-4 py-3 text-muted-foreground">Red indicator — connection failed</td>
        <td className="px-4 py-3 text-muted-foreground">Click to retry</td>
      </tr>
    </tbody>
  </table>
</div>

<Callout>
  <span className="text-primary font-semibold">No configuration needed.</span> The widget is entirely self-contained — it injects its own HTML and CSS (scoped under `[data-guava-widget]` so it won't conflict with your page styles), wrapped in an IIFE with no global variables. Works on any page with a modern browser.
</Callout>

### Generating a WebRTC code via SDK

Instead of obtaining a WebRTC code from the Guava dashboard, you can generate one programmatically with `client.create_webrtc_agent()`. This is useful when you need to create codes on-the-fly or control their TTL.

<CodeTabs
  python={{ code: `from guava import Client\nfrom datetime import timedelta\n\nclient = Client(api_key="your-api-key")\n\n# Create a WebRTC code valid for 1 hour\nwebrtc_code = client.create_webrtc_agent(ttl=timedelta(hours=1))\nprint(f"WebRTC code: {webrtc_code}")\n\n# Use the code to listen for inbound calls\nclient.listen_inbound(webrtc_code=webrtc_code, controller_class=MyCallController)`, filename: "generate_code.py" }}
  typescript={{ code: `import * as guava from "@guava-ai/guava-sdk";\n\nconst client = new guava.Client({ apiKey: "your-api-key" });\n\n// Create a WebRTC code valid for 1 hour (3600 seconds)\nconst webrtcCode = await client.createWebrtcAgent({ ttlSec: 3600 });\nconsole.log(\`WebRTC code: \${webrtcCode}\`);\n\n// Use the code to listen for inbound calls\nclient.listenInbound(\n  { webrtc_code: webrtcCode },\n  (logger) => new MyCallController(logger),\n);`, filename: "generate_code.ts" }}
/>

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `ttl` | `datetime.timedelta \| None` | No | How long the WebRTC code should remain valid. If omitted, the server default TTL applies. |

**Returns:** `str` — a WebRTC code (e.g. `grtc-...`) that can be passed to `listen_inbound(webrtc_code=...)` or used as a `?webrtc_code=<value>` query parameter in the browser widget.

Raises an HTTP error (via `check_response`) if the API request fails.

<GetStartedCTA />
