{
  "name": "TrustField Agentic Outbound Dispatch",
  "nodes": [
    {
      "parameters": {},
      "id": "trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.TRUSTFIELD_API_URL}}/api/admin/agentic/dispatch/request",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "company",
              "value": "={{$json.company}}"
            },
            {
              "name": "target_email",
              "value": "={{$json.target_email}}"
            },
            {
              "name": "subject",
              "value": "={{$json.subject}}"
            },
            {
              "name": "template_id",
              "value": "={{$json.template_id}}"
            },
            {
              "name": "body_preview",
              "value": "={{$json.body_html}}"
            },
            {
              "name": "workflow_run_id",
              "value": "={{$execution.id}}"
            }
          ]
        }
      },
      "id": "hitl_request",
      "name": "HITL Queue Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        480,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "amount": 1,
        "unit": "hours"
      },
      "id": "wait_hitl",
      "name": "Wait HITL (or Telegram approve)",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.TRUSTFIELD_API_URL}}/api/admin/agentic/dispatch/send",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "queue_id",
              "value": "={{$node['HITL Queue Request'].json.queue_id}}"
            }
          ]
        }
      },
      "id": "resend_send",
      "name": "Resend via API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        960,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.TRUSTFIELD_API_URL}}/api/admin/agentic/dispatch-ack",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "company",
              "value": "={{$json.company}}"
            },
            {
              "name": "dispatch_ref",
              "value": "=resend:{{$json.message_id}}"
            },
            {
              "name": "message_id",
              "value": "={{$json.message_id}}"
            },
            {
              "name": "provider",
              "value": "resend"
            },
            {
              "name": "template_id",
              "value": "={{$json.template_id}}"
            }
          ]
        }
      },
      "id": "ledger_ack",
      "name": "Ledger Ack (fallback)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1200,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "HITL Queue Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HITL Queue Request": {
      "main": [
        [
          {
            "node": "Wait HITL (or Telegram approve)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait HITL (or Telegram approve)": {
      "main": [
        [
          {
            "node": "Resend via API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resend via API": {
      "main": [
        [
          {
            "node": "Ledger Ack (fallback)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Import to n8n. Env: TRUSTFIELD_API_URL, X-Admin-Token credential. See docs/gtm/AGENTIC_N8N_DISPATCH_WORKFLOW.md"
  }
}