{
  "name": "Social Media Scheduler",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "social-schedule",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook - Schedule Content",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extract content from Maximo webhook\nconst input = $input.first().json;\n\n// Validate required fields\nif (!input.content) {\n  throw new Error('Content is required');\n}\n\nconst content = input.content;\nconst scheduledTimes = input.scheduledTimes || {};\nconst platforms = input.platforms || ['twitter', 'linkedin', 'facebook'];\nconst tone = input.tone || 'professional';\nconst industry = input.industry || '';\n\n// Default scheduling: spread posts throughout the day\nconst now = new Date();\nconst defaultSchedule = {\n  twitter: new Date(now.getTime() + 1 * 60 * 60 * 1000).toISOString(),\n  linkedin: new Date(now.getTime() + 2 * 60 * 60 * 1000).toISOString(),\n  facebook: new Date(now.getTime() + 3 * 60 * 60 * 1000).toISOString(),\n  instagram: new Date(now.getTime() + 4 * 60 * 60 * 1000).toISOString()\n};\n\nreturn {\n  content,\n  platforms,\n  tone,\n  industry,\n  scheduledTimes: {\n    ...defaultSchedule,\n    ...scheduledTimes\n  },\n  requestId: input.requestId || `req_${Date.now()}`\n};"
      },
      "id": "prepare-content",
      "name": "Prepare Content",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$env.MAXIMO_API_URL}}/api/n8n/social/generate-all",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"{{ $json.content }}\",\n  \"platforms\": {{ JSON.stringify($json.platforms) }},\n  \"tone\": \"{{ $json.tone }}\",\n  \"industry\": \"{{ $json.industry }}\"\n}",
        "options": {}
      },
      "id": "generate-posts",
      "name": "Generate Posts via Maximo",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Combine generated posts with scheduling info\nconst generated = $input.first().json;\nconst contentData = $('Prepare Content').first().json;\n\nconst posts = [];\n\n// Twitter post\nif (generated.twitter && contentData.platforms.includes('twitter')) {\n  posts.push({\n    platform: 'twitter',\n    text: generated.twitter.text + (generated.twitter.hashtags?.length ? ' ' + generated.twitter.hashtags.map(h => '#' + h).join(' ') : ''),\n    scheduledTime: contentData.scheduledTimes.twitter,\n    status: 'scheduled'\n  });\n}\n\n// LinkedIn post\nif (generated.linkedin && contentData.platforms.includes('linkedin')) {\n  posts.push({\n    platform: 'linkedin',\n    text: generated.linkedin.text,\n    scheduledTime: contentData.scheduledTimes.linkedin,\n    status: 'scheduled'\n  });\n}\n\n// Facebook post\nif (generated.facebook && contentData.platforms.includes('facebook')) {\n  posts.push({\n    platform: 'facebook',\n    text: generated.facebook.text,\n    scheduledTime: contentData.scheduledTimes.facebook,\n    status: 'scheduled'\n  });\n}\n\n// Instagram post\nif (generated.instagram && contentData.platforms.includes('instagram')) {\n  posts.push({\n    platform: 'instagram',\n    text: generated.instagram.text + (generated.instagram.hashtags?.length ? '\\n\\n' + generated.instagram.hashtags.map(h => '#' + h).join(' ') : ''),\n    scheduledTime: contentData.scheduledTimes.instagram,\n    status: 'scheduled'\n  });\n}\n\nreturn posts.map(post => ({ json: post }));"
      },
      "id": "prepare-scheduled-posts",
      "name": "Prepare Scheduled Posts",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "twitter-condition",
              "leftValue": "={{ $json.platform }}",
              "rightValue": "twitter",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "twitter-switch",
      "name": "Route by Platform",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1120,
        300
      ],
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "url": "={{$env.BUFFER_API_URL || 'https://api.bufferapp.com/1'}}/updates/create.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"text\": \"{{ $json.text }}\",\n  \"profile_ids\": [\"{{ $env.BUFFER_TWITTER_PROFILE_ID }}\"],\n  \"scheduled_at\": \"{{ $json.scheduledTime }}\"\n}",
        "options": {}
      },
      "id": "buffer-schedule-twitter",
      "name": "Buffer - Schedule Twitter",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        100
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "url": "={{$env.BUFFER_API_URL || 'https://api.bufferapp.com/1'}}/updates/create.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"text\": \"{{ $json.text }}\",\n  \"profile_ids\": [\"{{ $env.BUFFER_LINKEDIN_PROFILE_ID }}\"],\n  \"scheduled_at\": \"{{ $json.scheduledTime }}\"\n}",
        "options": {}
      },
      "id": "buffer-schedule-linkedin",
      "name": "Buffer - Schedule LinkedIn",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "url": "={{$env.BUFFER_API_URL || 'https://api.bufferapp.com/1'}}/updates/create.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"text\": \"{{ $json.text }}\",\n  \"profile_ids\": [\"{{ $env.BUFFER_FACEBOOK_PROFILE_ID }}\"],\n  \"scheduled_at\": \"{{ $json.scheduledTime }}\"\n}",
        "options": {}
      },
      "id": "buffer-schedule-facebook",
      "name": "Buffer - Schedule Facebook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        500
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{$env.SOCIAL_TRACKING_SHEET_ID}}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Posted Content",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Platform": "={{ $json.platform }}",
            "Post Text": "={{ $json.text.substring(0, 500) }}",
            "Scheduled Time": "={{ $json.scheduledTime }}",
            "Status": "={{ $json.status }}",
            "Created At": "={{ new Date().toISOString() }}",
            "Request ID": "={{ $('Prepare Content').first().json.requestId }}"
          },
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "track-to-sheets",
      "name": "Track to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.4,
      "position": [
        1560,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Aggregate all scheduling results\nconst items = $input.all();\nconst results = items.map(item => ({\n  platform: item.json.platform,\n  scheduledTime: item.json.scheduledTime,\n  status: item.json.status || 'scheduled',\n  text: item.json.text?.substring(0, 100) + '...'\n}));\n\nreturn {\n  success: true,\n  scheduled: results.length,\n  posts: results,\n  requestId: $('Prepare Content').first().json.requestId,\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "aggregate-results",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1780,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "respond-success",
      "name": "Respond with Results",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2000,
        300
      ]
    }
  ],
  "connections": {
    "Webhook - Schedule Content": {
      "main": [
        [
          {
            "node": "Prepare Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Content": {
      "main": [
        [
          {
            "node": "Generate Posts via Maximo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Posts via Maximo": {
      "main": [
        [
          {
            "node": "Prepare Scheduled Posts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Scheduled Posts": {
      "main": [
        [
          {
            "node": "Route by Platform",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Platform": {
      "main": [
        [
          {
            "node": "Buffer - Schedule Twitter",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Buffer - Schedule LinkedIn",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Buffer - Schedule Facebook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buffer - Schedule Twitter": {
      "main": [
        [
          {
            "node": "Track to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buffer - Schedule LinkedIn": {
      "main": [
        [
          {
            "node": "Track to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buffer - Schedule Facebook": {
      "main": [
        [
          {
            "node": "Track to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Track to Google Sheets": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Respond with Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "staticData": null,
  "tags": [
    {
      "name": "Social Media",
      "id": "social-media-tag"
    },
    {
      "name": "Automation",
      "id": "automation-tag"
    }
  ],
  "meta": {
    "templateId": "maximo-social-scheduler"
  }
}