"""creating_custom_tools_demo.py — two tools, custom names"""

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

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("html_tag_list")
def list_html_tags() -> str:
    """List HTML tag names available in this helper."""
    return ", ".join(f"<{name}>" for name in TAGS)


def lookup_raw(tag_name: str) -> str:
    key = tag_name.strip().lower().lstrip("<").rstrip(">")
    if key in TAGS:
        return f"<{key}>: {TAGS[key]}"
    return f"No entry for '{tag_name}'."


html_tag_lookup = StructuredTool.from_function(
    func=lookup_raw,
    name="html_tag_lookup",
    description="Look up one HTML tag by name.",
)

tools = [list_html_tags, html_tag_lookup]

print("Tools on agent:")
for t in tools:
    print(f"  {t.name}: {t.description}")

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

questions = [
    "Which HTML tags can you look up? Use html_tag_list.",
    "What does the <a> tag do? Use html_tag_lookup.",
]

for question in questions:
    print(f"\nQuestion: {question}")
    result = agent.invoke({"messages": [{"role": "user", "content": question}]})
    print(f"Answer: {result['messages'][-1].content}")