use adk_action::{TriggerNodeConfig, TriggerType};
use serde_json::json;
use crate::error::Result;
use crate::node::{NodeContext, NodeOutput};
pub async fn execute_trigger(config: &TriggerNodeConfig, ctx: &NodeContext) -> Result<NodeOutput> {
match config.trigger_type {
TriggerType::Manual => execute_manual_trigger(config, ctx),
TriggerType::Webhook => Ok(NodeOutput::new().with_update(
&config.standard.mapping.output_key,
json!({
"trigger_type": "webhook",
"timestamp": chrono::Utc::now().to_rfc3339(),
"path": config.webhook.as_ref().map(|w| w.path.as_str()).unwrap_or(""),
}),
)),
TriggerType::Schedule => Ok(NodeOutput::new().with_update(
&config.standard.mapping.output_key,
json!({
"trigger_type": "schedule",
"timestamp": chrono::Utc::now().to_rfc3339(),
"cron": config.schedule.as_ref().map(|s| s.cron.as_str()).unwrap_or(""),
}),
)),
TriggerType::Event => Ok(NodeOutput::new().with_update(
&config.standard.mapping.output_key,
json!({
"trigger_type": "event",
"timestamp": chrono::Utc::now().to_rfc3339(),
"source": config.event.as_ref().map(|e| e.source.as_str()).unwrap_or(""),
"event_type": config.event.as_ref().map(|e| e.event_type.as_str()).unwrap_or(""),
}),
)),
}
}
fn execute_manual_trigger(config: &TriggerNodeConfig, ctx: &NodeContext) -> Result<NodeOutput> {
let manual_config = config.manual.as_ref();
let input_label = manual_config.and_then(|m| m.input_label.as_deref()).unwrap_or("input");
let input_value = ctx.state.get(input_label).cloned().unwrap_or_else(|| {
manual_config
.and_then(|m| m.default_prompt.as_deref())
.map(|p| json!(p))
.unwrap_or(json!(null))
});
let output = json!({
"trigger_type": "manual",
"timestamp": chrono::Utc::now().to_rfc3339(),
"input": input_value,
"input_label": input_label,
});
Ok(NodeOutput::new().with_update(&config.standard.mapping.output_key, output))
}