{
  "name": "fluxo-relatorio",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 11
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        0
      ],
      "id": "e12fee8d-1393-48df-b29e-6c25b8353f69",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "operation": "getAll",
        "tableId": "feedbacks",
        "limit": 100,
        "filters": {
          "conditions": [
            {
              "keyName": "data_recebimento",
              "condition": "gte",
              "keyValue": "={{ $today.startOf('day') }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        208,
        0
      ],
      "id": "1a6ad9c4-69ff-46bc-8550-53d9ce836883",
      "name": "Get many rows",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{\n  {\n    \"model\": \"llama-3.3-70b-versatile\",\n    \"response_format\": { \"type\": \"json_object\" },\n    \"messages\": [\n      {\n        \"role\": \"system\",\n        \"content\": \"Voc\u00ea \u00e9 um analista de feedbacks. Voc\u00ea vai receber listas de feedbacks e gere um JSON com um resumo de 1 frase explicando o motivo principal para cada sentimento. Se a lista estiver vazia, diga 'Nenhum feedback nesta categoria'. O formato DEVE ser JSON: { \\\"positivos\\\": \\\"...\\\", \\\"neutros\\\": \\\"...\\\", \\\"negativos\\\": \\\"...\\\" }\"\n      },\n      {\n        \"role\": \"user\",\n        \"content\": \"Positivos: \" + $('Code').item.json.textos_concat.positivos + \"\\n\\nNeutros: \" + $('Code').item.json.textos_concat.neutros + \"\\n\\nNegativos: \" + $('Code').item.json.textos_concat.negativos\n      }\n    ]\n  }\n}}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        624,
        0
      ],
      "id": "310128dc-145a-4051-8b77-99bebe051f40",
      "name": "HTTP Request",
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Pega todos os itens que vieram do Supabase\nconst items = $input.all();\n\n// Prepara as vari\u00e1veis\nlet positivos = 0;\nlet neutros = 0;\nlet negativos = 0;\n\nlet textosPositivos = [];\nlet textosNeutros = [];\nlet textosNegativos = [];\n\n// Loop para contar e separar os textos\nfor (const item of items) {\n  // Acessa o JSON do item\n  const dados = item.json;\n  const sent = (dados.sentimento || '').toLowerCase().trim();\n  \n  if (sent === 'positivo') {\n    positivos++;\n    textosPositivos.push(dados.feedback);\n  } else if (sent === 'negativo') {\n    negativos++;\n    textosNegativos.push(dados.feedback);\n  } else {\n    neutros++;\n    textosNeutros.push(dados.feedback);\n  }\n}\n\n// Retorna um \u00fanico item com o resumo estat\u00edstico\nreturn {\n  json: {\n    data_relatorio: new Date().toISOString().split('T')[0],\n    total: items.length,\n    stats: {\n      positivos,\n      neutros,\n      negativos\n    },\n    // Monta strings gigantes para a IA ler\n    textos_concat: {\n      positivos: textosPositivos.join(\" | \"),\n      neutros: textosNeutros.join(\" | \"),\n      negativos: textosNegativos.join(\" | \")\n    }\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        416,
        0
      ],
      "id": "e018f4aa-fc3a-4d8b-a3ac-0a294ace2715",
      "name": "Code"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://webhook.site/04f5c7ae-2487-4501-bff8-e684da93481a",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{\n  {\n    \"data\": $('Code').item.json.data_relatorio,\n    \"total_feedbacks\": $('Code').item.json.total,\n    \"sentimentos\": $('Code').item.json.stats,\n    \"resumo_qualitativo\": JSON.parse($json.choices[0].message.content)\n  }\n}}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        832,
        0
      ],
      "id": "6bd617ae-fb15-41b5-bafe-3ebfdbab7eac",
      "name": "HTTP Request1"
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get many rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many rows": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "0a523032-053c-45fd-bc3b-8840a374b200",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "5u8Ba7ceATQAxkx8UtcEB",
  "tags": []
}