import { CodeTabs } from '../views/docs/CodeTabs';
import { PropTable, Callout, AutoNextLink } from '../views/docs/prose';

export const SMS_EX_PY = `import guava
import os

client = guava.Client(api_key=os.environ["GUAVA_API_KEY"])

agent_number = os.environ["GUAVA_AGENT_NUMBER"]   # one of your Guava numbers
customer = "+15551234567"

# Send an SMS from your Guava number to the customer.
client.send_sms(
    from_number=agent_number,
    to_number=customer,
    message="Hi! Reply YES to confirm your appointment, or STOP to opt out.",
)

# Block until the customer replies, giving up after 5 minutes.
reply = client.next_sms(from_number=customer, to_number=agent_number, timeout=300)

if reply is None:
    print("No reply within 5 minutes.")
else:
    print("Customer replied:", reply["content"])`;

export const SMS_EX_TS = `import * as guava from "@guava-ai/guava-sdk";

const client = new guava.Client(process.env.GUAVA_API_KEY);

const agentNumber = process.env.GUAVA_AGENT_NUMBER;  // one of your Guava numbers
const customer = "+15551234567";

// Send an SMS from your Guava number to the customer.
await client.sendSms(
  agentNumber,
  customer,
  "Hi! Reply YES to confirm your appointment, or STOP to opt out.",
);

// Block until the customer replies, giving up after 5 minutes.
const reply = await client.nextSms(customer, agentNumber, { timeoutMs: 300_000 });

if (reply === null) {
  console.log("No reply within 5 minutes.");
} else {
  console.log("Customer replied:", reply.content);
}`;

export const SMS_SEND_SIG_PY = `client.send_sms(
    from_number: str,
    to_number: str,
    message: str,
) -> None`;

export const SMS_SEND_SIG_TS = `await client.sendSms(
  fromNumber: string,
  toNumber: string,
  message: string,
): Promise<void>`;

export const SMS_NEXT_SIG_PY = `client.next_sms(
    from_number: str,
    to_number: str,
    *,
    timeout: float = 60.0,
    poll_interval: float = 2.0,
) -> dict | None`;

export const SMS_NEXT_SIG_TS = `await client.nextSms(
  fromNumber: string,
  toNumber: string,
  options?: { timeoutMs?: number; pollIntervalMs?: number },
): Promise<SmsMessage | null>`;

## SMS Messaging

The [`guava.Client`](./client) can send SMS messages from your Guava numbers and wait for inbound replies. This is useful for sending confirmations and reminders, or for collecting a response between calls.

<Callout>
  SMS messaging is available in the <strong>Python and TypeScript SDKs</strong>. For other languages, call the equivalent <a href="/docs/messages-api">Messages REST API</a> directly.
</Callout>

<CodeTabs
  python={{ code: SMS_EX_PY, filename: "sms.py" }}
  typescript={{ code: SMS_EX_TS, filename: "sms.ts" }}
/>

### send_sms / sendSms

Send a single SMS message. The `from_number` must be one of your Guava numbers with SMS configured, and the message is delivered to `to_number`.

<CodeTabs
  python={{ code: SMS_SEND_SIG_PY, filename: "signature" }}
  typescript={{ code: SMS_SEND_SIG_TS, filename: "signature" }}
/>

<PropTable rows={[
  { name: "from_number", type: "str", desc: "One of your Guava numbers, in E.164 format (e.g. \"+15551230001\"). Must have SMS enabled." },
  { name: "to_number", type: "str", desc: "The recipient's number, in E.164 format." },
  { name: "message", type: "str", desc: "The message body to send." },
]} />

Returns nothing (Python `None`; TypeScript resolves `void`). Raises (Python) / rejects (TypeScript) if the `from_number` isn't owned by your organization or doesn't have SMS configured.

<Callout>
  Sending SMS requires your organization to complete SMS brand and campaign registration. See <a href="/docs/outbound-and-sms-permissions">Outbound &amp; SMS Compliance</a>.
</Callout>

### next_sms / nextSms

Wait for the next inbound SMS sent **to** one of your Guava numbers **from** a given number, and return it. `next_sms` polls your inbox and only returns messages received after the call begins, so a reply to an earlier message won't be returned twice.

<Callout>
  Note the direction: `from_number` is the external number you're waiting to hear from (the customer), and `to_number` is your Guava number that receives the reply — the opposite of <code>send_sms</code>.
</Callout>

<CodeTabs
  python={{ code: SMS_NEXT_SIG_PY, filename: "signature" }}
  typescript={{ code: SMS_NEXT_SIG_TS, filename: "signature" }}
/>

<PropTable rows={[
  { name: "from_number", type: "str", desc: "The external number you're waiting to hear from, in E.164 format." },
  { name: "to_number", type: "str", desc: "Your Guava number that will receive the reply, in E.164 format." },
  { name: "timeout", type: "float", default: "60.0", desc: "Maximum number of seconds to wait before giving up. In TypeScript, pass `timeoutMs` (milliseconds) in the options object; defaults to 60000." },
  { name: "poll_interval", type: "float", default: "2.0", desc: "Seconds to wait between inbox checks. In TypeScript, pass `pollIntervalMs` (milliseconds) in the options object; defaults to 2000." },
]} />

Returns the message (Python `dict` / TypeScript `SmsMessage`), or `None` / `null` if the timeout elapses with no new message. A message has the following fields:

<PropTable rows={[
  { name: "id", type: "str", desc: "Unique ID of the message." },
  { name: "from_number", type: "str", desc: "The number that sent the message." },
  { name: "to_number", type: "str", desc: "Your Guava number that received the message." },
  { name: "content", type: "str", desc: "The message body." },
  { name: "received_at", type: "str", desc: "When the message was received, in ISO 8601 format." },
  { name: "modality", type: "str", desc: "The channel the message arrived on. Currently always \"sms\"." },
  { name: "direction", type: "str", desc: "Always \"inbound\" for received messages." },
]} />

<AutoNextLink currentSection="messaging" />
