{
  "name": "AI_Content_Agent_35CT_Fallback",
  "nodes": [
    {
      "parameters": {
        "triggerTimes": [
          {
            "mode": "everyDay",
            "hour": 7,
            "minute": 0
          }
        ]
      },
      "id": "ScheduleTrigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "operation": "list",
        "baseId": "YOUR_AIRTABLE_BASE_ID",
        "table": "ArticleIdeas",
        "filterByFormula": "Status = 'Pending'",
        "sort": [
          {
            "field": "ID",
            "direction": "asc"
          }
        ],
        "maxRecords": 10
      },
      "id": "GetPendingTopics",
      "name": "Get Pending Topics",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [
        400,
        300
      ],
      "credentials": {
        "airtableApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$json.length}}",
              "operation": "smaller",
              "value2": 10
            }
          ]
        }
      },
      "id": "CheckFallback",
      "name": "Check Fallback",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        600,
        300
      ]
    },
    {
      "parameters": {
        "operation": "list",
        "baseId": "YOUR_AIRTABLE_BASE_ID",
        "table": "ArticleIdeas",
        "filterByFormula": "Status = 'Used'",
        "sort": [
          {
            "field": "ID",
            "direction": "asc"
          }
        ],
        "maxRecords": 10
      },
      "id": "GetUsedTopics",
      "name": "Get Used Topics",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [
        800,
        200
      ],
      "credentials": {
        "airtableApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "mode": "append"
      },
      "id": "MergeTopics",
      "name": "Merge Topics",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1
      },
      "id": "SplitInBatches",
      "name": "Split In Batches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "jsonParameters": true,
        "headers": [
          {
            "name": "Authorization",
            "value": "Bearer YOUR_OPENAI_API_KEY"
          },
          {
            "name": "Content-Type",
            "value": "application/json"
          }
        ],
        "bodyParametersJson": "{\n  \"model\": \"gpt-4o\",\n  \"messages\": [\n    { \"role\": \"system\", \"content\": \"{{$json[\"fields\"][\"SystemPrompt\"]}}\" },\n    { \"role\": \"user\", \"content\": \"{{$json[\"fields\"][\"UserPrompt\"]}}\" }\n  ]\n}"
      },
      "id": "GPTRequest",
      "name": "GPT Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1400,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Title",
              "value": "={{$json[\"fields\"][\"Topic\"]}}"
            },
            {
              "name": "Slug",
              "value": "={{$json[\"fields\"][\"Topic\"].replace(/\\s+/g, '-').toLowerCase()}}"
            },
            {
              "name": "Body",
              "value": "={{$json[\"choices\"][0][\"message\"][\"content\"]}}"
            },
            {
              "name": "ScheduledTime",
              "value": "={{new Date(new Date().getTime() + ($itemIndex * 2 * 60 * 60 * 1000)).toISOString()}}"
            }
          ]
        }
      },
      "id": "SetFormat",
      "name": "Set Format",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1600,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.contentful.com/spaces/YOUR_SPACE_ID/environments/master/entries",
        "jsonParameters": true,
        "headers": [
          {
            "name": "Authorization",
            "value": "Bearer YOUR_CONTENTFUL_MANAGEMENT_API_KEY"
          },
          {
            "name": "Content-Type",
            "value": "application/vnd.contentful.management.v1+json"
          }
        ],
        "bodyParametersJson": "{\n  \"fields\": {\n    \"title\": { \"en-US\": \"{{$json[\"Title\"]}}\" },\n    \"slug\": { \"en-US\": \"{{$json[\"Slug\"]}}\" },\n    \"body\": { \"en-US\": \"{{$json[\"Body\"]}}\" },\n    \"publishDate\": { \"en-US\": \"{{$json[\"ScheduledTime\"]}}\" }\n  }\n}"
      },
      "id": "CreateContentful",
      "name": "Create Contentful Entry",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1800,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://indexing.googleapis.com/v3/urlNotifications:publish",
        "jsonParameters": true,
        "bodyParametersJson": "{\n  \"url\": \"https://yourwebsite.com/{{$json[\"Slug\"]}}\",\n  \"type\": \"URL_UPDATED\"\n}"
      },
      "id": "SubmitGSC",
      "name": "Submit to Google Search Console",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        2200,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "baseId": "YOUR_AIRTABLE_BASE_ID",
        "table": "ArticleIdeas",
        "id": "={{$json[\"fields\"][\"ID\"]}}",
        "fields": [
          {
            "fieldName": "Status",
            "fieldValue": "Used"
          }
        ]
      },
      "id": "UpdateStatus",
      "name": "Update Status",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [
        2400,
        300
      ],
      "credentials": {
        "airtableApi": "<your credential>"
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Pending Topics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Pending Topics": {
      "main": [
        [
          {
            "node": "Check Fallback",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Fallback": {
      "main": [
        [
          {
            "node": "Merge Topics",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Used Topics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Used Topics": {
      "main": [
        [
          {
            "node": "Merge Topics",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Topics": {
      "main": [
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split In Batches": {
      "main": [
        [
          {
            "node": "GPT Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT Request": {
      "main": [
        [
          {
            "node": "Set Format",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Format": {
      "main": [
        [
          {
            "node": "Create Contentful Entry",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contentful Entry": {
      "main": [
        [
          {
            "node": "Submit to Google Search Console",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit to Google Search Console": {
      "main": [
        [
          {
            "node": "Update Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}