"""tool_calling_demo.py — print schema, one agent question"""

from dotenv import load_dotenv
from langchain.agents import create_agent
from langchain.tools import tool

load_dotenv()

TAGS = {
    "a": "Creates a hyperlink. Use href for the URL.",
    "title": "Sets the browser tab title.",
    "h1": "Main heading on the page.",
}


@tool
def lookup_html_tag(tag_name: str) -> str:
    """Look up a short description of an HTML tag by name."""
    key = tag_name.strip().lower().lstrip("<").rstrip(">")
    if key in TAGS:
        return f"<{key}>: {TAGS[key]}"
    return f"No entry for '{tag_name}'."


print(f"Tool name: {lookup_html_tag.name}")
print(f"Description: {lookup_html_tag.description}")
print(f"Args schema: {lookup_html_tag.args}\n")

agent = create_agent(model="openai:gpt-4o-mini", tools=[lookup_html_tag])

question = "What does the <a> tag do? Use lookup_html_tag."
print(f"Question: {question}\n")

result = agent.invoke({"messages": [{"role": "user", "content": question}]})

for msg in result["messages"]:
    if getattr(msg, "type", None) == "tool":
        print(f"Tool returned: {msg.content}\n")

print(f"Answer: {result['messages'][-1].content}")