{
  "name": "Polygam Content Automation (Final Architecture)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "polygam-project",
        "responseMode": "responseNode",
        "authentication": "headerAuth",
        "options": {}
      },
      "id": "Webhook",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        200,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const input = $input.first().json;\nreturn { json: {\n  project_name: input.project_name || input.title || \"Unnamed Project\",\n  client: input.client || \"Unknown\",\n  location: input.location || \"\",\n  description: input.description || input.details || \"\",\n  challenges: input.challenges || \"\",\n  solution: input.solution || \"\",\n  results: input.results || \"\",\n  raw: input\n}};"
      },
      "id": "Normalizer",
      "name": "Input Normalizer",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        420,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "temperature",
              "value": "0.2"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"You are an engineering analyst. Extract a structured project profile from raw information about water and wastewater infrastructure projects. Return ONLY JSON.\"},{\"role\":\"user\",\"content\":\"Project:\\nName: \"+$json.project_name+\"\\nClient: \"+$json.client+\"\\nLocation: \"+$json.location+\"\\nDescription: \"+$json.description+\"\\nChallenges: \"+$json.challenges+\"\\nSolution: \"+$json.solution+\"\\nResults: \"+$json.results}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "Analyst",
      "name": "Project Analyst",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        640,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const content = $input.first().json.choices[0].message.content;\nlet parsed;\ntry {\n  parsed = JSON.parse(content);\n} catch (e) {\n  const start = content.indexOf('{');\n  const end = content.lastIndexOf('}');\n  if (start !== -1 && end !== -1 && end > start) {\n    try {\n      parsed = JSON.parse(content.slice(start, end + 1));\n    } catch (e2) {\n      parsed = { raw: content };\n    }\n  } else {\n    parsed = { raw: content };\n  }\n}\nreturn { json: parsed };"
      },
      "id": "ParseProfile",
      "name": "Parse Profile",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        860,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"Generate a structured engineering fact sheet for a water infrastructure project. Separate HARD FACTS from NARRATIVE ANGLES.\"},{\"role\":\"user\",\"content\":\"Profile:\\n\"+JSON.stringify($json)}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "FactSheet",
      "name": "Generate Fact Sheet",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1080,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"Write a professional engineering case study based on the provided fact sheet. Documentary tone, no advertising.\"},{\"role\":\"user\",\"content\":$json.choices[0].message.content}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "CaseStudy",
      "name": "Write Case Study",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1300,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"Act as a technical QA reviewer for engineering publications. Verify technical consistency and return the corrected case study.\"},{\"role\":\"user\",\"content\":$json.choices[0].message.content}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "TechnicalQA",
      "name": "Technical QA",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1520,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"Convert the case study into multiple independent but related Bale-style posts. Each post must have a strong hook, a distinct angle, and stand alone.\"},{\"role\":\"user\",\"content\":$json.choices[0].message.content}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "Serializer",
      "name": "Serialize Posts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1740,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "requestMethod": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o"
            },
            {
              "name": "messages",
              "value": "={{[{\"role\":\"system\",\"content\":\"Act as a B2B marketing editor. Improve hook strength, clarity, internal Bale SEO, star/emoji formatting. Return final posts only.\"},{\"role\":\"user\",\"content\":$json.choices[0].message.content}]}}"
            }
          ]
        },
        "options": {}
      },
      "id": "MarketingQA",
      "name": "Marketing QA",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1960,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const posts = $input.first().json.choices[0].message.content;\nconst footer = \"\\n\\n*\ud83d\udd0e \u062c\u0647\u062a \u0645\u0634\u0627\u0648\u0631\u0647 \u0648 \u0627\u0637\u0644\u0627\u0639\u0627\u062a \u0628\u06cc\u0634\u062a\u0631 :*\\n \ud83d\udce8@polygum_support\\n\\n\ud83d\udd17 \u06a9\u0627\u0646\u0627\u0644 \u062a\u062e\u0635\u0635\u06cc *\u067e\u0644\u06cc\u200c\u06af\u0627\u0645* \u062f\u0631 \\\"\u0628\u0644\u0647\\\" :\\n \ud83c\udf10@Polygum\";\nreturn { json: { final_posts: posts + footer } };"
      },
      "id": "AppendFooter",
      "name": "Append Footer",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2180,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {}
      },
      "id": "Response",
      "name": "Return Result",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        2400,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Input Normalizer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Input Normalizer": {
      "main": [
        [
          {
            "node": "Project Analyst",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Project Analyst": {
      "main": [
        [
          {
            "node": "Parse Profile",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Profile": {
      "main": [
        [
          {
            "node": "Generate Fact Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Fact Sheet": {
      "main": [
        [
          {
            "node": "Write Case Study",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write Case Study": {
      "main": [
        [
          {
            "node": "Technical QA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Technical QA": {
      "main": [
        [
          {
            "node": "Serialize Posts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Serialize Posts": {
      "main": [
        [
          {
            "node": "Marketing QA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Marketing QA": {
      "main": [
        [
          {
            "node": "Append Footer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Footer": {
      "main": [
        [
          {
            "node": "Return Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "versionId": "1"
}