{
  "id": "CODWF00SendNotification",
  "name": "COD · WF-00 Send Notification",
  "active": false,
  "nodes": [
    {
      "parameters": {
        "content": "## Notification layer\nReusable Telegram/email delivery. Callers select a template and pass only trusted variables. WhatsApp and SMS are intentionally disabled extension points.",
        "height": 220,
        "width": 1180,
        "color": 5
      },
      "id": "wf00-note-layer",
      "name": "Layer 0 · Notification Gateway",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-700, -260]
    },
    {
      "parameters": {},
      "id": "wf00-manual-trigger",
      "name": "Manual Test Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [-640, -100]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "wf00-test-audience", "name": "audience", "value": "admin", "type": "string" },
            { "id": "wf00-test-channel", "name": "channel", "value": "auto", "type": "string" },
            { "id": "wf00-test-template", "name": "template", "value": "test", "type": "string" },
            { "id": "wf00-test-language", "name": "language", "value": "ar", "type": "string" },
            { "id": "wf00-test-vars", "name": "vars", "value": "={}", "type": "object" }
          ]
        },
        "options": {}
      },
      "id": "wf00-manual-input",
      "name": "Manual Admin Test Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-400, -100]
    },
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "id": "wf00-trigger",
      "name": "Notification Request",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.2,
      "position": [-640, 40]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "wf00-resolved-channel",
              "name": "resolvedChannel",
              "value": "={{ $json.channel === 'auto' ? ($json.audience === 'admin' ? 'both' : ($json.chatId ? 'telegram' : 'email')) : $json.channel }}",
              "type": "string"
            },
            {
              "id": "wf00-resolved-chat",
              "name": "resolvedChatId",
              "value": "={{ $json.audience === 'admin' ? $env.TELEGRAM_ADMIN_CHAT_ID : ($json.chatId || '') }}",
              "type": "string"
            },
            {
              "id": "wf00-resolved-email",
              "name": "resolvedEmail",
              "value": "={{ $json.audience === 'admin' ? $env.OWNER_EMAIL : ($json.email || '') }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "wf00-resolve",
      "name": "Resolve Recipient & Channel",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-400, 40]
    },
    {
      "parameters": {
        "jsCode": "@@code:templates"
      },
      "id": "wf00-render",
      "name": "Render Template AR/EN",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [-160, 40]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
                "conditions": [
                  { "id": "wf00-switch-telegram", "leftValue": "={{ $json.resolvedChannel }}", "rightValue": "^(telegram|both)$", "operator": { "type": "string", "operation": "regex" } }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Telegram"
            },
            {
              "conditions": {
                "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
                "conditions": [
                  { "id": "wf00-switch-email", "leftValue": "={{ $json.resolvedChannel }}", "rightValue": "^(email|both)$", "operator": { "type": "string", "operation": "regex" } }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Email"
            },
            {
              "conditions": {
                "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
                "conditions": [
                  { "id": "wf00-switch-whatsapp", "leftValue": "={{ $json.resolvedChannel }}", "rightValue": "whatsapp", "operator": { "type": "string", "operation": "equals" } }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "WhatsApp (future)"
            },
            {
              "conditions": {
                "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 2 },
                "conditions": [
                  { "id": "wf00-switch-sms", "leftValue": "={{ $json.resolvedChannel }}", "rightValue": "sms", "operator": { "type": "string", "operation": "equals" } }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "SMS (future)"
            }
          ]
        },
        "options": { "fallbackOutput": "none", "allMatchingOutputs": true }
      },
      "id": "wf00-switch",
      "name": "Channel Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [80, 40]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "={{ $json.resolvedChatId }}",
        "text": "={{ $json.rendered.text }}",
        "replyMarkup": "={{ $json.buttons?.length ? 'inlineKeyboard' : 'none' }}",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  { "text": "={{ $json.buttons?.[0]?.text || 'تأكيد' }}", "additionalFields": { "callback_data": "={{ $json.buttons?.[0]?.callbackData || ('act:confirm:' + ($json.orderId || '')) }}" } },
                  { "text": "={{ $json.buttons?.[1]?.text || 'إلغاء' }}", "additionalFields": { "callback_data": "={{ $json.buttons?.[1]?.callbackData || ('act:cancel:' + ($json.orderId || '')) }}" } }
                ]
              }
            }
          ]
        },
        "additionalFields": { "appendAttribution": false }
      },
      "id": "wf00-telegram",
      "name": "Telegram Send",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [360, -120],
      "credentials": {
        "telegramApi": { "id": "cred_telegram", "name": "COD Telegram Bot" }
      }
    },
    {
      "parameters": {
        "resource": "email",
        "operation": "send",
        "fromEmail": "={{ $env.SMTP_FROM }}",
        "toEmail": "={{ $json.resolvedEmail }}",
        "subject": "={{ $json.rendered.subject }}",
        "emailFormat": "both",
        "text": "={{ $json.rendered.text }}",
        "html": "={{ $json.rendered.html }}",
        "options": { "appendAttribution": false }
      },
      "id": "wf00-email",
      "name": "Send Email SMTP",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [360, 20],
      "credentials": {
        "smtp": { "id": "cred_smtp", "name": "COD SMTP" }
      }
    },
    {
      "parameters": {},
      "id": "wf00-whatsapp",
      "name": "WhatsApp Provider (Future)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [360, 160],
      "disabled": true,
      "notesInFlow": true,
      "notes": "Reserved for Evolution API or WhatsApp Cloud API."
    },
    {
      "parameters": {},
      "id": "wf00-sms",
      "name": "SMS Provider (Future)",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [360, 280],
      "disabled": true,
      "notesInFlow": true,
      "notes": "Reserved for a future SMS provider."
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.INTERNAL_API_URL }}/api/internal/conversations",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ orderId: $('Render Template AR/EN').item.json.orderId || undefined, customerId: $('Render Template AR/EN').item.json.customerId || undefined, channel: ($json.chat || $json.result?.chat) ? 'TELEGRAM' : 'EMAIL', direction: 'OUT', messageType: 'TEMPLATE', content: $('Render Template AR/EN').item.json.rendered.text }) }}",
        "options": { "timeout": 10000 }
      },
      "id": "wf00-log",
      "name": "Log Outbound Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [640, -40],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 1000,
      "continueOnFail": true,
      "credentials": {
        "httpHeaderAuth": { "id": "cred_internal_api", "name": "COD Internal API Bearer" }
      }
    }
  ],
  "connections": {
    "Manual Test Trigger": { "main": [[{ "node": "Manual Admin Test Input", "type": "main", "index": 0 }]] },
    "Manual Admin Test Input": { "main": [[{ "node": "Resolve Recipient & Channel", "type": "main", "index": 0 }]] },
    "Notification Request": { "main": [[{ "node": "Resolve Recipient & Channel", "type": "main", "index": 0 }]] },
    "Resolve Recipient & Channel": { "main": [[{ "node": "Render Template AR/EN", "type": "main", "index": 0 }]] },
    "Render Template AR/EN": { "main": [[{ "node": "Channel Switch", "type": "main", "index": 0 }]] },
    "Channel Switch": {
      "main": [
        [{ "node": "Telegram Send", "type": "main", "index": 0 }],
        [{ "node": "Send Email SMTP", "type": "main", "index": 0 }],
        [{ "node": "WhatsApp Provider (Future)", "type": "main", "index": 0 }],
        [{ "node": "SMS Provider (Future)", "type": "main", "index": 0 }]
      ]
    },
    "Telegram Send": { "main": [[{ "node": "Log Outbound Message", "type": "main", "index": 0 }]] },
    "Send Email SMTP": { "main": [[{ "node": "Log Outbound Message", "type": "main", "index": 0 }]] }
  },
  "settings": {
    "executionOrder": "v1",
    "errorWorkflow": "CODWF99ErrorHandler",
    "timezone": "Africa/Cairo"
  },
  "versionId": "f0000000-0000-4000-8000-000000000000",
  "meta": { "templateCredsSetupCompleted": true },
  "tags": []
}
