{
  "name": "VMP | Lead CRM + Alerta Ventas",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "vmp-lead-crm",
        "responseMode": "onReceived",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "webhook-lead",
      "name": "Webhook - Lead Creado",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Verificar firma HMAC-SHA256 del webhook\nconst crypto = require('crypto');\nconst secret = $env.VMP_WEBHOOK_SECRET || 'vmp-n8n-default-secret-change-me';\nconst signature = $input.first().headers['x-vmp-signature'] || '';\nconst body = JSON.stringify($input.first().body);\nconst expected = 'sha256=' + crypto.createHmac('sha256', secret).update(body).digest('hex');\n\nif (signature && expected !== signature) {\n  throw new Error('Firma HMAC inv\u00e1lida \u2014 request rechazado');\n}\n\nconst lead = $input.first().body.data;\nreturn [{ json: { \n  ...lead, \n  isHighPriority: (lead.quantity || 0) >= 30,\n  timestamp: new Date().toISOString()\n}}];"
      },
      "id": "verify-hmac",
      "name": "Verificar Firma & Parsear",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        470,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isHighPriority }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-high-priority",
      "name": "\u00bfAlta Prioridad? (\u226530 alumnos)",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        690,
        300
      ]
    },
    {
      "parameters": {
        "resource": "spreadsheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "value": "__GOOGLE_SHEET_ID__",
          "__rl": true,
          "mode": "id"
        },
        "sheetName": {
          "value": "Leads",
          "__rl": true,
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {
            "ID": "={{ $json.id }}",
            "Empresa": "={{ $json.empresa }}",
            "Nombre Contacto": "={{ $json.nombre }}",
            "Email": "={{ $json.email }}",
            "Tel\u00e9fono": "={{ $json.telefono }}",
            "Cantidad Alumnos": "={{ $json.quantity }}",
            "Curso": "={{ $json.course }}",
            "Modalidad": "={{ $json.modality }}",
            "Precio Total": "={{ $json.totalPrice }}",
            "Prioridad": "={{ $json.isHighPriority ? 'ALTA \ud83d\udd25' : 'NORMAL' }}",
            "Estado": "NUEVO",
            "Fecha": "={{ $json.timestamp }}"
          }
        }
      },
      "id": "google-sheets-lead",
      "name": "Google Sheets \u2014 Agregar Lead",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        910,
        400
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "=https://api.twilio.com/2010-04-01/Accounts/{{ $env.TWILIO_ACCOUNT_SID }}/Messages.json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "twilioApi",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "From",
              "value": "={{ $env.TWILIO_WHATSAPP_FROM }}"
            },
            {
              "name": "To",
              "value": "={{ $env.TWILIO_SALES_PHONE }}"
            },
            {
              "name": "Body",
              "value": "=\ud83d\udd25 *LEAD PRIORITARIO VMP*\n\n\ud83c\udfe2 *Empresa:* {{ $json.empresa }}\n\ud83d\udc64 *Contacto:* {{ $json.nombre }}\n\ud83d\udce7 *Email:* {{ $json.email }}\n\ud83d\udcf1 *Tel:* {{ $json.telefono }}\n\n\ud83d\udcda *Curso:* {{ $json.course }}\n\ud83d\udc65 *Alumnos:* {{ $json.quantity }}\n\ud83d\udcb0 *Total:* ${{ $json.totalPrice }}\n\n\u26a1 Cliente con m\u00e1s de 30 alumnos. \u00a1Llamar YA!"
            }
          ]
        }
      },
      "id": "twilio-alert",
      "name": "WhatsApp \u2014 Alerta Ventas Urgente",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        910,
        200
      ],
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "amount": 3,
        "unit": "days"
      },
      "id": "wait-3-days",
      "name": "Esperar 3 d\u00edas",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1,
      "position": [
        1130,
        400
      ]
    },
    {
      "parameters": {
        "fromEmail": "ventas@vmp-edtech.com",
        "toEmail": "={{ $json.email }}",
        "subject": "\u00bfPudimos ayudarte? \u2014 VMP EDTECH",
        "emailType": "html",
        "message": "=<h2>Hola {{ $json.nombre }},</h2><p>Hace 3 d\u00edas pediste informaci\u00f3n sobre capacitaci\u00f3n para <strong>{{ $json.empresa }}</strong>.</p><p>Queremos asegurarnos de que recibiste toda la informaci\u00f3n que necesitabas. \u00bfPodemos ayudarte con algo m\u00e1s?</p><p>Estamos a tu disposici\u00f3n,<br>Equipo VMP EDTECH</p>"
      },
      "id": "followup-email",
      "name": "Email Seguimiento 3 d\u00edas",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1350,
        400
      ]
    }
  ],
  "connections": {
    "Webhook - Lead Creado": {
      "main": [
        [
          {
            "node": "Verificar Firma & Parsear",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar Firma & Parsear": {
      "main": [
        [
          {
            "node": "\u00bfAlta Prioridad? (\u226530 alumnos)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00bfAlta Prioridad? (\u226530 alumnos)": {
      "main": [
        [
          {
            "node": "WhatsApp \u2014 Alerta Ventas Urgente",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Google Sheets \u2014 Agregar Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "WhatsApp \u2014 Alerta Ventas Urgente": {
      "main": [
        [
          {
            "node": "Google Sheets \u2014 Agregar Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets \u2014 Agregar Lead": {
      "main": [
        [
          {
            "node": "Esperar 3 d\u00edas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Esperar 3 d\u00edas": {
      "main": [
        [
          {
            "node": "Email Seguimiento 3 d\u00edas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "VMP-EDTECH"
    }
  ]
}