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

from langchain_core.messages import HumanMessage, SystemMessage
{% set providers = ir.used_providers %}
{% if 'openai' in providers or 'openai_compatible' in providers %}
from langchain_openai import ChatOpenAI
{% endif %}
{% if 'azure_openai' in providers %}
from langchain_openai import AzureChatOpenAI
{% endif %}
{% if 'anthropic' in providers %}
from langchain_anthropic import ChatAnthropic
{% endif %}
{% if 'google' in providers %}
from langchain_google_genai import ChatGoogleGenerativeAI
{% endif %}
{% if 'ollama' in providers %}
from langchain_ollama import ChatOllama
{% endif %}
{% if 'bedrock' in providers %}
from langchain_aws import ChatBedrock
{% endif %}

from state import AgentState
from tools import TOOLS

{% macro make_llm(node, temperature) %}
{% set p = node.resolved_provider %}
{% set m = node.resolved_model %}
{% set pd = node.resolved_provider_def %}
{% if p == "anthropic" %}
llm = ChatAnthropic(model="{{ m }}", temperature={{ temperature }})
{% elif p == "google" %}
llm = ChatGoogleGenerativeAI(model="{{ m }}", temperature={{ temperature }})
{% elif p == "ollama" %}
llm = ChatOllama(model="{{ m }}", temperature={{ temperature }}, base_url="{{ pd.base_url if pd else 'http://localhost:11434' }}")
{% elif p == "azure_openai" %}
llm = AzureChatOpenAI(azure_deployment="{{ pd.deployment }}", azure_endpoint="{{ pd.base_url }}", api_version="{{ pd.api_version or '2024-02-01' }}", temperature={{ temperature }})
{% elif p == "bedrock" %}
llm = ChatBedrock(model_id="{{ m }}", region_name="{{ pd.region or 'us-east-1' }}", temperature={{ temperature }})
{% elif p == "openai_compatible" %}
llm = ChatOpenAI(model="{{ m }}", temperature={{ temperature }}, base_url="{{ pd.base_url }}")
{% else %}
llm = ChatOpenAI(model="{{ m }}", temperature={{ temperature }})
{% endif %}
{% endmacro %}

{% for node in ir.nodes %}
{% if node.agent %}
def node_{{ node.id | safe_id }}(state: AgentState) -> dict:
    """{{ node.description }}"""
    {% set temperature = node.agent.temperature if node.agent.temperature is not none else ir.settings.default_temperature %}
    {{ make_llm(node, temperature) }}
{% if node.tool_defs %}
    tools = TOOLS.get("{{ node.id }}", [])
    if tools:
        llm = llm.bind_tools(tools)
{% endif %}
    messages = list(state.get("messages", []))
{% if node.agent.system_prompt %}
    system_prompt = """{{ node.agent.system_prompt | escape_string }}"""
    if not any(isinstance(m, SystemMessage) for m in messages):
        messages = [SystemMessage(content=system_prompt)] + messages
{% endif %}
    response = llm.invoke(messages)

    updates: dict = {"messages": [response]}
{% for field_name, field_def in node.agent.output_schema.items() %}
    # Extract {{ field_name }} from response
    if hasattr(response, "tool_calls") and response.tool_calls:
        for tc in response.tool_calls:
            if "{{ field_name }}" in tc.get("args", {}):
                updates["{{ field_name }}"] = tc["args"]["{{ field_name }}"]
                break
{% endfor %}
    return updates

{% elif node.node_def.type.value == "handoff" %}
def node_{{ node.id | safe_id }}(state: AgentState) -> dict:
    """Human handoff: {{ node.description }}"""
    print("[HANDOFF] {{ node.description }}")
    return {}

{% elif node.node_def.type.value == "function" %}
def node_{{ node.id | safe_id }}(state: AgentState) -> dict:
    """Function node: {{ node.description }}"""
    # TODO: Implement action: {{ node.node_def.action or node.id }}
    return {}

{% endif %}
{% endfor %}
