{
  "name": "07 - Predictive Virality Alert",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 30
            }
          ]
        }
      },
      "name": "Cron: Every 30 Min",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        250,
        300
      ],
      "id": "node-cron"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, title, creator_id, play_count, like_count, vibe_packet, created_at, (play_count / GREATEST(EXTRACT(EPOCH FROM (NOW() - created_at)) / 3600, 1)) AS plays_per_hour FROM tracks WHERE created_at > NOW() - INTERVAL '48 hours' AND (play_count / GREATEST(EXTRACT(EPOCH FROM (NOW() - created_at)) / 3600, 1)) > 50 ORDER BY plays_per_hour DESC LIMIT 20"
      },
      "name": "Neon: Detect Play Velocity Spikes",
      "type": "n8n-nodes-base.postgres",
      "position": [
        500,
        300
      ],
      "id": "node-detect"
    },
    {
      "parameters": {
        "batchSize": 1
      },
      "name": "Split Tracks",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        750,
        300
      ],
      "id": "node-split"
    },
    {
      "parameters": {
        "url": "={{ $env.THINKZONE_API_URL }}/v1/analyze",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "prompt",
              "value": "Analyze this music track's virality potential on a scale of 0-100. Return only a JSON object with: { virality_score: number, reason: string, recommended_actions: string[] }. Track data: {{ JSON.stringify($json) }}"
            }
          ]
        }
      },
      "name": "ThinkZone API: Virality Score",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1000,
        300
      ],
      "id": "node-virality"
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.virality_score }}",
              "operation": "larger",
              "value2": 80
            }
          ]
        }
      },
      "name": "If: Score > 80",
      "type": "n8n-nodes-base.if",
      "position": [
        1250,
        300
      ],
      "id": "node-if-viral"
    },
    {
      "parameters": {
        "url": "={{ $env.BRAIN_URL }}/events/core",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "event",
              "value": "discovery.viral_candidate"
            },
            {
              "name": "payload",
              "value": "={{ { track_id: $json.id, virality_score: $json.virality_score, push_to_discovery: true, feature_in_charts: true } }}"
            }
          ]
        }
      },
      "name": "Brain: Push to Discovery",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1500,
        200
      ],
      "id": "node-push-discovery"
    },
    {
      "parameters": {
        "url": "={{ $env.THINKZONE_API_URL }}/v1/tools",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "tool",
              "value": "notify_owner"
            },
            {
              "name": "message",
              "value": "\ud83d\udd25 VIRAL ALERT: '{{ $json.title }}' scoring {{ $json.virality_score }}/100. Plays/hr: {{ $json.plays_per_hour }}. Pushed to Discovery. Reason: {{ $json.reason }}"
            }
          ]
        }
      },
      "name": "Notify: Viral Alert",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1500,
        400
      ],
      "id": "node-notify"
    }
  ],
  "connections": {
    "Cron: Every 30 Min": {
      "main": [
        [
          {
            "node": "Neon: Detect Play Velocity Spikes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Neon: Detect Play Velocity Spikes": {
      "main": [
        [
          {
            "node": "Split Tracks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Tracks": {
      "main": [
        [
          {
            "node": "ThinkZone API: Virality Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ThinkZone API: Virality Score": {
      "main": [
        [
          {
            "node": "If: Score > 80",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If: Score > 80": {
      "main": [
        [
          {
            "node": "Brain: Push to Discovery",
            "type": "main",
            "index": 0
          },
          {
            "node": "Notify: Viral Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    "disco-bass",
    "analytics",
    "discovery",
    "virality"
  ]
}