{
  "name": "Enrutamiento Inteligente de Tickets de Soporte",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "soporte-whatsapp",
        "options": {}
      },
      "id": "b8c9d0e1-0008-4a01-b001-ef1234567001",
      "name": "Webhook - WhatsApp Soporte",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 150]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        }
      },
      "id": "b8c9d0e1-0008-4a02-b002-ef1234567002",
      "name": "Gmail Trigger - Correos Soporte",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [250, 350],
      "credentials": {
        "gmailOAuth2": {
          "id": "",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "soporte-formulario-web",
        "options": {}
      },
      "id": "b8c9d0e1-0008-4a03-b003-ef1234567003",
      "name": "Webhook - Formulario Web",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 550]
    },
    {
      "parameters": {
        "mode": "multiplex",
        "mergeByFields": {},
        "options": {}
      },
      "id": "b8c9d0e1-0008-4a04-b004-ef1234567004",
      "name": "Merge - Unificar Fuentes",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [500, 350]
    },
    {
      "parameters": {
        "functionCode": "const message = (items[0].json.mensaje || items[0].json.text || items[0].json.body || '').toLowerCase();\nconst canal = items[0].json.canal || 'web';\n\nlet prioridad = 'media';\nlet departamento = 'soporte_tecnico';\n\n// Determinar prioridad\nconst urgentWords = ['urgente', 'emergencia', 'caído', 'no funciona', 'error grave', 'pérdida'];\nconst lowWords = ['consulta', 'pregunta', 'información', 'horario'];\n\nif (urgentWords.some(w => message.includes(w))) {\n  prioridad = 'alta';\n} else if (lowWords.some(w => message.includes(w))) {\n  prioridad = 'baja';\n}\n\n// Determinar departamento\nif (message.includes('factura') || message.includes('pago') || message.includes('cobro') || message.includes('precio')) {\n  departamento = 'facturacion';\n} else if (message.includes('comprar') || message.includes('cotización') || message.includes('producto')) {\n  departamento = 'ventas';\n}\n\nconst ticketId = 'TK-' + Date.now().toString().slice(-6);\n\nreturn [{\n  json: {\n    ticketId,\n    nombre: items[0].json.nombre || items[0].json.from || 'Sin nombre',\n    correo: items[0].json.correo || items[0].json.email || '',\n    mensaje: items[0].json.mensaje || items[0].json.text || items[0].json.body,\n    canal,\n    prioridad,\n    departamento,\n    fecha: new Date().toISOString(),\n    estado: 'Abierto'\n  }\n}];"
      },
      "id": "b8c9d0e1-0008-4a05-b005-ef1234567005",
      "name": "Function - Clasificar Ticket",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [750, 350]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Tickets Soporte"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "b8c9d0e1-0008-4a06-b006-ef1234567006",
      "name": "Google Sheets - Registrar Ticket",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1000, 350],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "channel": "={{ $json.departamento === 'ventas' ? '#ventas' : $json.departamento === 'facturacion' ? '#facturacion' : '#soporte-tecnico' }}",
        "text": "=🎫 *Nuevo Ticket: {{ $json.ticketId }}*\n\n*Prioridad:* {{ $json.prioridad === 'alta' ? '🔴 ALTA' : $json.prioridad === 'media' ? '🟡 Media' : '🟢 Baja' }}\n*Canal:* {{ $json.canal }}\n*Cliente:* {{ $json.nombre }}\n*Mensaje:* {{ $json.mensaje }}\n\n_Fecha: {{ $json.fecha }}_",
        "otherOptions": {
          "mrkdwn": true
        }
      },
      "id": "b8c9d0e1-0008-4a07-b007-ef1234567007",
      "name": "Slack - Notificar Departamento",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2,
      "position": [1250, 350],
      "credentials": {
        "slackOAuth2Api": {
          "id": "",
          "name": "Slack account"
        }
      }
    }
  ],
  "connections": {
    "Webhook - WhatsApp Soporte": {
      "main": [
        [
          {
            "node": "Merge - Unificar Fuentes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger - Correos Soporte": {
      "main": [
        [
          {
            "node": "Merge - Unificar Fuentes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Formulario Web": {
      "main": [
        [
          {
            "node": "Merge - Unificar Fuentes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge - Unificar Fuentes": {
      "main": [
        [
          {
            "node": "Function - Clasificar Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function - Clasificar Ticket": {
      "main": [
        [
          {
            "node": "Google Sheets - Registrar Ticket",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Registrar Ticket": {
      "main": [
        [
          {
            "node": "Slack - Notificar Departamento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "customer-service"
    },
    {
      "name": "tickets"
    },
    {
      "name": "soporte"
    }
  ]
}
