"""Lab 03 - Create, use, and version a prompt agent.

Objective 2.1 - Create and test a single-agent solution; build a client for it.

Run:  python 03_prompt_agent.py
"""
from azure.ai.projects.models import PromptAgentDefinition

from _common import banner, get_clients, require

MODEL = require("MODEL_DEPLOYMENT")
AGENT_NAME = "ai901-lab-agent"

project, _ = get_clients()

banner("Step 1 - Create an agent version")

agent = project.agents.create_version(
    agent_name=AGENT_NAME,
    definition=PromptAgentDefinition(
        model=MODEL,
        instructions=(
            "You are a rail travel assistant for Contoso Rail. "
            "Answer questions about tickets, refunds, and timetables. "
            "Be concise and use British English. "
            "If you do not know, say so and offer to escalate to a human. "
            "Never invent fares or times."
        ),
    ),
)
print(f"  id      : {agent.id}")
print(f"  name    : {agent.name}")
print(f"  version : {agent.version}")
print("\n  A prompt agent is pure configuration: model + instructions + tools.")
print("  There is no code and no container for Foundry to run.")

banner("Step 2 - Chat with the agent")

# Bind the OpenAI client to the agent. Note: no model= needed now --
# the agent's own definition supplies it.
_, openai = get_clients(agent_name=AGENT_NAME)
conversation = openai.conversations.create()

for question in (
    "Can I get a refund on an advance ticket?",
    "And what if the train was cancelled?",
    "What is the airspeed velocity of an unladen swallow?",
):
    reply = openai.responses.create(conversation=conversation.id, input=question)
    print(f"\nQ: {question}\nA: {reply.output_text}")

print("\n  ^ Turn 2 resolves context from turn 1.")
print("  ^ Turn 3 should refuse and offer escalation -- that is the")
print("    instructions doing their job as a behaviour guardrail.")

banner("Step 3 - Create a second version")

updated = project.agents.create_version(
    agent_name=AGENT_NAME,
    definition=PromptAgentDefinition(
        model=MODEL,
        instructions=(
            "You are a rail travel assistant for Contoso Rail. "
            "Answer ONLY in bullet points, maximum three bullets. "
            "British English. Never invent fares or times."
        ),
    ),
)
print(f"  New version: {updated.version}")
print("  Versions are snapshotted automatically. You can roll back or compare.")

_, openai_v2 = get_clients(agent_name=AGENT_NAME)
convo2 = openai_v2.conversations.create()
reply = openai_v2.responses.create(
    conversation=convo2.id, input="How do I claim Delay Repay?"
)
print(f"\nA: {reply.output_text}")

print("\nExam anchors:")
print("  - Prompt agent  = config only, Foundry runs it, nothing to maintain")
print("  - Hosted agent  = your code as a container, you own the logic")
print("  - Responses API direct = ephemeral agent, no agent resource at all")
print("  - Lifecycle: Create -> Test -> Trace -> Evaluate -> Optimize -> Publish -> Monitor")
print(f"\nTidy up later in the portal: Build -> Agents -> {AGENT_NAME}")
