{
  "name": "SIC Luj\u00e1n - Paso 2: F\u00e1brica Visual (v22.1)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "sic-paso2-visual",
        "options": {}
      },
      "name": "Webhook desde Paso 1",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        100,
        300
      ],
      "_note": "Reemplaza el polling cada minuto. Recibe id_master del Paso 1."
    },
    {
      "parameters": {
        "jsCode": "// Verificar secreto compartido para seguridad entre flujos\nconst secret = $input.first().json.headers?.['x-sic-secret'];\nconst expectedSecret = $env.SIC_WEBHOOK_SECRET;\n\nif (!secret || secret !== expectedSecret) {\n  throw new Error('Acceso no autorizado: secreto inv\u00e1lido o ausente.');\n}\n\nconst idMaster = $input.first().json.body?.id_master;\nif (!idMaster) {\n  throw new Error('id_master no recibido desde Paso 1.');\n}\n\nreturn [{ json: { id_master: idMaster } }];"
      },
      "name": "Verificar secreto",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": "INGESTA_EVENTOS",
        "filters": {
          "conditions": [
            {
              "column": "id_master",
              "operation": "equal",
              "value": "={{$node[\"Verificar secreto\"].json[\"id_master\"]}}"
            },
            {
              "column": "estado",
              "operation": "equal",
              "value": "Pendiente Visual"
            }
          ]
        }
      },
      "name": "Leer fila de Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        500,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"Leer fila de Sheets\"].json[\"url_imagen_original\"]}}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "name": "\u00bfTiene imagen?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "operation": "render",
        "templateId": "YOUR_CREATOMATE_TEMPLATE_ID",
        "modifications": {
          "mappingMode": "defineBelow",
          "value": {
            "Texto_Titulo": "={{$node[\"Leer fila de Sheets\"].json[\"title\"]}}",
            "Texto_Fecha": "={{$node[\"Leer fila de Sheets\"].json[\"date\"]}}",
            "Texto_Horarios": "={{$node[\"Leer fila de Sheets\"].json[\"horarios\"]}}",
            "Texto_Disciplinas": "={{$node[\"Leer fila de Sheets\"].json[\"disciplinas\"]}}",
            "Fondo": "={{$node[\"Leer fila de Sheets\"].json[\"url_imagen_original\"]}}"
          }
        },
        "options": {
          "waitForFinish": true
        }
      },
      "name": "Creatomate (con imagen)",
      "type": "n8n-nodes-base.creatomate",
      "typeVersion": 1,
      "position": [
        900,
        220
      ],
      "credentials": {
        "creatomateApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "render",
        "templateId": "YOUR_CREATOMATE_TEMPLATE_NOIMAGEN_ID",
        "modifications": {
          "mappingMode": "defineBelow",
          "value": {
            "Texto_Titulo": "={{$node[\"Leer fila de Sheets\"].json[\"title\"]}}",
            "Texto_Fecha": "={{$node[\"Leer fila de Sheets\"].json[\"date\"]}}",
            "Texto_Horarios": "={{$node[\"Leer fila de Sheets\"].json[\"horarios\"]}}",
            "Texto_Disciplinas": "={{$node[\"Leer fila de Sheets\"].json[\"disciplinas\"]}}"
          }
        },
        "options": {
          "waitForFinish": true
        }
      },
      "name": "Creatomate (sin imagen)",
      "type": "n8n-nodes-base.creatomate",
      "typeVersion": 1,
      "position": [
        900,
        420
      ],
      "credentials": {
        "creatomateApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": "INGESTA_EVENTOS",
        "key": "id_master",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "url_flyer_1": "={{$node[\"Creatomate (con imagen)\"].json[\"url\"] || $node[\"Creatomate (sin imagen)\"].json[\"url\"]}}",
            "estado": "Pendiente WP"
          }
        }
      },
      "name": "Actualizar Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1100,
        300
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.N8N_PASO3_WEBHOOK_URL }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-SIC-Secret",
              "value": "={{ $env.SIC_WEBHOOK_SECRET }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "id_master",
              "value": "={{$node[\"Verificar secreto\"].json[\"id_master\"]}}"
            },
            {
              "name": "trigger",
              "value": "flyer_listo"
            }
          ]
        },
        "options": {}
      },
      "name": "Disparar Paso 3",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1300,
        300
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_SHEET_ID",
          "mode": "id"
        },
        "sheetName": "INGESTA_EVENTOS",
        "key": "id_master",
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "estado": "Error Visual",
            "error_log": "={{$execution.error?.message || 'Error en Creatomate'}}"
          }
        }
      },
      "name": "Marcar error en Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        900,
        580
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Webhook desde Paso 1": {
      "main": [
        [
          {
            "node": "Verificar secreto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar secreto": {
      "main": [
        [
          {
            "node": "Leer fila de Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Leer fila de Sheets": {
      "main": [
        [
          {
            "node": "\u00bfTiene imagen?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00bfTiene imagen?": {
      "main": [
        [
          {
            "node": "Creatomate (con imagen)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Creatomate (sin imagen)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Creatomate (con imagen)": {
      "main": [
        [
          {
            "node": "Actualizar Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Creatomate (sin imagen)": {
      "main": [
        [
          {
            "node": "Actualizar Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Actualizar Sheets": {
      "main": [
        [
          {
            "node": "Disparar Paso 3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "errorWorkflow": "SIC-Error-Handler",
  "_readme": {
    "cambios_v22_1": [
      "Reemplazado polling everyMinute por webhook encadenado desde Paso 1",
      "Agregada verificaci\u00f3n de secreto X-SIC-Secret",
      "Dos ramas Creatomate: con y sin imagen de fondo",
      "Incluye campos Texto_Horarios y Texto_Disciplinas en el flyer",
      "Nodo de error escribe estado 'Error Visual' en Sheets en vez de silenciar el fallo"
    ],
    "variables_requeridas": [
      "N8N_PASO3_WEBHOOK_URL: URL del webhook de inicio del Paso 3",
      "SIC_WEBHOOK_SECRET: El mismo token que usa el Paso 1",
      "YOUR_CREATOMATE_TEMPLATE_ID: Plantilla con imagen de fondo",
      "YOUR_CREATOMATE_TEMPLATE_NOIMAGEN_ID: Plantilla alternativa sin imagen",
      "YOUR_GOOGLE_SHEET_ID: ID de tu Google Sheet SIC_LABORATORIO"
    ],
    "plantilla_creatomate_capas_requeridas": [
      "Texto_Titulo",
      "Texto_Fecha",
      "Texto_Horarios",
      "Texto_Disciplinas",
      "Fondo (solo en plantilla con imagen)"
    ]
  }
}