{
  "name": "Reddit Approved to Drafts",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 10,
              "unit": "minutes"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": "1n8TfxlswtIVqHgRg7JOkUri6BQa9NYrTTGqDZXOmGxY",
        "sheetName": "Reddit_Inbox"
      },
      "name": "Read Inbox",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        440,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const rows = $json.data || [];\nconst approved = rows.filter(r => (r.status || '').toLowerCase() === 'approved');\nreturn approved.map(r => ({ json: r }));"
      },
      "name": "Filter Approved",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "operation": "lookup",
        "documentId": "1n8TfxlswtIVqHgRg7JOkUri6BQa9NYrTTGqDZXOmGxY",
        "sheetName": "Comment_Drafts",
        "lookupColumn": "post_id",
        "lookupValue": "={{$json.post_id}}"
      },
      "name": "Drafts Dedup Lookup",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        840,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const found = $json?.matchedRow || $json?.data || null;\nif (found && Array.isArray(found) && found.length > 0) return [];\nreturn [{ json: $input.first().json }];"
      },
      "name": "If Not Already Drafted",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1040,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json.post_url.endsWith('/') ? $json.post_url + '.json' : $json.post_url + '/.json'}}",
        "options": {
          "responseFormat": "json"
        }
      },
      "name": "Fetch Thread JSON",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const body = $json;\nconst post = body?.[0]?.data?.children?.[0]?.data || {};\nconst comments = body?.[1]?.data?.children || [];\n\nconst topComments = comments\n  .map(c => c?.data)\n  .filter(d => d && d.body)\n  .slice(0, 5)\n  .map(d => d.body);\n\nreturn [{\n  json: {\n    post_id: post.id || $input.first().json.post_id,\n    post_url: post.permalink ? 'https://www.reddit.com' + post.permalink : $input.first().json.post_url,\n    title: post.title || $input.first().json.title,\n    postText: post.selftext || '',\n    topComments\n  }\n}];"
      },
      "name": "Extract Post + Comments",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1440,
        300
      ]
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "messages": [
          {
            "role": "system",
            "content": "You are writing a Reddit comment. Be helpful. Plain language. No links. No selling. Do not say DM me. No hype. No emojis. End with a question. If uncertain, ask a clarifying question instead of guessing."
          },
          {
            "role": "user",
            "content": "Post title: {{$json.title}}\n\nPost text: {{$json.postText}}\n\nTop comments:\n- {{($json.topComments || []).join('\\n- ')}}\n\nWrite:\n1) A one-sentence summary of what the OP is really asking.\n2) 3 bullet points a local would mention.\n3) A 6-10 sentence draft reply. End with a question."
          }
        ]
      },
      "name": "OpenAI Draft",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 4,
      "position": [
        1640,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const text = $json?.choices?.[0]?.message?.content || '';\nconst parts = text.split(/\\n\\n+/);\nconst summary = parts[0] || '';\nconst bullets = parts.find(p => p.trim().startsWith('-')) || '';\nconst draft = parts.slice(1).join('\\n\\n');\n\nconst cleaned = draft\n  .replace(/https?:\\/\\/\\S+/g, '')\n  .replace(/\\bDM me\\b/gi, '')\n  .trim();\n\nreturn [{\n  json: {\n    post_id: $input.first().json.post_id,\n    post_url: $input.first().json.post_url,\n    summary_of_question: summary.trim(),\n    key_points_to_address: bullets.trim(),\n    draft_comment: cleaned,\n    needs_edit: 'yes',\n    posted: 'no',\n    created_at: new Date().toISOString()\n  }\n}];"
      },
      "name": "Parse + Clean",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1840,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": "1n8TfxlswtIVqHgRg7JOkUri6BQa9NYrTTGqDZXOmGxY",
        "sheetName": "Comment_Drafts",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "post_id": "={{$json.post_id}}",
            "post_url": "={{$json.post_url}}",
            "summary_of_question": "={{$json.summary_of_question}}",
            "key_points_to_address": "={{$json.key_points_to_address}}",
            "draft_comment": "={{$json.draft_comment}}",
            "needs_edit": "={{$json.needs_edit}}",
            "posted": "={{$json.posted}}",
            "created_at": "={{$json.created_at}}"
          }
        }
      },
      "name": "Append to Comment_Drafts",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        2040,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Read Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Inbox": {
      "main": [
        [
          {
            "node": "Filter Approved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Approved": {
      "main": [
        [
          {
            "node": "Drafts Dedup Lookup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Drafts Dedup Lookup": {
      "main": [
        [
          {
            "node": "If Not Already Drafted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Not Already Drafted": {
      "main": [
        [
          {
            "node": "Fetch Thread JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Thread JSON": {
      "main": [
        [
          {
            "node": "Extract Post + Comments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Post + Comments": {
      "main": [
        [
          {
            "node": "OpenAI Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Draft": {
      "main": [
        [
          {
            "node": "Parse + Clean",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse + Clean": {
      "main": [
        [
          {
            "node": "Append to Comment_Drafts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false
}