{
  "name": "Vorlux AI | Gamification Rewards",
  "active": false,
  "nodes": [
    { "parameters": { "rule": { "interval": [{ "field": "hours", "hoursInterval": 1 }] } }, "id": "sched", "name": "Hourly Schedule", "type": "n8n-nodes-base.scheduleTrigger", "typeVersion": 1.2, "position": [220, 300] },
    { "parameters": { "url": "http://host.docker.internal:3010/api/community/leaderboard", "method": "GET", "options": { "timeout": 15000 } }, "id": "leaderboard", "name": "Fetch Leaderboard", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [460, 200] },
    { "parameters": { "url": "http://host.docker.internal:3010/api/activities", "method": "GET", "qs": { "period": "1h", "type": "engagement" }, "options": { "timeout": 15000 } }, "id": "activities", "name": "Fetch Recent Activities", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [460, 400] },
    { "parameters": { "mode": "runOnceForAllItems", "jsCode": "const leaderboard = $('Fetch Leaderboard').first().json.data || [];\nconst activities = $('Fetch Recent Activities').first().json.data || [];\nconst rewards = [];\nconst activityByUser = {};\nactivities.forEach(a => {\n  const uid = a.userId || a.user_id;\n  if (!activityByUser[uid]) activityByUser[uid] = { count: 0, types: new Set() };\n  activityByUser[uid].count++;\n  activityByUser[uid].types.add(a.type);\n});\nObject.entries(activityByUser).forEach(([userId, data]) => {\n  if (data.count >= 5) rewards.push({ userId, type: 'points', amount: data.count * 10, reason: 'Active engagement' });\n  if (data.types.size >= 3) rewards.push({ userId, type: 'badge', badge: 'multi-contributor', reason: 'Multiple activity types' });\n});\nreturn [{ json: { totalRewards: rewards.length, rewards, processedAt: new Date().toISOString() } }];" }, "id": "calc-rewards", "name": "Calculate Rewards", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [720, 300] },
    { "parameters": { "url": "http://host.docker.internal:3010/api/coins", "method": "POST", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ JSON.stringify({ rewards: $json.rewards }) }}", "options": { "timeout": 15000 } }, "id": "award", "name": "Award Rewards", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [960, 200] },
    { "parameters": { "method": "POST", "url": "={{$env.DISCORD_OPS_WEBHOOK}}", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json.totalRewards > 0 ? JSON.stringify({ content: '🏆 **Gamification Rewards**\\nRewards distributed: ' + $json.totalRewards }) : JSON.stringify({ content: '🏆 Gamification check — no new rewards this hour' }) }}" }, "id": "notify", "name": "Discord Notify", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [960, 400] }
  ],
  "connections": {
    "Hourly Schedule": { "main": [[{ "node": "Fetch Leaderboard", "type": "main", "index": 0 }, { "node": "Fetch Recent Activities", "type": "main", "index": 0 }]] },
    "Fetch Leaderboard": { "main": [[{ "node": "Calculate Rewards", "type": "main", "index": 0 }]] },
    "Fetch Recent Activities": { "main": [[{ "node": "Calculate Rewards", "type": "main", "index": 0 }]] },
    "Calculate Rewards": { "main": [[{ "node": "Award Rewards", "type": "main", "index": 0 }, { "node": "Discord Notify", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
