{
  "name": "RozvyEstates_A_Initial_MultiChannel_Blast",
  "nodes": [
    {
      "parameters": {},
      "name": "0. Manual Trigger (Run Once)",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        50,
        130
      ]
    },
    {
      "parameters": {
        "fileName": "=[INSERT YOUR CSV FILE NAME HERE]",
        "fileFormat": "csv",
        "options": {
          "columnDelimiter": ","
        },
        "outputFormat": "items"
      },
      "name": "1. Read Leads CSV",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        250,
        130
      ]
    },
    {
      "parameters": {
        "batchSize": 20,
        "options": {
          "wait": 500
        }
      },
      "name": "2. Batch Processor (Safety)",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        450,
        130
      ]
    },
    {
      "parameters": {
        "mode": "if",
        "value1": "={{ $json.Phone }}",
        "value2": "",
        "operator": "notEqual"
      },
      "name": "3. Check Phone",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        70
      ]
    },
    {
      "parameters": {
        "url": "https://gate.whapi.cloud/messages/text",
        "method": "POST",
        "body": "{\"to\": \"+27{{ $json.Phone }}\", \"body\": \"Hello {{ $json.Name }}. Rozvy Estates here! Special personalized offer just for you. Reply YES to this message for details on our goat meat.\"}",
        "authentication": "headerAuth",
        "options": {
          "headerAuth": [
            {
              "name": "Authorization",
              "value": "Bearer [INSERT YOUR WHAPI TOKEN]"
            }
          ]
        }
      },
      "name": "4A. Send WhatsApp Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        870,
        70
      ]
    },
    {
      "parameters": {
        "mode": "if",
        "value1": "={{ $json.Email }}",
        "value2": "",
        "operator": "notEqual"
      },
      "name": "3. Check Email",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        250
      ]
    },
    {
      "parameters": {
        "resource": "subscribers",
        "operation": "add",
        "email": "={{ $json.Email }}",
        "name": "={{ $json.Name }}",
        "options": {
          "addGroup": "[INSERT MAILERLITE GROUP ID]",
          "status": "active"
        }
      },
      "name": "4B. Add Subscriber to MailerLite",
      "type": "n8n-nodes-base.mailerLite",
      "typeVersion": 1,
      "position": [
        870,
        250
      ]
    },
    {
      "parameters": {
        "functionCode": "let whatsAppSent = ($('4A. Send WhatsApp Message').item.length > 0) ? 'SENT' : 'NOT SENT (No Phone)';\nlet emailSent = ($('4B. Add Subscriber to MailerLite').item.length > 0) ? 'SENT' : 'NOT SENT (No Email)';\n\n// Create a new item containing the original lead data plus the new status fields\nlet newItems = [\n    {\n        json: {\n            ...$input.json,\n            WhatsApp_Status: whatsAppSent,\n            Email_Status: emailSent,\n            Date_Sent: new Date().toISOString().split('T')[0],\n            Reply_Status: 'PENDING'\n        }\n    }\n];\n\nreturn newItems;",
        "options": {}
      },
      "name": "5. Update Lead Status",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1100,
        160
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "spreadsheetId": "[INSERT YOUR GOOGLE SHEET ID]",
        "sheetId": "[INSERT YOUR SHEET TAB ID]",
        "key": "Email",
        "keyValue": "={{ $json.Email }}",
        "dataToSend": "={\n\"WhatsApp_Status\": \"={{ $json.WhatsApp_Status }}\",\n\"Email_Status\": \"={{ $json.Email_Status }}\",\n\"Date_Sent\": \"={{ $json.Date_Sent }}\",\n\"Reply_Status\": \"={{ $json.Reply_Status }}\"\n}",
        "options": {
          "writeHeader": true
        }
      },
      "name": "6. Log Status to Google Sheet",
      "type": "n8n-nodes-google-sheets.googleSheets",
      "typeVersion": 1,
      "position": [
        1350,
        160
      ]
    }
  ],
  "connections": {
    "0. Manual Trigger (Run Once)": [
      [
        "1. Read Leads CSV"
      ]
    ],
    "1. Read Leads CSV": [
      [
        "2. Batch Processor (Safety)"
      ]
    ],
    "2. Batch Processor (Safety)": [
      [
        "3. Check Phone"
      ],
      [
        "3. Check Email"
      ]
    ],
    "3. Check Phone": [
      [
        "4A. Send WhatsApp Message",
        "main"
      ],
      [],
      [
        "5. Update Lead Status",
        "false"
      ]
    ],
    "3. Check Email": [
      [
        "4B. Add Subscriber to MailerLite",
        "main"
      ],
      [],
      [
        "5. Update Lead Status",
        "false"
      ]
    ],
    "4A. Send WhatsApp Message": [
      [
        "5. Update Lead Status"
      ]
    ],
    "4B. Add Subscriber to MailerLite": [
      [
        "5. Update Lead Status"
      ]
    ],
    "5. Update Lead Status": [
      [
        "6. Log Status to Google Sheet"
      ]
    ]
  }
}