# Copyright (c) 2025 Patrick Motsch
# Start node executor (node type trigger.manual) — outputs the unified run envelope from context.

import logging
from typing import Any, Dict

from modules.workflows.automation2.runEnvelope import normalize_run_envelope

logger = logging.getLogger(__name__)


class TriggerExecutor:
    """
    Single start node on the canvas. Output is always context['runEnvelope'], normalized.
    Invocation mode (manual, form, webhook, …) is configured as workflow entry points, not here.
    """

    async def execute(
        self,
        node: Dict[str, Any],
        context: Dict[str, Any],
    ) -> Any:
        node_id = node.get("id", "")
        base = context.get("runEnvelope")
        if not isinstance(base, dict):
            out = normalize_run_envelope(None, user_id=context.get("userId"))
        else:
            out = normalize_run_envelope(base, user_id=context.get("userId"))
        logger.info(
            "TriggerExecutor node %s trigger.type=%s",
            node_id,
            (out.get("trigger") or {}).get("type"),
        )
        return out
