import time

from google import genai
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
    OTLPSpanExporter,  # type: ignore[import-not-found]
)
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

from openinference.instrumentation.google_genai import GoogleGenAIInstrumentor

endpoint = "http://localhost:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
GoogleGenAIInstrumentor().instrument(tracer_provider=tracer_provider)
tracer = tracer_provider.get_tracer(__name__)

# Make sure to set the GEMINI_API_KEY environment variable


async def run():
    client = genai.Client().aio

    initial_interaction = await client.interactions.create(
        input="Research the history of the Google TPUs with a focus on 2025 and 2026.",
        agent="deep-research-pro-preview-12-2025",
        background=True,
    )
    with tracer.start_as_current_span("google_genai.interactions.poll") as poll_span:
        while True:
            interaction = await client.interactions.get(initial_interaction.id)
            poll_span.set_attribute("google_genai.interaction.status", interaction.status)
            print(f"Status: {interaction.status}")

            if interaction.status == "completed":
                print("\nFinal Report:\n", interaction.steps[-1].content[0].text)
                break
            elif interaction.status in ["failed", "cancelled"]:
                print(f"Failed with status: {interaction.status}")
                break


if __name__ == "__main__":
    client = genai.Client()

    initial_interaction = client.interactions.create(
        input="Research the history of the Google TPUs with a focus on 2025 and 2026.",
        agent="deep-research-pro-preview-12-2025",
        background=True,
    )
    with tracer.start_as_current_span("google_genai.interactions.poll") as poll_span:
        while True:
            interaction = client.interactions.get(initial_interaction.id)
            poll_span.set_attribute("google_genai.interaction.status", interaction.status)
            print(f"Status: {interaction.status}")

            if interaction.status == "completed":
                print("\nFinal Report:\n", interaction.steps[-1].content[0].text)
                break
            elif interaction.status in ["failed", "cancelled"]:
                print(f"Failed with status: {interaction.status}")
                break
            time.sleep(10)
