"""
AgentLens Basic Example — Simplest possible usage.

Demonstrates:
  - Initializing the SDK
  - Creating a trace with tags
  - Logging decision points (TOOL_SELECTION, PLANNING)
  - Graceful shutdown

Usage:
    pip install vectry-agentlens
    python basic_agent.py
"""

import agentlens
import time

# 1. Initialize AgentLens
agentlens.init(
    api_key="your-api-key-here",
    endpoint="http://localhost:4200",
)

# 2. Run an agent task inside a trace context
with agentlens.trace("research-task", tags=["demo", "basic"]):
    # Simulate: agent decides which tool to use for research
    agentlens.log_decision(
        type="TOOL_SELECTION",
        chosen={
            "name": "search_web",
            "confidence": 0.85,
            "params": {"query": "latest AI research papers 2025"},
        },
        alternatives=[
            {
                "name": "search_docs",
                "confidence": 0.6,
                "reason_rejected": "Internal docs unlikely to have latest papers",
            },
            {
                "name": "search_arxiv",
                "confidence": 0.78,
                "reason_rejected": "Web search covers arXiv plus other sources",
            },
        ],
        reasoning="Web search gives the broadest coverage for recent AI papers.",
    )

    time.sleep(0.3)  # Simulate tool execution time

    # Simulate: agent plans next steps after getting search results
    agentlens.log_decision(
        type="PLANNING",
        chosen={
            "name": "summarize_top_3",
            "confidence": 0.92,
            "params": {"max_papers": 3, "format": "bullet_points"},
        },
        alternatives=[
            {
                "name": "summarize_all",
                "confidence": 0.5,
                "reason_rejected": "Too many results, would dilute quality",
            },
        ],
        reasoning="Focusing on top 3 papers gives concise, high-value summary.",
    )

    time.sleep(0.2)  # Simulate summarization

    # Simulate: decide whether to retry with refined query
    agentlens.log_decision(
        type="CUSTOM",
        chosen={
            "name": "return_results",
            "confidence": 0.95,
            "params": {"result_count": 3},
        },
        alternatives=[
            {
                "name": "refine_and_retry",
                "confidence": 0.3,
                "reason_rejected": "Current results are already high quality",
            },
        ],
        reasoning="Results are comprehensive enough; no need to retry.",
    )

# 3. Shutdown — flush any pending data
agentlens.shutdown()

print("Done! Check your AgentLens dashboard for the 'research-task' trace.")
