#!/usr/bin/env python3
"""
Anthropic Client Wrapper Example

Demonstrates how to use Rora's Anthropic wrapper for automatic
metrics tracking on all Anthropic API calls.

Requirements:
    pip install rora[anthropic]
"""

import os
from rora.integrations.anthropic_wrapper import RoraAnthropic, RoraAsyncAnthropic

RORA_API_KEY = os.environ.get("RORA_API_KEY", "your_rora_key")


def sync_example():
    """Synchronous Anthropic client with Rora tracking."""
    print("\n=== Sync Anthropic Example ===\n")

    client = RoraAnthropic(rora_api_key=RORA_API_KEY)

    print("Making Claude API call...")
    message = client.messages.create(
        model="claude-3-haiku-20240307",
        max_tokens=256,
        messages=[
            {"role": "user", "content": "What is the capital of France?"}
        ]
    )

    print(f"Response: {message.content[0].text}")
    print(f"\nMetrics: {client.rora_metrics}")


async def async_example():
    """Asynchronous Anthropic client with Rora tracking."""
    print("\n=== Async Anthropic Example ===\n")

    client = RoraAsyncAnthropic(rora_api_key=RORA_API_KEY)

    print("Making async Claude API call...")
    message = await client.messages.create(
        model="claude-3-haiku-20240307",
        max_tokens=256,
        messages=[
            {"role": "user", "content": "Write a one-line poem about coding."}
        ]
    )

    print(f"Response: {message.content[0].text}")
    print(f"\nMetrics: {client.rora_metrics}")


def multiple_calls_example():
    """Track metrics across multiple API calls."""
    print("\n=== Multiple Calls Example ===\n")

    client = RoraAnthropic(rora_api_key=RORA_API_KEY)

    prompts = [
        "What is 2 + 2?",
        "Name a color.",
        "Say hello in French.",
    ]

    for prompt in prompts:
        print(f"Prompt: {prompt}")
        message = client.messages.create(
            model="claude-3-haiku-20240307",
            max_tokens=64,
            messages=[{"role": "user", "content": prompt}]
        )
        print(f"Response: {message.content[0].text}\n")

    print("=== Aggregated Metrics ===")
    metrics = client.rora_metrics
    print(f"Total calls: {metrics.get('total_calls', 0)}")
    print(f"Total latency: {metrics.get('total_latency_ms', 0)}ms")
    print(f"Avg latency: {metrics.get('avg_latency_ms', 0):.0f}ms")


if __name__ == "__main__":
    import sys
    import asyncio

    if len(sys.argv) > 1 and sys.argv[1] == "async":
        asyncio.run(async_example())
    elif len(sys.argv) > 1 and sys.argv[1] == "multi":
        multiple_calls_example()
    else:
        sync_example()
