"""Tools generated from blueprint: {{ ir.name }}"""
# Generated by agent-blueprint {{ ir.version }}

import os
import httpx
from langchain_core.tools import tool

{% for tool_name, tool_def in ir.all_tools.items() %}
{% if tool_def.type.value == "function" and tool_def.impl %}
{% set p = impl_parts(tool_name, tool_def.impl) %}
{{ p.import_stmt }}
{% endif %}
{% endfor %}

{% for tool_name, tool_def in ir.all_tools.items() %}
{% if tool_def.type.value == "function" %}
{% if tool_def.impl %}
{% set p = impl_parts(tool_name, tool_def.impl) %}
# {{ tool_name }} → wired from {{ tool_def.impl }}
{{ tool_name }} = tool({{ p.alias }}, name="{{ tool_name }}", description="{{ tool_def.description or tool_name | escape_string }}")

{% else %}
@tool
def {{ tool_name }}(
{% for param_name, param_def in tool_def.parameters.items() %}
    {{ param_name }}: {{ param_def.type | python_type }}{% if not loop.last %},{% endif %}

{% endfor %}
) -> str:
    """{{ tool_def.description or tool_name }}"""
    # TODO: implement {{ tool_name }}
    # Move your implementation here or use the 'impl' field in the blueprint
    # to point to an existing function: impl: "mypackage.module.{{ tool_name }}"
    raise NotImplementedError("{{ tool_name }} is not implemented yet")

{% endif %}
{% elif tool_def.type.value == "api" %}
@tool
def {{ tool_name }}(
{% for param_name, param_def in tool_def.parameters.items() %}
    {{ param_name }}: {{ param_def.type | python_type }}{% if not loop.last %},{% endif %}

{% endfor %}
) -> str:
    """{{ tool_def.description or tool_name }}"""
{% if tool_def.auth %}
    headers = {}
{% if tool_def.auth.type.value == "bearer" %}
    token = os.environ.get("{{ tool_def.auth.token_env }}", "")
    headers["Authorization"] = f"Bearer {token}"
{% endif %}
{% else %}
    headers = {}
{% endif %}
    url = "{{ tool_def.url }}"
    response = httpx.{{ tool_def.method.value.lower() }}(url, headers=headers)
    response.raise_for_status()
    return response.text

{% elif tool_def.type.value == "retrieval" %}
def {{ tool_name }}_retriever():
    """Retrieval tool: {{ tool_def.description or tool_name }}"""
    # TODO: Configure retrieval from source: {{ tool_def.source }}
    raise NotImplementedError("Configure retrieval for {{ tool_name }}")

@tool
def {{ tool_name }}(query: str) -> str:
    """{{ tool_def.description or tool_name }} (top_k={{ tool_def.top_k }})"""
    retriever = {{ tool_name }}_retriever()
    docs = retriever.get_relevant_documents(query)
    return "\n\n".join(d.page_content for d in docs)

{% endif %}
{% endfor %}

TOOLS: dict[str, list] = {
{% for node in ir.nodes %}
{% if node.tool_defs %}
    "{{ node.id }}": [{% for t in node.tool_defs.keys() %}{{ t }}{% if not loop.last %}, {% endif %}{% endfor %}],
{% endif %}
{% endfor %}
}
