{
  "name": "LRAC-031 \u00b7 Reporte semanal ejecutivo Gemini Pro",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 6 * * 1"
            }
          ]
        }
      },
      "id": "node-001-cron",
      "name": "Cron \u00b7 lunes 6:00 hora Per\u00fa",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -1800,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "WITH ultima_sem AS (SELECT * FROM lrac_metricas_diarias WHERE fecha BETWEEN NOW() - INTERVAL '7 days' AND NOW()), prev_sem AS (SELECT * FROM lrac_metricas_diarias WHERE fecha BETWEEN NOW() - INTERVAL '14 days' AND NOW() - INTERVAL '7 days') SELECT json_build_object('lrac_actual', (SELECT AVG(lrac_4p) FROM ultima_sem), 'lrac_previo', (SELECT AVG(lrac_4p) FROM prev_sem), 'racs_creados', (SELECT COUNT(*) FROM lrac_racs WHERE creado >= NOW() - INTERVAL '7 days'), 'racs_cerrados', (SELECT COUNT(*) FROM lrac_racs WHERE cerrado >= NOW() - INTERVAL '7 days' AND estado = 'CERRADO_VERIFICADO_IA'), 'racs_reabiertos', (SELECT COUNT(*) FROM lrac_racs WHERE reabierto >= NOW() - INTERVAL '7 days'), 'detecciones_epp', (SELECT COUNT(*) FROM lrac_detecciones WHERE timestamp >= NOW() - INTERVAL '7 days'), 'gerencias_bajas', (SELECT json_agg(json_build_object('vp', vp, 'gerencia', gerencia, 'lrac_4p', lrac_4p) ORDER BY lrac_4p LIMIT 3) FROM lrac_metricas_semanal), 'patrones_recurrentes', (SELECT json_agg(json_build_object('tipo', tipo, 'count', cnt) ORDER BY cnt DESC LIMIT 3) FROM (SELECT tipo, COUNT(*) AS cnt FROM lrac_racs WHERE creado >= NOW() - INTERVAL '7 days' GROUP BY tipo) p)) AS resumen;"
      },
      "id": "node-002-pg-aggregate",
      "name": "Postgres \u00b7 agregar datos \u00faltimos 7 d\u00edas",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        -1500,
        0
      ]
    },
    {
      "parameters": {
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent",
        "method": "POST",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env.GEMINI_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"contents\": [{\n    \"parts\": [{ \"text\": \"Eres VP SHE senior con 20 a\u00f1os en mineria peruana. A partir de estos datos de la semana pasada de Mina Juanita S.A., produce un reporte ejecutivo de 1 p\u00e1gina (markdown) dirigido a la junta directiva. Tono: directo, ejecutivo, sin jerga. Estructura: RESUMEN (3 frases con cifras), QUE FUNCIONO (top 3 logros), QUE NO FUNCIONO (top 3 alertas), RECOMENDACIONES (3 acciones), TENDENCIA (proyeccion 4 sem). Datos: {{ JSON.stringify($json.resumen) }}\" }]\n  }],\n  \"generationConfig\": { \"temperature\": 0.2 }\n}"
      },
      "id": "node-003-gemini-reporte",
      "name": "Gemini Pro \u00b7 narrativa ejecutiva",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -1200,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const md = $input.first().json.candidates[0].content.parts[0].text;\nconst fechaIso = new Date().toISOString().slice(0, 10);\nconst semana = `Semana del ${new Date(Date.now() - 7*86400000).toISOString().slice(0,10)} al ${fechaIso}`;\nconst markdownCompleto = `# Reporte ejecutivo SHE \u00b7 ${semana}\\n\\n${md}\\n\\n---\\n\\n_Generado autom\u00e1ticamente por LRAC-031 \u00b7 Gemini Pro \u00b7 ${fechaIso}_`;\nreturn [{ json: { markdown: markdownCompleto, fecha: fechaIso, semana } }];"
      },
      "id": "node-004-format",
      "name": "Formatear markdown",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -900,
        0
      ]
    },
    {
      "parameters": {
        "url": "{{ $env.PDFKIT_URL }}/md-to-pdf",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"markdown\": {{ JSON.stringify($json.markdown) }}, \"theme\": \"mmg-lrac\", \"filename\": \"reporte-she-{{ $json.fecha }}.pdf\" }"
      },
      "id": "node-005-pdfkit",
      "name": "PDFkit \u00b7 render PDF con plantilla MMG",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -600,
        0
      ]
    },
    {
      "parameters": {
        "operation": "upload",
        "name": "=Reporte SHE \u00b7 {{ $json.semana }}.pdf",
        "folderId": "{{ $env.GDRIVE_REPORTES_SHE }}",
        "binaryData": true,
        "binaryPropertyName": "data"
      },
      "id": "node-006-drive",
      "name": "Google Drive \u00b7 archivar hist\u00f3rico",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -300,
        0
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $env.VP_SHE_EMAIL }},{{ $env.VP_OPS_EMAIL }},{{ $env.VP_PROY_EMAIL }},{{ $env.VP_SUPPLY_EMAIL }}",
        "subject": "=Reporte SHE semanal \u00b7 {{ $json.semana }}",
        "emailType": "html",
        "message": "=Estimad@s VPs,<br><br>Adjunto el reporte ejecutivo SHE de la semana pasada generado autom\u00e1ticamente por LRAC-031.<br><br>{{ $json.markdown }}<br><br>Para drill-down, ver el dashboard ejecutivo: <a href=\"https://lasbambas-lrac-emanuel.streamlit.app\">link</a>.<br><br>--<br>Aecodito \u00b7 LRAC-031",
        "attachments": "data"
      },
      "id": "node-007-gmail",
      "name": "Gmail \u00b7 enviar a VPs",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO lrac_reportes_ejecutivos (fecha, semana, markdown, pdf_drive_id, destinatarios) VALUES ($1, $2, $3, $4, $5);"
      },
      "id": "node-008-pg-log",
      "name": "Postgres \u00b7 log reporte",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        300,
        0
      ]
    }
  ],
  "connections": {
    "Cron \u00b7 lunes 6:00 hora Per\u00fa": {
      "main": [
        [
          {
            "node": "Postgres \u00b7 agregar datos \u00faltimos 7 d\u00edas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres \u00b7 agregar datos \u00faltimos 7 d\u00edas": {
      "main": [
        [
          {
            "node": "Gemini Pro \u00b7 narrativa ejecutiva",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini Pro \u00b7 narrativa ejecutiva": {
      "main": [
        [
          {
            "node": "Formatear markdown",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formatear markdown": {
      "main": [
        [
          {
            "node": "PDFkit \u00b7 render PDF con plantilla MMG",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PDFkit \u00b7 render PDF con plantilla MMG": {
      "main": [
        [
          {
            "node": "Google Drive \u00b7 archivar hist\u00f3rico",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive \u00b7 archivar hist\u00f3rico": {
      "main": [
        [
          {
            "node": "Gmail \u00b7 enviar a VPs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u00b7 enviar a VPs": {
      "main": [
        [
          {
            "node": "Postgres \u00b7 log reporte",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "workflow_id": "LRAC-031",
    "description": "Reporte semanal ejecutivo Gemini Pro a VPs \u00b7 cada lunes 6:00 \u00b7 ahorra 6-10 h analista",
    "author": "Emanuel Edgar Ancco Guaygua"
  },
  "tags": [
    {
      "name": "lrac"
    },
    {
      "name": "gaiatech-m1"
    },
    {
      "name": "M1.D"
    }
  ]
}