{
  "name": "Theo AI v4.2 Unificado - 18 Eventos HMAC + Fallback LLM + Meta CAPI + Retenção Matinal",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "instagram-saas-v42",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "webhook-18-events",
      "name": "Webhook SaaS v4.2 (18 Eventos HMAC)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        220,
        280
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.body.subAccountStatus || 'active'}}",
              "value2": "active"
            }
          ],
          "number": [
            {
              "value1": "={{$json.body.remainingCredits || 500}}",
              "operation": "larger",
              "value2": 0
            }
          ]
        }
      },
      "id": "guard-credits-sub",
      "name": "Trava Sub-Conta & Franquia 500 Créditos IA",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        460,
        280
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"gpt-4o-mini\",\n  \"messages\": [{\"role\": \"user\", \"content\": \"Responda ao lead: {{ $json.body?.payload?.text || \"Ola\" }}\"}]\n}",
        "options": {}
      },
      "id": "llm-primary-fallback",
      "name": "Agente IA + Co-Piloto (GPT Principal -> Gemini Fallback)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        720,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v20.0/me/messages",
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "recipient",
              "value": "={{ { id: $json.body.senderId } }}"
            },
            {
              "name": "message",
              "value": "={{ { text: $node['Agente IA + Co-Piloto (Fallback OpenAI -> Groq -> OpenRouter)'].json.message.content } }}"
            }
          ]
        }
      },
      "id": "meta-dm-dispatch",
      "name": "Disparo Direct + Meta CAPI ROI Tracker",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        980,
        200
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * *"
            }
          ]
        }
      },
      "id": "cron-morning-retention",
      "name": "Automação de Retenção Matinal (08:00 AM) + Virais",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        220,
        520
      ]
    }
  ],
  "connections": {
    "Webhook SaaS v4.2 (18 Eventos HMAC)": {
      "main": [
        [
          {
            "node": "Trava Sub-Conta & Franquia 500 Créditos IA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trava Sub-Conta & Franquia 500 Créditos IA": {
      "main": [
        [
          {
            "node": "Agente IA + Co-Piloto (Fallback OpenAI -> Groq -> OpenRouter)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Agente IA + Co-Piloto (Fallback OpenAI -> Groq -> OpenRouter)": {
      "main": [
        [
          {
            "node": "Disparo Direct + Meta CAPI ROI Tracker",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}