{
  "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": "let __smartCodRun;\n(() => {\n  // n8n/code/templates.ts\n  var TEMPLATE_KEYS = [\n    \"confirmation\",\n    \"receipt\",\n    \"cancellation\",\n    \"reminder_1\",\n    \"reminder_2\",\n    \"reminder_3\",\n    \"retention_offer\",\n    \"address_request\",\n    \"address_confirmation\",\n    \"shipping_update\",\n    \"fraud_alert\",\n    \"review_alert\",\n    \"low_stock_alert\",\n    \"warehouse_packing_slip\",\n    \"daily_digest\",\n    \"holding_reply\",\n    \"question_reply\",\n    \"test\"\n  ];\n  var value = (variables, key, fallback = \"-\") => String(variables[key] ?? fallback);\n  var templates = {\n    ar: {\n      confirmation: (v) => ({\n        subject: `\\u062A\\u0623\\u0643\\u064A\\u062F \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}`,\n        text: `\\u0645\\u0631\\u062D\\u0628\\u064B\\u0627 ${value(v, \"customerName\", \"\\u0639\\u0645\\u064A\\u0644\\u0646\\u0627\")}. \\u064A\\u0631\\u062C\\u0649 \\u062A\\u0623\\u0643\\u064A\\u062F \\u0637\\u0644\\u0628\\u0643 ${value(v, \"orderId\")} \\u0628\\u0642\\u064A\\u0645\\u0629 ${value(v, \"codAmount\")} ${value(v, \"currency\", \"EGP\")}.\n${value(v, \"confirmationUrl\", \"\")}\n\\u0627\\u0631\\u0628\\u0637 \\u062A\\u064A\\u0644\\u064A\\u062C\\u0631\\u0627\\u0645 \\u0644\\u0645\\u062A\\u0627\\u0628\\u0639\\u0629 \\u0627\\u0644\\u0637\\u0644\\u0628: ${value(v, \"telegramLink\", \"\")}`\n      }),\n      receipt: (v) => ({ subject: `\\u062A\\u0645 \\u062A\\u0623\\u0643\\u064A\\u062F ${value(v, \"orderId\")}`, text: `\\u2705 \\u062A\\u0645 \\u062A\\u0623\\u0643\\u064A\\u062F \\u0637\\u0644\\u0628\\u0643 ${value(v, \"orderId\")} \\u0648\\u0633\\u0646\\u062C\\u0647\\u0632\\u0647 \\u0644\\u0644\\u0634\\u062D\\u0646.` }),\n      cancellation: (v) => ({ subject: `\\u0625\\u0644\\u063A\\u0627\\u0621 ${value(v, \"orderId\")}`, text: `\\u062A\\u0645 \\u0625\\u0644\\u063A\\u0627\\u0621 \\u0637\\u0644\\u0628\\u0643 ${value(v, \"orderId\")}. \\u064A\\u0633\\u0639\\u062F\\u0646\\u0627 \\u062E\\u062F\\u0645\\u062A\\u0643 \\u0641\\u064A \\u0623\\u064A \\u0648\\u0642\\u062A.` }),\n      reminder_1: (v) => ({ subject: \"\\u062A\\u0630\\u0643\\u064A\\u0631 \\u0628\\u062A\\u0623\\u0643\\u064A\\u062F \\u0627\\u0644\\u0637\\u0644\\u0628\", text: `\\u062A\\u0630\\u0643\\u064A\\u0631: \\u0637\\u0644\\u0628\\u0643 \\u0631\\u0642\\u0645 ${value(v, \"orderId\")} \\u0644\\u0627 \\u064A\\u0632\\u0627\\u0644 \\u0628\\u0627\\u0646\\u062A\\u0638\\u0627\\u0631 \\u062A\\u0623\\u0643\\u064A\\u062F\\u0643! ${value(v, \"confirmationUrl\", \"\")}` }),\n      reminder_2: (v) => ({ subject: \"\\u062A\\u0630\\u0643\\u064A\\u0631 \\u062B\\u0627\\u0646\\u064D \\u0628\\u062A\\u0623\\u0643\\u064A\\u062F \\u0627\\u0644\\u0637\\u0644\\u0628\", text: `\\u0646\\u0648\\u062F \\u062A\\u062C\\u0647\\u064A\\u0632 \\u0637\\u0644\\u0628\\u0643 ${value(v, \"orderId\")}. \\u0623\\u0643\\u0651\\u062F \\u0627\\u0644\\u0637\\u0644\\u0628 \\u0645\\u0646 \\u0647\\u0646\\u0627: ${value(v, \"confirmationUrl\", \"\")}` }),\n      reminder_3: (v) => ({ subject: \"\\u0622\\u062E\\u0631 \\u062A\\u0630\\u0643\\u064A\\u0631 \\u0628\\u062A\\u0623\\u0643\\u064A\\u062F \\u0627\\u0644\\u0637\\u0644\\u0628\", text: `\\u0647\\u0630\\u0627 \\u0622\\u062E\\u0631 \\u062A\\u0630\\u0643\\u064A\\u0631 \\u0644\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}. \\u064A\\u0631\\u062C\\u0649 \\u0627\\u0644\\u062A\\u0623\\u0643\\u064A\\u062F \\u0623\\u0648 \\u0627\\u0644\\u0625\\u0644\\u063A\\u0627\\u0621: ${value(v, \"confirmationUrl\", \"\")}` }),\n      retention_offer: (v) => ({ subject: \"\\u0639\\u0631\\u0636 \\u062E\\u0627\\u0635 \\u0644\\u0625\\u062A\\u0645\\u0627\\u0645 \\u0637\\u0644\\u0628\\u0643\", text: `\\u064A\\u0645\\u0643\\u0646\\u0646\\u0627 \\u062A\\u0642\\u062F\\u064A\\u0645 \\u062E\\u0635\\u0645 ${value(v, \"discountPercent\")}\\u066A \\u0639\\u0644\\u0649 \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}. \\u0647\\u0644 \\u062A\\u0631\\u063A\\u0628 \\u0641\\u064A \\u0642\\u0628\\u0648\\u0644 \\u0627\\u0644\\u0639\\u0631\\u0636\\u061F` }),\n      address_request: (v) => ({ subject: \"\\u062A\\u062D\\u062F\\u064A\\u062B \\u0639\\u0646\\u0648\\u0627\\u0646 \\u0627\\u0644\\u062A\\u0648\\u0635\\u064A\\u0644\", text: `\\u0623\\u0631\\u0633\\u0644 \\u0627\\u0644\\u0639\\u0646\\u0648\\u0627\\u0646 \\u0627\\u0644\\u062C\\u062F\\u064A\\u062F \\u0628\\u0627\\u0644\\u062A\\u0641\\u0635\\u064A\\u0644 \\u0644\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}\\u060C \\u0645\\u0639 \\u0627\\u0644\\u0645\\u062F\\u064A\\u0646\\u0629 \\u0648\\u0627\\u0644\\u0645\\u062D\\u0627\\u0641\\u0638\\u0629.` }),\n      address_confirmation: (v) => ({ subject: \"\\u062A\\u0645 \\u062A\\u062D\\u062F\\u064A\\u062B \\u0627\\u0644\\u0639\\u0646\\u0648\\u0627\\u0646\", text: `\\u{1F4CD} \\u062A\\u0645 \\u062D\\u0641\\u0638 \\u0639\\u0646\\u0648\\u0627\\u0646 \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}: ${value(v, \"address\")}.` }),\n      shipping_update: (v) => ({ subject: `\\u062A\\u062D\\u062F\\u064A\\u062B \\u0627\\u0644\\u0634\\u062D\\u0646\\u0629 ${value(v, \"awbNumber\")}`, text: `\\u{1F69A} \\u062A\\u062D\\u062F\\u064A\\u062B \\u0627\\u0644\\u0634\\u062D\\u0646\\u0629 (${value(v, \"awbNumber\")}): ${value(v, \"statusText\", \"\\u062A\\u0645 \\u062A\\u062D\\u062F\\u064A\\u062B \\u062D\\u0627\\u0644\\u0629 \\u0634\\u062D\\u0646\\u062A\\u0643\")} ${value(v, \"trackingUrl\", \"\")}` }),\n      fraud_alert: (v) => ({ subject: \"\\u062A\\u0646\\u0628\\u064A\\u0647 \\u0627\\u062D\\u062A\\u064A\\u0627\\u0644 \\u0645\\u0631\\u062A\\u0641\\u0639\", text: `\\u{1F6A8} \\u062A\\u0645 \\u062D\\u0638\\u0631 \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")} \\u0628\\u062F\\u0631\\u062C\\u0629 \\u062E\\u0637\\u0648\\u0631\\u0629 ${value(v, \"riskScore\")}. \\u0627\\u0644\\u0623\\u0633\\u0628\\u0627\\u0628: ${value(v, \"riskReasons\")}` }),\n      review_alert: (v) => ({ subject: \"\\u0637\\u0644\\u0628 \\u064A\\u062D\\u062A\\u0627\\u062C \\u0645\\u0631\\u0627\\u062C\\u0639\\u0629\", text: `\\u26A0\\uFE0F \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")} \\u064A\\u062D\\u062A\\u0627\\u062C \\u0645\\u0631\\u0627\\u062C\\u0639\\u0629 \\u0628\\u0634\\u0631\\u064A\\u0629. \\u062F\\u0631\\u062C\\u0629 \\u0627\\u0644\\u062E\\u0637\\u0648\\u0631\\u0629: ${value(v, \"riskScore\")}.${v.transcript ? `\n\\u0627\\u0644\\u0631\\u0633\\u0627\\u0644\\u0629: ${value(v, \"transcript\")}` : \"\"}` }),\n      low_stock_alert: (v) => ({ subject: \"\\u062A\\u0646\\u0628\\u064A\\u0647 \\u0645\\u062E\\u0632\\u0648\\u0646 \\u0645\\u0646\\u062E\\u0641\\u0636\", text: `\\u{1F4E6} \\u0627\\u0644\\u0645\\u062E\\u0632\\u0648\\u0646 \\u0645\\u0646\\u062E\\u0641\\u0636 \\u0644\\u0644\\u0645\\u0646\\u062A\\u062C ${value(v, \"sku\")}: \\u0627\\u0644\\u0645\\u062A\\u0627\\u062D ${value(v, \"available\")}.` }),\n      warehouse_packing_slip: (v) => ({ subject: `\\u062A\\u062C\\u0647\\u064A\\u0632 \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")}`, text: `\\u{1F4E6} \\u062C\\u0647\\u0651\\u0632 \\u0627\\u0644\\u0637\\u0644\\u0628 ${value(v, \"orderId\")} \\u0644\\u0644\\u0634\\u062D\\u0646. \\u0627\\u0644\\u0645\\u0646\\u062A\\u062C\\u0627\\u062A: ${value(v, \"items\")}. \\u0645\\u0644\\u0635\\u0642 \\u0627\\u0644\\u0634\\u062D\\u0646: ${value(v, \"labelUrl\")}.` }),\n      daily_digest: (v) => ({ subject: `\\u0645\\u0644\\u062E\\u0635 \\u0627\\u0644\\u0625\\u063A\\u0644\\u0627\\u0642 \\u0627\\u0644\\u064A\\u0648\\u0645\\u064A ${value(v, \"date\")}`, text: value(v, \"digest\") }),\n      holding_reply: () => ({ subject: \"\\u062A\\u0645 \\u0627\\u0633\\u062A\\u0644\\u0627\\u0645 \\u0631\\u0633\\u0627\\u0644\\u062A\\u0643\", text: \"\\u0634\\u0643\\u0631\\u064B\\u0627 \\u0644\\u062A\\u0648\\u0627\\u0635\\u0644\\u0643. \\u062A\\u0645 \\u062A\\u062D\\u0648\\u064A\\u0644 \\u0631\\u0633\\u0627\\u0644\\u062A\\u0643 \\u0625\\u0644\\u0649 \\u0641\\u0631\\u064A\\u0642 \\u062E\\u062F\\u0645\\u0629 \\u0627\\u0644\\u0639\\u0645\\u0644\\u0627\\u0621 \\u0648\\u0633\\u0646\\u0631\\u062F \\u0639\\u0644\\u064A\\u0643 \\u0642\\u0631\\u064A\\u0628\\u064B\\u0627.\" }),\n      question_reply: (v) => ({ subject: \"\\u0631\\u062F \\u062E\\u062F\\u0645\\u0629 \\u0627\\u0644\\u0639\\u0645\\u0644\\u0627\\u0621\", text: value(v, \"reply\", \"\\u0633\\u0646\\u0631\\u0627\\u062C\\u0639 \\u0633\\u0624\\u0627\\u0644\\u0643 \\u0648\\u0646\\u0631\\u062F \\u0639\\u0644\\u064A\\u0643 \\u0642\\u0631\\u064A\\u0628\\u064B\\u0627.\") }),\n      test: () => ({ subject: \"\\u0627\\u062E\\u062A\\u0628\\u0627\\u0631 \\u0646\\u0638\\u0627\\u0645 \\u0627\\u0644\\u0625\\u0634\\u0639\\u0627\\u0631\\u0627\\u062A\", text: \"\\u2705 \\u064A\\u0639\\u0645\\u0644 \\u0646\\u0638\\u0627\\u0645 \\u0625\\u0634\\u0639\\u0627\\u0631\\u0627\\u062A Smart COD \\u0628\\u0646\\u062C\\u0627\\u062D.\" })\n    },\n    en: {\n      confirmation: (v) => ({ subject: `Confirm order ${value(v, \"orderId\")}`, text: `Hello ${value(v, \"customerName\", \"customer\")}. Please confirm order ${value(v, \"orderId\")} for ${value(v, \"codAmount\")} ${value(v, \"currency\", \"EGP\")}.\n${value(v, \"confirmationUrl\", \"\")}\nLink Telegram for order updates: ${value(v, \"telegramLink\", \"\")}` }),\n      receipt: (v) => ({ subject: `Order ${value(v, \"orderId\")} confirmed`, text: `\\u2705 Your order ${value(v, \"orderId\")} is confirmed and will be prepared for shipping.` }),\n      cancellation: (v) => ({ subject: `Order ${value(v, \"orderId\")} cancelled`, text: `Your order ${value(v, \"orderId\")} has been cancelled.` }),\n      reminder_1: (v) => ({ subject: \"Order confirmation reminder\", text: `Order ${value(v, \"orderId\")} is still waiting for confirmation. ${value(v, \"confirmationUrl\", \"\")}` }),\n      reminder_2: (v) => ({ subject: \"Second order reminder\", text: `We would like to prepare order ${value(v, \"orderId\")}. Confirm it here: ${value(v, \"confirmationUrl\", \"\")}` }),\n      reminder_3: (v) => ({ subject: \"Final order reminder\", text: `This is the final reminder for ${value(v, \"orderId\")}. Please confirm or cancel: ${value(v, \"confirmationUrl\", \"\")}` }),\n      retention_offer: (v) => ({ subject: \"A special offer for your order\", text: `We can offer ${value(v, \"discountPercent\")}% off order ${value(v, \"orderId\")}. Would you like to accept?` }),\n      address_request: (v) => ({ subject: \"Update delivery address\", text: `Send the full new address, city, and region for order ${value(v, \"orderId\")}.` }),\n      address_confirmation: (v) => ({ subject: \"Address updated\", text: `\\u{1F4CD} The address for ${value(v, \"orderId\")} was updated to: ${value(v, \"address\")}.` }),\n      shipping_update: (v) => ({ subject: `Shipment update ${value(v, \"awbNumber\")}`, text: `\\u{1F69A} Shipment update (${value(v, \"awbNumber\")}): ${value(v, \"statusText\", \"Your shipment status changed\")} ${value(v, \"trackingUrl\", \"\")}` }),\n      fraud_alert: (v) => ({ subject: \"High-risk fraud alert\", text: `\\u{1F6A8} Order ${value(v, \"orderId\")} was blocked with risk score ${value(v, \"riskScore\")}. Reasons: ${value(v, \"riskReasons\")}` }),\n      review_alert: (v) => ({ subject: \"Order needs review\", text: `\\u26A0\\uFE0F Order ${value(v, \"orderId\")} requires human review. Risk score: ${value(v, \"riskScore\")}.${v.transcript ? `\nMessage: ${value(v, \"transcript\")}` : \"\"}` }),\n      low_stock_alert: (v) => ({ subject: \"Low-stock alert\", text: `\\u{1F4E6} Stock is low for ${value(v, \"sku\")}: ${value(v, \"available\")} available.` }),\n      warehouse_packing_slip: (v) => ({ subject: `Pack order ${value(v, \"orderId\")}`, text: `\\u{1F4E6} Prepare order ${value(v, \"orderId\")} for shipping. Items: ${value(v, \"items\")}. Label: ${value(v, \"labelUrl\")}.` }),\n      daily_digest: (v) => ({ subject: `Daily closing digest ${value(v, \"date\")}`, text: value(v, \"digest\") }),\n      holding_reply: () => ({ subject: \"We received your message\", text: \"Thanks for contacting us. Your message is with customer support and we will reply soon.\" }),\n      question_reply: (v) => ({ subject: \"Customer support reply\", text: value(v, \"reply\", \"We will review your question and reply shortly.\") }),\n      test: () => ({ subject: \"Notification system test\", text: \"\\u2705 The Smart COD notification system is working.\" })\n    }\n  };\n  function escapeHtml(text) {\n    return text.replaceAll(\"&\", \"&amp;\").replaceAll(\"<\", \"&lt;\").replaceAll(\">\", \"&gt;\").replaceAll('\"', \"&quot;\").replaceAll(\"'\", \"&#39;\");\n  }\n  function isTemplateKey(value2) {\n    return TEMPLATE_KEYS.includes(value2);\n  }\n  function renderTemplate(key, language, variables = {}) {\n    const rendered = templates[language][key](variables);\n    const direction = language === \"ar\" ? \"rtl\" : \"ltr\";\n    return {\n      ...rendered,\n      language,\n      html: `<div dir=\"${direction}\" style=\"font-family:Arial,sans-serif;white-space:pre-line\">${escapeHtml(rendered.text)}</div>`\n    };\n  }\n  function run(items) {\n    return items.map((item) => {\n      const keyValue = String(item.json.template ?? \"test\");\n      if (!isTemplateKey(keyValue)) throw new Error(`Unknown notification template: ${keyValue}`);\n      const language = item.json.language === \"en\" ? \"en\" : \"ar\";\n      const variables = item.json.vars ?? {};\n      return { ...item, json: { ...item.json, rendered: renderTemplate(keyValue, language, variables) } };\n    });\n  }\n\n  // templates.bundle-entry.ts\n  __smartCodRun = run;\n})();\nreturn __smartCodRun($input.all(), { now: new Date().toISOString() });"
      },
      "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": []
}
