"""
Test CrewAI instrumentation against local Latitude instance.

Required env vars:
- LATITUDE_API_KEY
- LATITUDE_PROJECT_SLUG
- OPENAI_API_KEY (CrewAI uses OpenAI by default)

Install: uv add crewai
"""

import os

import crewai
import openai
from crewai import Agent, Crew, Task

from latitude_telemetry import Latitude, capture

# Initialize telemetry pointing to local instance
# Note: CrewAI uses OpenAI by default, so we instrument both
latitude = Latitude(
    api_key=os.environ["LATITUDE_API_KEY"],
    project=os.environ["LATITUDE_PROJECT_SLUG"],
    instrumentations={"crewai": crewai, "openai": openai},
    disable_batch=True,
)


@capture("test-crewai-crew", {"tags": ["python", "test"], "session_id": "example"})
def test_crewai_crew():
    researcher = Agent(
        role="Researcher",
        goal="Research and summarize topics concisely",
        backstory="You are a skilled researcher who provides brief, accurate summaries.",
        verbose=False,
    )

    task = Task(
        description="Explain what OpenTelemetry is in exactly one sentence.",
        expected_output="A single sentence explanation of OpenTelemetry.",
        agent=researcher,
    )

    crew = Crew(
        agents=[researcher],
        tasks=[task],
        verbose=False,
    )

    result = crew.kickoff()
    return result.raw


if __name__ == "__main__":
    test_crewai_crew()
    latitude.flush()
