{
  "name": "6-[Frontend]-GenerateDocument",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "legal-ai/generate-document",
        "options": {}
      },
      "id": "nodeWebHook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -1020,
        220
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "type",
              "value": "={{ $json.body.type || 'generic' }}",
              "type": "string"
            },
            {
              "name": "documentType",
              "value": "={{ $json.body.documentType || '' }}",
              "type": "string"
            },
            {
              "name": "description",
              "value": "={{ $json.body.description || '' }}",
              "type": "string"
            },
            {
              "name": "templateKey",
              "value": "={{ $json.body.templateKey || '' }}",
              "type": "string"
            },
            {
              "name": "notes",
              "value": "={{ $json.body.notes || '' }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "nodeParseRequest",
      "name": "Parse Request",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -780,
        220
      ]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.type }}",
              "rightValue": "template",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "id": "nodeIsTemplate",
      "name": "Is Template?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [
        -540,
        220
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {
          "temperature": 0.2,
          "frequencyPenalty": 0,
          "presencePenalty": 0,
          "responseFormat": "json"
        },
        "messages": {
          "messageValues": [
            {
              "message": "Eres un asistente legal que redacta documentos en espa\u00f1ol profesional. Tipo: {{ $json.documentType || 'Documento Legal' }}. Descripci\u00f3n: {{ $json.description }}. Devuelve JSON con {\\\"title\\\", \\\"summary\\\", \\\"content\\\" (HTML)}."
            }
          ]
        }
      },
      "id": "nodeGenerateGeneric",
      "name": "Generar Borrador",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        -240,
        40
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "status",
              "value": "success",
              "type": "string"
            },
            {
              "name": "mode",
              "value": "generic",
              "type": "string"
            },
            {
              "name": "title",
              "value": "={{ $json.output.title || `${$json.documentType} - ${new Date().toLocaleDateString('es-AR')}` }}",
              "type": "string"
            },
            {
              "name": "summary",
              "value": "={{ $json.output.summary || '' }}",
              "type": "string"
            },
            {
              "name": "content",
              "value": "={{ $json.output.content || '' }}",
              "type": "string"
            },
            {
              "name": "message",
              "value": "Borrador generado correctamente. Puedes copiar el contenido o pegarlo en un documento nuevo.",
              "type": "string"
            }
          ]
        }
      },
      "id": "nodePrepareGenericResponse",
      "name": "Preparar Respuesta Gen\u00e9rica",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        0,
        40
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "nodeRespondGeneric",
      "name": "Responder Gen\u00e9rico",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        240,
        40
      ]
    },
    {
      "parameters": {
        "functionCode": "const templates = {\n  intimacion: { fileId: 'REEMPLAZA_CON_ID_INTIMACION', titlePrefix: 'Redacci\u00f3n Intimaci\u00f3n' },\n  carta_documento: { fileId: 'REEMPLAZA_CON_ID_CARTA_DOCUMENTO', titlePrefix: 'Carta Documento' },\n  demanda: { fileId: 'REEMPLAZA_CON_ID_DEMANDA', titlePrefix: 'Demanda' },\n  contrato: { fileId: 'REEMPLAZA_CON_ID_CONTRATO', titlePrefix: 'Contrato' },\n  poder: { fileId: 'REEMPLAZA_CON_ID_PODER', titlePrefix: 'Poder' }\n};\nconst key = (item.templateKey || '').toLowerCase();\nif (!templates[key]) {\n  throw new Error(`Plantilla no configurada para la clave: ${item.templateKey}`);\n}\nreturn {\n  ...item,\n  templateConfig: templates[key],\n  destinationFolderId: 'REEMPLAZA_CON_ID_CARPETA_REDACCIONES'\n};"
      },
      "id": "nodeSelectTemplate",
      "name": "Seleccionar Plantilla",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        -300,
        360
      ]
    },
    {
      "parameters": {
        "operation": "copy",
        "fileId": "={{ $json.templateConfig.fileId }}",
        "options": {
          "parentId": "={{ $json.destinationFolderId }}",
          "name": "={{ `${$json.templateConfig.titlePrefix} - ${(new Date()).toISOString().split('T')[0]}` }}"
        }
      },
      "id": "nodeCopyTemplate",
      "name": "Duplicar Plantilla",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -40,
        360
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "status",
              "value": "success",
              "type": "string"
            },
            {
              "name": "mode",
              "value": "template",
              "type": "string"
            },
            {
              "name": "driveUrl",
              "value": "={{ $json.webViewLink || '' }}",
              "type": "string"
            },
            {
              "name": "fileId",
              "value": "={{ $json.id || '' }}",
              "type": "string"
            },
            {
              "name": "message",
              "value": "Documento duplicado correctamente en la carpeta de redacciones. Completa los campos variables y revisa antes de compartir.",
              "type": "string"
            }
          ]
        }
      },
      "id": "nodePrepareTemplateResponse",
      "name": "Preparar Respuesta Plantilla",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        200,
        360
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "nodeRespondTemplate",
      "name": "Responder Plantilla",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        440,
        360
      ]
    },
    {
      "parameters": {
        "content": "## Flujo de generaci\u00f3n de documentos\n\n- Webhook: POST /legal-ai/generate-document\n- type=generic -> devuelve title/summary/content (HTML).\n- type=template -> duplica la plantilla en Google Drive.\n\n\u26a0\ufe0f Reemplaza los IDs de plantillas y la carpeta destino en el nodo \"Seleccionar Plantilla\".",
        "height": 360,
        "width": 720,
        "color": 4
      },
      "id": "nodeNote",
      "name": "Notas",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -600,
        -80
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Parse Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Request": {
      "main": [
        [
          {
            "node": "Is Template?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Template?": {
      "main": [
        [
          {
            "node": "Generar Borrador",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Seleccionar Plantilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generar Borrador": {
      "main": [
        [
          {
            "node": "Preparar Respuesta Gen\u00e9rica",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparar Respuesta Gen\u00e9rica": {
      "main": [
        [
          {
            "node": "Responder Gen\u00e9rico",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Seleccionar Plantilla": {
      "main": [
        [
          {
            "node": "Duplicar Plantilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Duplicar Plantilla": {
      "main": [
        [
          {
            "node": "Preparar Respuesta Plantilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparar Respuesta Plantilla": {
      "main": [
        [
          {
            "node": "Responder Plantilla",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "id": "generate-document-frontend",
  "tags": []
}