"""
{{ display_name }} - Transform node.

This module handles data transformation and cleaning using pandas.
"""

from io import StringIO

import pandas as pd

from state import {{ class_name }}State


async def transform(state: {{ class_name }}State) -> dict:
    """Transform and clean the raw data.

    Args:
        state: Current pipeline state.

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

    context.log_progress("Transforming", "Parsing and cleaning data...")

    # Get options
    options = input_data.get("options", {})
    delimiter = options.get("delimiter", ",")
    skip_rows = options.get("skip_rows", 0)

    # Parse CSV data
    df = pd.read_csv(
        StringIO(state["raw_data"]),
        delimiter=delimiter,
        skiprows=skip_rows,
    )

    # Basic cleaning
    df = df.dropna(how="all")  # Remove completely empty rows
    df.columns = df.columns.str.strip()  # Clean column names

    context.log_success(
        "Transform complete",
        f"Processed {len(df)} rows, {len(df.columns)} columns",
    )

    return {
        "current_step": "transformed",
        "dataframe_json": df.to_json(orient="records"),
        "row_count": len(df),
        "column_count": len(df.columns),
        "metadata": {
            "columns": df.columns.tolist(),
            "dtypes": df.dtypes.astype(str).to_dict(),
        },
    }
