"""
{{ display_name }} - Fetch node.

This module handles fetching data from the source URL.
"""

from torivers_sdk.tools import HttpClient

from state import {{ class_name }}State


async def fetch(state: {{ class_name }}State) -> dict:
    """Fetch data from the source URL.

    Args:
        state: Current pipeline state.

    Returns:
        Dictionary with state updates including raw data.
    """
    context = state["context"]
    input_data = state["input_data"]

    data_url = input_data.get("data_url")
    context.log_progress("Fetching data", f"Downloading from {data_url}...")

    http = HttpClient.get_default()
    response = await http.get(data_url)

    raw_data = response.body.decode("utf-8")
    context.log_success("Data fetched", f"Downloaded {len(raw_data)} bytes")

    return {
        "current_step": "data_fetched",
        "raw_data": raw_data,
        "metadata": {
            "source_url": data_url,
            "content_length": len(raw_data),
        },
    }
