{
        "description": "Check for overdue invoices and send reminders",
        "trigger": {
            "event": "schedule:daily",
            "schedule": "0 3 * * *",
            "description": "Run at 3 AM daily"
        },
        "steps": [
            {
                "order": 1,
                "type": "query",
                "sql": "SELECT invoice_id, invoice_number, name, amount, amount_paid, invoice_date FROM invoices WHERE status IN ('pending', 'submitted') AND invoice_date + INTERVAL '30 days' < NOW() AND amount > amount_paid AND NOT isDeleted",
                "outputVar": "overdue_invoices",
                "description": "Find all overdue invoices"
            },
            {
                "order": 2,
                "type": "foreach",
                "collection": "{overdue_invoices}",
                "steps": [
                    {
                        "type": "update",
                        "table": "invoices",
                        "fields": {
                            "status": "overdue"
                        },
                        "where": {
                            "invoice_id": "{item.invoice_id}"
                        }
                    },
                    {
                        "type": "notification",
                        "recipients": ["{item.customer_email}", "accounting_team"],
                        "template": "invoice_overdue_reminder",
                        "data": {
                            "invoice_number": "{item.invoice_number}",
                            "customer_name": "{item.name}",
                            "amount": "{item.amount}",
                            "amount_paid": "{item.amount_paid}",
                            "amount_due": "{item.amount} - {item.amount_paid}",
                            "days_overdue": "DATEDIFF(NOW(), {item.invoice_date} + INTERVAL '30 days')"
                        },
                        "channels": ["email", "sms"]
                    },
                    {
                        "type": "emit",
                        "event": "invoice:overdue",
                        "data": {
                            "invoice_id": "{item.invoice_id}",
                            "days_overdue": "DATEDIFF(NOW(), {item.invoice_date} + INTERVAL '30 days')"
                        }
                    }
                ]
            },
            {
                "order": 3,
                "type": "audit",
                "action": "invoices_overdue_check",
                "data": {
                    "count": "{overdue_invoices.length}",
                    "total_overdue_amount": "{overdue_invoices.reduce((sum, inv) => sum + (inv.amount - inv.amount_paid), 0)}"
                }
            }
        ]
    }