{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-webhook",
        "responseFormat": "json"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const from = $json.From;\nconst payload = $json.Interactive?.button_reply?.id || (/oi/i.test($json.Body) && 'menu');\nreturn [{ json: { from, action: payload } }];"
      },
      "name": "Parse Button",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "rules": [
          {
            "value1": "={{$json.action}}",
            "operation": "equal",
            "value2": "menu"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "equal",
            "value2": "liberar_he"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "equal",
            "value2": "bloquear_maquina"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "equal",
            "value2": "desbloquear_maquina"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "contains",
            "value2": "dept_"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "contains",
            "value2": "member_"
          },
          {
            "value1": "={{$json.action}}",
            "operation": "equal",
            "value2": "confirm_yes"
          }
        ]
      },
      "name": "Switch Action",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "url": "=https://graph.facebook.com/v15.0/{{ $credentials.whatsappPhoneNumberId }}/messages",
        "method": "POST",
        "jsonParameters": true,
        "options": {},
        "headers": {
          "Authorization": "Bearer {{ $credentials.whatsappToken }}",
          "Content-Type": "application/json"
        },
        "bodyParametersJson": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": {{$json.from}},\n  \"type\": \"interactive\",\n  \"interactive\": {\n    \"type\": \"button\",\n    \"body\": { \"text\": \"Ol\u00e1! Escolha uma a\u00e7\u00e3o:\" },\n    \"action\": {\n      \"buttons\": [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"liberar_he\", \"title\": \"Liberar HE\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"bloquear_maquina\", \"title\": \"Bloquear m\u00e1quina\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"desbloquear_maquina\", \"title\": \"Desbloquear m\u00e1quina\" } }\n      ]\n    }\n  }\n}"
      },
      "name": "Send Initial Menu",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        800,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://sua-api.local/api/registerSelection",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "={ \"user\": {{$json.from}}, \"stage\": \"initial\", \"selection\": \"{{$json.action}}\" }"
      },
      "name": "Register Initial",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1000,
        200
      ]
    },
    {
      "parameters": {
        "url": "=https://graph.facebook.com/v15.0/{{ $credentials.whatsappPhoneNumberId }}/messages",
        "method": "POST",
        "jsonParameters": true,
        "options": {},
        "headers": {
          "Authorization": "Bearer {{ $credentials.whatsappToken }}",
          "Content-Type": "application/json"
        },
        "bodyParametersJson": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": {{$json.from}},\n  \"type\": \"interactive\",\n  \"interactive\": {\n    \"type\": \"button\",\n    \"body\": { \"text\": \"Escolha o departamento:\" },\n    \"action\": {\n      \"buttons\": [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"dept_ti\", \"title\": \"TI\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"dept_financeiro\", \"title\": \"Financeiro\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"dept_rh\", \"title\": \"RH\" } }\n      ]\n    }\n  }\n}"
      },
      "name": "Send Department Menu",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1200,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://sua-api.local/api/registerSelection",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "={ \"user\": {{$json.from}}, \"stage\": \"department\", \"selection\": \"{{$json.action}}\" }"
      },
      "name": "Register Department",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1400,
        200
      ]
    },
    {
      "parameters": {
        "url": "=https://graph.facebook.com/v15.0/{{ $credentials.whatsappPhoneNumberId }}/messages",
        "method": "POST",
        "jsonParameters": true,
        "options": {},
        "headers": {
          "Authorization": "Bearer {{ $credentials.whatsappToken }}",
          "Content-Type": "application/json"
        },
        "bodyParametersJson": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": {{$json.from}},\n  \"type\": \"interactive\",\n  \"interactive\": {\n    \"type\": \"button\",\n    \"body\": { \"text\": \"Escolha o membro:\" },\n    \"action\": {\n      \"buttons\": {{$json.action === 'dept_ti' ? [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_alice\", \"title\": \"Alice\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_bob\", \"title\": \"Bob\" } }\n      ] : $json.action === 'dept_financeiro' ? [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_carla\", \"title\": \"Carla\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_daniel\", \"title\": \"Daniel\" } }\n      ] : [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_eva\", \"title\": \"Eva\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"member_frank\", \"title\": \"Frank\" } }\n      ]}}}\n}"
      },
      "name": "Send Member Menu",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1600,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://sua-api.local/api/registerSelection",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "={ \"user\": {{$json.from}}, \"stage\": \"member\", \"selection\": \"{{$json.action}}\" }"
      },
      "name": "Register Member",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1800,
        200
      ]
    },
    {
      "parameters": {
        "url": "=https://graph.facebook.com/v15.0/{{ $credentials.whatsappPhoneNumberId }}/messages",
        "method": "POST",
        "jsonParameters": true,
        "options": {},
        "headers": {
          "Authorization": "Bearer {{ $credentials.whatsappToken }}",
          "Content-Type": "application/json"
        },
        "bodyParametersJson": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": {{$json.from}},\n  \"type\": \"interactive\",\n  \"interactive\": {\n    \"type\": \"button\",\n    \"body\": { \"text\": \"Confirma a a\u00e7\u00e3o escolhida?\" },\n    \"action\": {\n      \"buttons\": [\n        { \"type\": \"reply\", \"reply\": { \"id\": \"confirm_yes\", \"title\": \"Confirmar\" } },\n        { \"type\": \"reply\", \"reply\": { \"id\": \"menu\", \"title\": \"Cancelar\" } }\n      ]\n    }\n  }\n}"
      },
      "name": "Send Confirmation",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        2000,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://sua-api.local/api/executeAction",
        "method": "POST",
        "jsonParameters": true,
        "bodyParametersJson": "={ \"user\": {{$json.from}}, \"confirm\": true }"
      },
      "name": "Process Action",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        2200,
        200
      ]
    },
    {
      "parameters": {
        "responseCode": 200
      },
      "name": "HTTP Response",
      "type": "n8n-nodes-base.httpResponse",
      "typeVersion": 1,
      "position": [
        2400,
        200
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Parse Button",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Button": {
      "main": [
        [
          {
            "node": "Switch Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Action": {
      "main": [
        [
          {
            "node": "Send Initial Menu",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Register Initial",
            "type": "main",
            "index": 1
          }
        ],
        [
          {
            "node": "Register Initial",
            "type": "main",
            "index": 2
          }
        ],
        [
          {
            "node": "Register Initial",
            "type": "main",
            "index": 3
          }
        ],
        [
          {
            "node": "Register Department",
            "type": "main",
            "index": 4
          }
        ],
        [
          {
            "node": "Register Member",
            "type": "main",
            "index": 5
          }
        ],
        [
          {
            "node": "Process Action",
            "type": "main",
            "index": 6
          }
        ]
      ]
    },
    "Send Initial Menu": {
      "main": [
        [
          {
            "node": "HTTP Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register Initial": {
      "main": [
        [
          {
            "node": "Send Department Menu",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Department Menu": {
      "main": [
        [
          {
            "node": "HTTP Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register Department": {
      "main": [
        [
          {
            "node": "Send Member Menu",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Member Menu": {
      "main": [
        [
          {
            "node": "HTTP Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register Member": {
      "main": [
        [
          {
            "node": "Send Confirmation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Confirmation": {
      "main": [
        [
          {
            "node": "HTTP Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Action": {
      "main": [
        [
          {
            "node": "HTTP Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}