{
  "name": "Monera — Alertes KPI (déclenchement manuel ou webhook)",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-trigger",
      "name": "Déclenchement manuel",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [240, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "http://localhost:3000/api/kpis/alertes",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [{ "name": "Authorization", "value": "Bearer REMPLACE_PAR_TON_TOKEN_ADMIN" }]
        },
        "options": {}
      },
      "id": "http-alertes",
      "name": "Lire les alertes",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [460, 300]
    },
    {
      "parameters": {
        "conditions": {
          "number": [{ "value1": "={{ $json.recommandations.length }}", "operation": "larger", "value2": 0 }]
        }
      },
      "id": "check-alertes",
      "name": "Des alertes ?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [680, 300]
    },
    {
      "parameters": {
        "fromEmail": "monera-alertes@gmail.com",
        "toEmail": "yasminee.maiz@gmail.com",
        "subject": "=⚠️ Monera — {{ $json.recommandations.length }} alerte(s) financière(s)",
        "text": "=Bonjour,\n\n{{ $json.recommandations.length }} alerte(s) ont été détectées :\n\n{{ $json.recommandations.map(r => '• [' + r.priorite.toUpperCase() + '] ' + r.titre + '\\n  ' + r.message).join('\\n\\n') }}\n\nDonnées au : {{ $json.meta.updated_at }}\n\nConsultez votre dashboard → http://localhost:5173/dashboard\n\n— Monera Alertes automatiques"
      },
      "id": "email-alertes",
      "name": "Envoyer email d'alerte",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [900, 200]
    },
    {
      "parameters": {
        "values": {
          "string": [{ "name": "message", "value": "Aucune alerte — situation normale." }]
        },
        "options": {}
      },
      "id": "no-alert",
      "name": "Aucune alerte",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [900, 420]
    }
  ],
  "connections": {
    "Déclenchement manuel": {
      "main": [[{ "node": "Lire les alertes", "type": "main", "index": 0 }]]
    },
    "Lire les alertes": {
      "main": [[{ "node": "Des alertes ?", "type": "main", "index": 0 }]]
    },
    "Des alertes ?": {
      "main": [
        [{ "node": "Envoyer email d'alerte", "type": "main", "index": 0 }],
        [{ "node": "Aucune alerte",          "type": "main", "index": 0 }]
      ]
    }
  },
  "settings": { "executionOrder": "v1" }
}
