{
  "name": "02 - Distribution to Streaming Platforms",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "audio-processed",
        "responseMode": "onReceived"
      },
      "name": "Webhook: audio.processed",
      "type": "n8n-nodes-base.webhook",
      "position": [
        250,
        300
      ],
      "id": "node-trigger"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT distribute_to_streaming FROM track_settings WHERE track_id = '{{ $json.track_id }}'"
      },
      "name": "Check: Distribution Opt-In",
      "type": "n8n-nodes-base.postgres",
      "position": [
        500,
        300
      ],
      "id": "node-check-optin"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.distribute_to_streaming }}",
              "value2": true
            }
          ]
        }
      },
      "name": "If: Opted In",
      "type": "n8n-nodes-base.if",
      "position": [
        750,
        300
      ],
      "id": "node-if-optin"
    },
    {
      "parameters": {
        "url": "https://api.distrokid.com/v1/upload",
        "method": "POST",
        "authentication": "headerAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "track_url",
              "value": "={{ $json.r2_url_processed }}"
            },
            {
              "name": "title",
              "value": "={{ $json.title }}"
            },
            {
              "name": "artist",
              "value": "={{ $json.creator_name }}"
            },
            {
              "name": "genre",
              "value": "={{ $json.vibe_packet.primary_vibe }}"
            },
            {
              "name": "cover_art_url",
              "value": "={{ $json.cover_art_url }}"
            }
          ]
        }
      },
      "name": "DistroKid: Upload",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1000,
        200
      ],
      "id": "node-distrokid"
    },
    {
      "parameters": {
        "url": "={{ $env.BRAIN_URL }}/events/core",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "event",
              "value": "distribution.submitted"
            },
            {
              "name": "payload",
              "value": "={{ { track_id: $json.track_id, creator_id: $json.creator_id, platforms: ['spotify', 'apple_music', 'youtube_music'], eta_hours: 24 } }}"
            }
          ]
        }
      },
      "name": "Brain: Emit distribution.submitted",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1250,
        200
      ],
      "id": "node-brain-dist"
    },
    {
      "parameters": {
        "url": "={{ $env.THINKZONE_API_URL }}/v1/tools",
        "method": "POST",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "tool",
              "value": "notify_owner"
            },
            {
              "name": "message",
              "value": "Track '{{ $json.title }}' by {{ $json.creator_name }} submitted for distribution to Spotify, Apple Music, YouTube Music. ETA: 24 hours."
            }
          ]
        }
      },
      "name": "Notify: Creator",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1500,
        200
      ],
      "id": "node-notify"
    }
  ],
  "connections": {
    "Webhook: audio.processed": {
      "main": [
        [
          {
            "node": "Check: Distribution Opt-In",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check: Distribution Opt-In": {
      "main": [
        [
          {
            "node": "If: Opted In",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If: Opted In": {
      "main": [
        [
          {
            "node": "DistroKid: Upload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DistroKid: Upload": {
      "main": [
        [
          {
            "node": "Brain: Emit distribution.submitted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Brain: Emit distribution.submitted": {
      "main": [
        [
          {
            "node": "Notify: Creator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    "disco-bass",
    "distribution"
  ]
}