{
  "name": "Panel Responder Manual",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "panel-responder",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "w1",
      "name": "Webhook Responder",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const b = $json.body || {};\nif (b.secret !== 'jeancrg2026panel' || !b.contacto_id || !b.texto) throw new Error('Parametros invalidos: se requiere secret, contacto_id y texto');\nreturn [{ json: { ok: true, contacto_id: b.contacto_id, texto: b.texto } }];"
      },
      "id": "w2",
      "name": "Validar",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "tableId": "contactos",
        "options": {
          "select": "*"
        }
      },
      "id": "w3",
      "name": "Traer Contacto",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 2,
      "position": [
        400,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const cid = $('Validar').item.json.contacto_id;\nconst c = $('Traer Contacto').all().flatMap(i => i.json).flat(1).find(x => x && x.id === cid);\nif (!c || !c.telefono) throw new Error('Contacto no encontrado');\nreturn [{ json: { telefono: c.telefono, texto: $('Validar').item.json.texto, contacto_id: cid } }];"
      },
      "id": "w4",
      "name": "Buscar Telefono",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "operation": "sendMessage",
        "phoneNumberId": "1309447485585504",
        "recipientPhoneNumber": "={{ $json.telefono }}"
      },
      "onError": "continueRegularOutput",
      "id": "w5",
      "name": "Enviar WhatsApp",
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1.1,
      "position": [
        800,
        300
      ],
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "tableId": "conversaciones",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "contacto_id",
              "fieldValue": "={{ $('Buscar Telefono').item.json.contacto_id }}"
            },
            {
              "fieldId": "tenant_id",
              "fieldValue": "d2f0d3a0-0000-4000-8000-000000000001"
            },
            {
              "fieldId": "role",
              "fieldValue": "assistant"
            },
            {
              "fieldId": "content",
              "fieldValue": "={{ $('Buscar Telefono').item.json.texto }}"
            },
            {
              "fieldId": "derivado_a_humano",
              "fieldValue": "true"
            }
          ]
        }
      },
      "id": "w6",
      "name": "Guardar Respuesta Humana",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const err = $json.error;\nif (err) {\n  const s = typeof err === 'string' ? err : JSON.stringify(err);\n  if (/131047|131048|window|template/i.test(s)) {\n    return [{ json: { ok: false, error: 'ventana-24h-cerrada: el cliente debe escribirte primero desde su WhatsApp (regla de Meta). Escribele desde tu personal o esper\u00e1 su mensaje.' } }];\n  }\n  return [{ json: { ok: false, error: 'envio-fallido: ' + (typeof err === 'string' ? err : (err.message || s)) } }];\n}\nreturn [{ json: { ok: true } }];"
      },
      "id": "w7",
      "name": "Formatear Resultado",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Responder": {
      "main": [
        [
          {
            "node": "Validar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validar": {
      "main": [
        [
          {
            "node": "Traer Contacto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Traer Contacto": {
      "main": [
        [
          {
            "node": "Buscar Telefono",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar Telefono": {
      "main": [
        [
          {
            "node": "Enviar WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar WhatsApp": {
      "main": [
        [
          {
            "node": "Guardar Respuesta Humana",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardar Respuesta Humana": {
      "main": [
        [
          {
            "node": "Formatear Resultado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}