# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
DESCRIPTION:
    This sample demonstrates how to run basic Prompt Agent operations
    using the synchronous client. It first creates an Agent version
    and Conversation as prerequisites, then demonstrates retrieve/get
    operations against those created resources.

    For OpenAI operations in this sample, see:
    https://platform.openai.com/docs/api-reference/conversations/retrieve?lang=python
    https://platform.openai.com/docs/api-reference/conversations/create-items?lang=python

USAGE:
    python sample_agent_retrieve_basic.py

    Before running the sample:

    pip install "azure-ai-projects>=2.0.0" python-dotenv

    Set these environment variables with your own values:
    1) FOUNDRY_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
       page of your Microsoft Foundry portal.
    2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
       the "Models + endpoints" tab in your Microsoft Foundry project.
"""

import os
from dotenv import load_dotenv
from agent_retrieve_helper import create_and_retrieve_agent_and_conversation  # pylint: disable=import-error
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

load_dotenv()

endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
model = os.environ["FOUNDRY_MODEL_NAME"]

with (
    DefaultAzureCredential() as credential,
    AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
    # Creates prerequisite resources and yields (agent_name, conversation_id).
    # Then automatically deletes the created agent version when this context manager exits.
    create_and_retrieve_agent_and_conversation(project_client=project_client, model=model) as (
        agent_name,
        conversation_id,
    ),
    project_client.get_openai_client() as openai_client,
):

    # Retrieve latest version for the prerequisite agent.
    agent = project_client.agents.get(agent_name=agent_name)
    print(f"Agent retrieved (id: {agent.id}, name: {agent.name}, version: {agent.versions.latest.version})")

    # Retrieve the prerequisite conversation.
    conversation = openai_client.conversations.retrieve(conversation_id=conversation_id)
    print(f"Retrieved conversation (id: {conversation.id})")

    # Add a new user text message to the conversation
    openai_client.conversations.items.create(
        conversation_id=conversation.id,
        items=[{"type": "message", "role": "user", "content": "How many feet are in a mile?"}],
    )
    print("Added a user message to the conversation")

    response = openai_client.responses.create(
        conversation=conversation.id,
        extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
    )
    print(f"Response output: {response.output_text}")
