# Copyright (c) 2025 Patrick Motsch
# Trigger node executor.

import logging
from typing import Dict, Any

logger = logging.getLogger(__name__)


class TriggerExecutor:
    """Execute trigger nodes (manual, schedule, formSubmit)."""

    async def execute(
        self,
        node: Dict[str, Any],
        context: Dict[str, Any],
    ) -> Any:
        nodeType = node.get("type", "")
        nodeId = node.get("id", "")
        logger.info("TriggerExecutor node %s type=%s parameters=%s", nodeId, nodeType, node.get("parameters"))
        if nodeType == "trigger.manual":
            out = {"triggered": True, "source": "manual"}
            logger.info("TriggerExecutor node %s -> manual trigger: %s", nodeId, out)
            return out
        if nodeType == "trigger.schedule":
            out = {"triggered": True, "source": "schedule"}
            logger.info("TriggerExecutor node %s -> schedule trigger: %s", nodeId, out)
            return out
        if nodeType == "trigger.formSubmit":
            params = node.get("parameters") or {}
            formId = params.get("formId", "")
            out = {"triggered": True, "source": "formSubmit", "formId": formId}
            logger.info("TriggerExecutor node %s -> formSubmit: %s", nodeId, out)
            return out
        out = {"triggered": True, "source": "unknown"}
        logger.info("TriggerExecutor node %s -> unknown: %s", nodeId, out)
        return out
