{
  "name": "Promo: Scripts \u2192 Buffer/YouTube",
  "nodes": [
    {
      "parameters": {
        "operation": "lookup",
        "sheetId": "REPLACE_SHEET_ID",
        "range": "SKUs!A1:N"
      },
      "id": "readSkus",
      "name": "Read SKUs (live)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 5,
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Filter live SKUs and craft prompts\nconst rows=$items().map(i=>i.json);\nconst headers=rows.shift();\nconst idxStatus=headers.indexOf('status');\nconst idxName=headers.indexOf('sku_name');\nconst idxLink=headers.indexOf('gumroad_link');\nconst lives=rows.filter(r=> (r[idxStatus]||'').toLowerCase()==='live');\nreturn lives.map(r=>({json:{name:r[idxName], link:r[idxLink]}}));"
      },
      "id": "filterLive",
      "name": "Filter Live",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "messages": "=[{ 'role':'system','content':'Return 10 short scripts as JSON array.' }, { 'role':'user','content': 'Write 10 scripts (20\u201330s) promoting {{ $json.name }}. Each includes: Hook (<=8 words), 3 tips, CTA: Link in bio. Include captions and b-roll cues. Return JSON array.' }]"
      },
      "id": "openai",
      "name": "OpenAI \u2192 10 scripts",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 4,
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Flatten scripts\nconst all=[];\nfor(const item of items){\n  let arr=item.json; if(typeof arr==='string'){ try{arr=JSON.parse(arr)}catch(e){arr=[]} }\n  for(const s of arr){ all.push({json:s}); }\n}\nreturn all;"
      },
      "id": "flatten",
      "name": "Flatten",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2
    },
    {
      "parameters": {
        "operation": "append",
        "sheetId": "REPLACE_SHEET_ID",
        "range": "Content!A2"
      },
      "id": "appendContent",
      "name": "Append to Content",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 5,
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Read SKUs (live)": {
      "main": [
        [
          {
            "node": "Filter Live",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Live": {
      "main": [
        [
          {
            "node": "OpenAI \u2192 10 scripts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI \u2192 10 scripts": {
      "main": [
        [
          {
            "node": "Flatten",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten": {
      "main": [
        [
          {
            "node": "Append to Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}