{
  "name": "Image Generation Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "image/generate",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-image-gen",
      "name": "Webhook - Generate Image",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT cd.*, c.branding_json FROM content_drafts cd JOIN campaigns c ON cd.campaign_id = c.id WHERE cd.id = {{ $json.body.draft_id }}",
        "additionalFields": {}
      },
      "id": "get-content",
      "name": "Get Content Draft",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        450,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "http://langchain-service:8001/chains/image-prompt-builder",
        "method": "POST",
        "bodyParametersJson": "={\n  \"content_description\": \"{{ $json.content }}\",\n  \"content_type\": \"{{ $json.type }}\",\n  \"platform\": \"{{ $('Webhook - Generate Image').item.json.body.platform || 'linkedin' }}\",\n  \"brand_colors\": {{ $json.branding_json.primary_color ? `[\"${$json.branding_json.primary_color}\", \"${$json.branding_json.secondary_color}\"]` : '[]' }},\n  \"brand_style\": \"{{ $json.branding_json.brand_voice || 'professional' }}\"\n}",
        "options": {}
      },
      "id": "build-prompts",
      "name": "Build Image Prompts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-dalle",
              "leftValue": "={{ $('Webhook - Generate Image').item.json.body.provider || 'dalle' }}",
              "rightValue": "dalle",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-dalle",
      "name": "If DALL-E Provider",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://langchain-service:8001/agents/image",
        "method": "POST",
        "bodyParametersJson": "={\n  \"provider\": \"dalle\",\n  \"prompt\": \"{{ $('Build Image Prompts').item.json.dalle_prompt }}\",\n  \"size\": \"{{ $('Webhook - Generate Image').item.json.body.size || '1024x1024' }}\",\n  \"quality\": \"{{ $('Webhook - Generate Image').item.json.body.quality || 'standard' }}\",\n  \"style\": \"{{ $('Webhook - Generate Image').item.json.body.style || 'vivid' }}\"\n}",
        "options": {
          "timeout": 120000
        }
      },
      "id": "generate-dalle",
      "name": "Generate with DALL-E 3",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1050,
        200
      ]
    },
    {
      "parameters": {
        "url": "http://langchain-service:8001/agents/image",
        "method": "POST",
        "bodyParametersJson": "={\n  \"provider\": \"midjourney\",\n  \"prompt\": \"{{ $('Build Image Prompts').item.json.midjourney_prompt }}\",\n  \"aspect_ratio\": \"{{ $('Webhook - Generate Image').item.json.body.aspect_ratio || '16:9' }}\",\n  \"version\": \"{{ $('Webhook - Generate Image').item.json.body.mj_version || '6' }}\",\n  \"stylize\": {{ $('Webhook - Generate Image').item.json.body.stylize || 100 }}\n}",
        "options": {
          "timeout": 180000
        }
      },
      "id": "generate-midjourney",
      "name": "Generate with Midjourney",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1050,
        400
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.image_url }}",
        "method": "GET",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "download-image",
      "name": "Download Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "text",
        "binaryPropertyName": "data",
        "text": "={{ $('Get Content Draft').item.json.branding_json.logo_url || 'Brand' }}",
        "fontSize": 24,
        "fontColor": "#FFFFFF",
        "positionX": 20,
        "positionY": 20,
        "options": {
          "backgroundColor": "rgba(0,0,0,0.7)",
          "backgroundPadding": 10
        }
      },
      "id": "add-watermark",
      "name": "Add Brand Watermark",
      "type": "n8n-nodes-base.editImage",
      "typeVersion": 1,
      "position": [
        1450,
        300
      ]
    },
    {
      "parameters": {
        "operation": "resize",
        "binaryPropertyName": "data",
        "width": "={{ $('Webhook - Generate Image').item.json.body.final_width || 1200 }}",
        "height": "={{ $('Webhook - Generate Image').item.json.body.final_height || 628 }}",
        "resizeOption": "fitImage",
        "options": {}
      },
      "id": "resize-image",
      "name": "Resize to Platform Specs",
      "type": "n8n-nodes-base.editImage",
      "typeVersion": 1,
      "position": [
        1450,
        450
      ]
    },
    {
      "parameters": {
        "operation": "compress",
        "binaryPropertyName": "data",
        "quality": 85,
        "options": {}
      },
      "id": "optimize-image",
      "name": "Optimize Image Quality",
      "type": "n8n-nodes-base.editImage",
      "typeVersion": 1,
      "position": [
        1650,
        350
      ]
    },
    {
      "parameters": {
        "jsCode": "// Save image to file system and create database record\nconst binary = $input.item.binary.data;\nconst filename = `image_${Date.now()}_${Math.random().toString(36).substring(7)}.png`;\nconst filePath = `/app/media_assets/${filename}`;\n\n// In production, this would save to actual file system or S3\n// For now, we'll store the data URI\n\nconst metadata = {\n  draft_id: $('Get Content Draft').item.json.id,\n  type: 'image',\n  file_path: filePath,\n  url: `http://localhost:8000/media/${filename}`,\n  prompt: $('Build Image Prompts').item.json.dalle_prompt || $('Build Image Prompts').item.json.midjourney_prompt,\n  api_provider: $('Webhook - Generate Image').item.json.body.provider || 'dalle',\n  metadata_json: {\n    dimensions: {\n      width: $('Webhook - Generate Image').item.json.body.final_width || 1200,\n      height: $('Webhook - Generate Image').item.json.body.final_height || 628\n    },\n    format: 'png',\n    size_bytes: binary.length,\n    prompt_params: {\n      platform: $('Webhook - Generate Image').item.json.body.platform || 'linkedin',\n      quality: $('Webhook - Generate Image').item.json.body.quality || 'standard',\n      style: $('Webhook - Generate Image').item.json.body.style || 'vivid'\n    },\n    brand_colors: $('Get Content Draft').item.json.branding_json.primary_color ? [\n      $('Get Content Draft').item.json.branding_json.primary_color,\n      $('Get Content Draft').item.json.branding_json.secondary_color\n    ] : [],\n    generated_at: new Date().toISOString()\n  }\n};\n\nreturn metadata;"
      },
      "id": "prepare-media-record",
      "name": "Prepare Media Asset Record",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1850,
        350
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "media_assets",
        "columns": "draft_id, type, file_path, url, prompt, api_provider, metadata_json, created_at",
        "returnFields": "id, draft_id, url, type",
        "additionalFields": {}
      },
      "id": "save-media",
      "name": "Save Media Asset",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        2050,
        350
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "table": "content_drafts",
        "updateKey": "id",
        "columns": "status",
        "additionalFields": {
          "additionalFields": [
            {
              "name": "updated_at",
              "value": "=NOW()"
            }
          ]
        }
      },
      "id": "update-draft-status",
      "name": "Update Draft Status",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        2050,
        500
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "http://localhost:5678/webhook/content-review",
        "method": "POST",
        "bodyParametersJson": "={\n  \"draft_id\": {{ $json.draft_id }},\n  \"media_asset_id\": {{ $json.id }},\n  \"action\": \"media_ready_for_review\",\n  \"media_url\": \"{{ $json.url }}\",\n  \"media_type\": \"{{ $json.type }}\",\n  \"dashboard_url\": \"http://localhost:8501/media_review?asset_id={{ $json.id }}\"\n}",
        "options": {}
      },
      "id": "notify-review",
      "name": "Notify Review Dashboard",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        2250,
        350
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"message\": \"Image generated successfully\",\n  \"asset_id\": {{ $json.id }},\n  \"draft_id\": {{ $json.draft_id }},\n  \"image_url\": \"{{ $json.url }}\",\n  \"provider\": \"{{ $('Webhook - Generate Image').item.json.body.provider || 'dalle' }}\",\n  \"dimensions\": {\n    \"width\": {{ $('Webhook - Generate Image').item.json.body.final_width || 1200 }},\n    \"height\": {{ $('Webhook - Generate Image').item.json.body.final_height || 628 }}\n  },\n  \"prompt_used\": \"{{ $('Build Image Prompts').item.json.dalle_prompt || $('Build Image Prompts').item.json.midjourney_prompt }}\",\n  \"status\": \"ready_for_review\",\n  \"review_url\": \"http://localhost:8501/media_review?asset_id={{ $json.id }}\"\n}",
        "options": {}
      },
      "id": "response-success",
      "name": "Respond Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        2450,
        350
      ]
    }
  ],
  "connections": {
    "Webhook - Generate Image": {
      "main": [
        [
          {
            "node": "Get Content Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Content Draft": {
      "main": [
        [
          {
            "node": "Build Image Prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Image Prompts": {
      "main": [
        [
          {
            "node": "If DALL-E Provider",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If DALL-E Provider": {
      "main": [
        [
          {
            "node": "Generate with DALL-E 3",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generate with Midjourney",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate with DALL-E 3": {
      "main": [
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate with Midjourney": {
      "main": [
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Image": {
      "main": [
        [
          {
            "node": "Add Brand Watermark",
            "type": "main",
            "index": 0
          },
          {
            "node": "Resize to Platform Specs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Brand Watermark": {
      "main": [
        [
          {
            "node": "Optimize Image Quality",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resize to Platform Specs": {
      "main": [
        [
          {
            "node": "Optimize Image Quality",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Optimize Image Quality": {
      "main": [
        [
          {
            "node": "Prepare Media Asset Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Media Asset Record": {
      "main": [
        [
          {
            "node": "Save Media Asset",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Media Asset": {
      "main": [
        [
          {
            "node": "Update Draft Status",
            "type": "main",
            "index": 0
          },
          {
            "node": "Notify Review Dashboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notify Review Dashboard": {
      "main": [
        [
          {
            "node": "Respond Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-01-15T00:00:00.000Z",
  "versionId": "1"
}