{
  "nodes": [
    {
      "id": "2e37cecf-fcff-43f5-a547-7f15f385f3e5",
      "name": "Webhook Admin",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "wm-admin-v3",
        "responseMode": "lastNode",
        "options": {}
      }
    },
    {
      "id": "785e284c-6f97-4d09-87ae-e041da3712c8",
      "name": "Get Admin Token",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        220,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT value#>>'{}' AS token FROM wm_config WHERE key='secrets.admin_token' LIMIT 1;",
        "options": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "81d08322-8a43-4041-b659-18242293d59e",
      "name": "Auth + Parse",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const headers = $('Webhook Admin').item.json.headers || {};\nconst provided = String(headers['x-admin-token'] || headers['X-Admin-Token'] || '').trim();\nconst expected = String($json.token || '').trim();\nif (!expected || expected === 'CHANGE_ME_BEFORE_PROD') {\n  return { json: { ok: false, status: 500, error: 'admin_token_not_configured' } };\n}\nif (provided !== expected) {\n  return { json: { ok: false, status: 401, error: 'unauthorized' } };\n}\nconst body = $('Webhook Admin').item.json.body || {};\nconst action = String(body.action || '').toLowerCase();\nconst tel = String(body.tel || '').replace(/\\D/g, '');\nconst params = body.params || {};\nconst VALID = ['get_state','reset','pause_bot','resume_bot','list_leads'];\nif (!VALID.includes(action)) {\n  return { json: { ok: false, status: 400, error: 'invalid_action', action } };\n}\nreturn { json: { ok: true, action, tel, params } };"
      }
    },
    {
      "id": "77857558-988b-4d33-8586-045e8af6cd2a",
      "name": "IF Authorized",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        660,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c_ok",
              "leftValue": "={{ $json.ok }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "fdb25b5f-1440-4221-8ee0-393875de31d8",
      "name": "Switch Action",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        880,
        200
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "get_state",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "c1"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "get_state"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "reset",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "c2"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "reset"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "pause_bot",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "c3"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "pause_bot"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "resume_bot",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "c4"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "resume_bot"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.action }}",
                    "rightValue": "list_leads",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "c5"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "list_leads"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "9011941b-f728-4598-a69a-30bd71b3ad5b",
      "name": "Proxy Get State",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://ia-n8n.clalha.easypanel.host/webhook/wm-se-v3",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ tel: $json.tel, action: 'get' }) }}",
        "options": {
          "timeout": 10000
        }
      }
    },
    {
      "id": "4b531544-fc37-4751-b12b-3441824e541c",
      "name": "Proxy Reset",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        100
      ],
      "parameters": {
        "method": "POST",
        "url": "https://ia-n8n.clalha.easypanel.host/webhook/wm-se-v3",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ tel: $json.tel, action: 'clear', params: { scope: 'state_only' } }) }}",
        "options": {
          "timeout": 10000
        }
      }
    },
    {
      "id": "b2690a40-0f55-4b47-9daa-a36d4553e111",
      "name": "Proxy Pause Bot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "https://ia-n8n.clalha.easypanel.host/webhook/wm-se-v3",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ tel: $json.tel, action: 'pause_bot', params: { duration_ms: $json.params.duration_ms || 3600000, reason: $json.params.reason || 'admin_pause' } }) }}",
        "options": {
          "timeout": 10000
        }
      }
    },
    {
      "id": "a300cb53-0f39-4cc4-a266-63ba32285da0",
      "name": "Proxy Resume Bot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "https://ia-n8n.clalha.easypanel.host/webhook/wm-se-v3",
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "application/json",
        "body": "={{ JSON.stringify({ tel: $json.tel, action: 'resume_bot' }) }}",
        "options": {
          "timeout": 10000
        }
      }
    },
    {
      "id": "7d486169-c3bf-4cb1-b6a5-382b71549495",
      "name": "Redis KEYS",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        1100,
        400
      ],
      "parameters": {
        "operation": "keys",
        "keyPattern": "lead:*:state",
        "options": {}
      },
      "credentials": {
        "redis": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "1c8529b5-3f54-44b2-9f2a-ba726a287fb3",
      "name": "Format Leads",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1320,
        400
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nconst phones = [];\nfor (const it of items) {\n  const j = it.json || {};\n  for (const key of Object.keys(j)) {\n    const m = String(key).match(/^lead:(\\d+):state$/);\n    if (m) phones.push(m[1]);\n  }\n}\nreturn [{ json: { ok: true, count: phones.length, phones } }];"
      }
    },
    {
      "id": "1d177318-e60e-4eac-84c2-329b50e3243f",
      "name": "Respond Unauthorized",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        500
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "return { json: { ok: false, error: $json.error, status: $json.status || 401 } };"
      }
    }
  ],
  "connections": {
    "Webhook Admin": {
      "main": [
        [
          {
            "node": "Get Admin Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Admin Token": {
      "main": [
        [
          {
            "node": "Auth + Parse",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Auth + Parse": {
      "main": [
        [
          {
            "node": "IF Authorized",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Authorized": {
      "main": [
        [
          {
            "node": "Switch Action",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Unauthorized",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch Action": {
      "main": [
        [
          {
            "node": "Proxy Get State",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Proxy Reset",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Proxy Pause Bot",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Proxy Resume Bot",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Redis KEYS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Redis KEYS": {
      "main": [
        [
          {
            "node": "Format Leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}