{
  "name": "05 - Fan Monetization Events",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "stripe-webhook",
        "responseMode": "onReceived"
      },
      "name": "Webhook: Stripe Event",
      "type": "n8n-nodes-base.webhook",
      "position": [
        250,
        300
      ],
      "id": "node-stripe-webhook"
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.type }}",
              "operation": "contains",
              "value2": "payment_intent.succeeded"
            }
          ]
        }
      },
      "name": "If: Payment Succeeded",
      "type": "n8n-nodes-base.if",
      "position": [
        500,
        300
      ],
      "id": "node-if-payment"
    },
    {
      "parameters": {
        "jsCode": "// Parse metadata from Stripe payment\nconst data = $json.data.object;\nconst meta = data.metadata;\n\n// Calculate royalty splits\nconst gross = data.amount;\nconst platform_cut = Math.floor(gross * 0.15); // 15% platform fee\nconst creator_cut = Math.floor(gross * 0.70); // 70% to creator\nconst remixer_cut = meta.remixer_id ? Math.floor(gross * 0.15) : 0; // 15% to remixer if remix\n\nreturn [{\n  json: {\n    track_id: meta.track_id,\n    creator_id: meta.creator_id,\n    remixer_id: meta.remixer_id || null,\n    event_type: meta.event_type, // stream | stem_sale | remix_license | subscription\n    gross_cents: gross,\n    splits: {\n      platform: platform_cut,\n      creator: creator_cut,\n      remixer: remixer_cut\n    },\n    fan_id: meta.fan_id,\n    stripe_payment_id: data.id\n  }\n}];"
      },
      "name": "Code: Calculate Splits",
      "type": "n8n-nodes-base.code",
      "position": [
        750,
        300
      ],
      "id": "node-splits"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO royalty_events (id, track_id, event_type, gross_cents, splits, paid_at) VALUES (gen_random_uuid(), '{{ $json.track_id }}', '{{ $json.event_type }}', {{ $json.gross_cents }}, '{{ JSON.stringify($json.splits) }}'::jsonb, NOW())"
      },
      "name": "Neon: Log Royalty Event",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1000,
        200
      ],
      "id": "node-log-royalty"
    },
    {
      "parameters": {
        "url": "={{ $env.BRAIN_URL }}/events/core",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "event",
              "value": "royalty.earned"
            },
            {
              "name": "payload",
              "value": "={{ $json }}"
            }
          ]
        }
      },
      "name": "Brain: Emit royalty.earned",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1000,
        400
      ],
      "id": "node-brain-royalty"
    },
    {
      "parameters": {
        "url": "={{ $env.THINKZONE_API_URL }}/v1/tools",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "tool",
              "value": "notify_owner"
            },
            {
              "name": "message",
              "value": "\ud83d\udcb8 Royalty earned: ${{ ($json.gross_cents / 100).toFixed(2) }} on track {{ $json.track_id }} ({{ $json.event_type }}). Creator gets ${{ ($json.splits.creator / 100).toFixed(2) }}."
            }
          ]
        }
      },
      "name": "Notify Creator",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1250,
        300
      ],
      "id": "node-notify"
    }
  ],
  "connections": {
    "Webhook: Stripe Event": {
      "main": [
        [
          {
            "node": "If: Payment Succeeded",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If: Payment Succeeded": {
      "main": [
        [
          {
            "node": "Code: Calculate Splits",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Calculate Splits": {
      "main": [
        [
          {
            "node": "Neon: Log Royalty Event",
            "type": "main",
            "index": 0
          },
          {
            "node": "Brain: Emit royalty.earned",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Neon: Log Royalty Event": {
      "main": [
        [
          {
            "node": "Notify Creator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    "disco-bass",
    "monetization",
    "royalties"
  ]
}