{
  "name": "Ativa\u00e7\u00e3o WhatsApp Di\u00e1ria",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * *"
            }
          ]
        }
      },
      "id": "cron-trigger",
      "name": "Cron Di\u00e1rio 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/rpc/leads_para_ativar",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "options": {}
      },
      "id": "buscar-leads",
      "name": "Buscar Leads (Supabase)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.length }}",
              "operation": "larger",
              "value2": 0
            }
          ]
        }
      },
      "id": "check-leads",
      "name": "Verificar Leads",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {},
      "id": "split-leads",
      "name": "Dividir Leads",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.WAHA_BASE_URL }}{{ $env.WAHA_SEND_TEXT_PATH }}",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "chatId",
              "value": "={{ $json.telefone.replace('+', '') }}@c.us"
            },
            {
              "name": "text",
              "value": "Ol\u00e1 {{ $json.nome }}! \ud83d\udc4b\n\nObrigado por se cadastrar. Estamos prontos para atender voc\u00ea pelo WhatsApp.\n\nPara cancelar mensagens futuras, responda PARAR.\n\n---\nVoc\u00ea autorizou o recebimento desta mensagem em {{ $json.data_consentimento }}."
            },
            {
              "name": "session",
              "value": "default"
            }
          ]
        },
        "options": {}
      },
      "id": "enviar-whatsapp",
      "name": "Enviar WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1050,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/whatsapp_envios",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=minimal"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "lead_id",
              "value": "={{ $node['Dividir Leads'].json.id }}"
            },
            {
              "name": "mensagem",
              "value": "={{ $node['Enviar WhatsApp'].parameter.bodyParameters.parameters[1].value }}"
            },
            {
              "name": "status_envio",
              "value": "sucesso"
            },
            {
              "name": "resposta",
              "value": "={{ JSON.stringify($json) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "log-envio",
      "name": "Registrar Envio (Log)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1250,
        200
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/leads?id=eq.{{ $node['Dividir Leads'].json.id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "whatsapp_ativado",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "id": "marcar-ativado",
      "name": "Marcar Ativado",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1450,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SUPABASE_URL }}/rest/v1/whatsapp_envios",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SUPABASE_SERVICE_ROLE }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "lead_id",
              "value": "={{ $node['Dividir Leads'].json.id }}"
            },
            {
              "name": "mensagem",
              "value": "={{ $node['Enviar WhatsApp'].parameter.bodyParameters.parameters[1].value }}"
            },
            {
              "name": "status_envio",
              "value": "falha"
            },
            {
              "name": "codigo_erro",
              "value": "={{ $json.error?.message || 'Erro desconhecido' }}"
            },
            {
              "name": "resposta",
              "value": "={{ JSON.stringify($json) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "log-erro",
      "name": "Registrar Erro",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1250,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $env.SMTP_HOST }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "check-smtp",
      "name": "SMTP Configurado?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1450,
        400
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $env.SMTP_FROM }}",
        "toEmail": "={{ $env.SMTP_FROM }}",
        "subject": "Erro no envio WhatsApp",
        "text": "=Erro ao enviar para {{ $node['Dividir Leads'].json.nome }} ({{ $node['Dividir Leads'].json.telefone }})\n\nErro: {{ $json.error?.message }}\n\nTimestamp: {{ $now.toISO() }}",
        "options": {}
      },
      "id": "enviar-email-erro",
      "name": "Notificar Erro (Email)",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1650,
        300
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {},
      "id": "no-op-fim",
      "name": "Fim",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    }
  ],
  "connections": {
    "Cron Di\u00e1rio 09:00": {
      "main": [
        [
          {
            "node": "Buscar Leads (Supabase)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar Leads (Supabase)": {
      "main": [
        [
          {
            "node": "Verificar Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar Leads": {
      "main": [
        [
          {
            "node": "Dividir Leads",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fim",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dividir Leads": {
      "main": [
        [
          {
            "node": "Enviar WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar WhatsApp": {
      "main": [
        [
          {
            "node": "Registrar Envio (Log)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Registrar Erro",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Registrar Envio (Log)": {
      "main": [
        [
          {
            "node": "Marcar Ativado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Registrar Erro": {
      "main": [
        [
          {
            "node": "SMTP Configurado?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SMTP Configurado?": {
      "main": [
        [
          {
            "node": "Notificar Erro (Email)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "timezone": "America/Cuiaba"
  },
  "tags": []
}