{
  "name": "EmailSorter - Automatische E-Mail-Sortierung",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "email-webhook",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Email Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300],
      "webhookId": "email-sorter-webhook"
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "get",
        "messageId": "={{ $json.emailId }}",
        "options": {
          "format": "metadata"
        }
      },
      "id": "gmail-get-email",
      "name": "Gmail: E-Mail abrufen",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [450, 200],
      "credentials": {
        "gmailOAuth2": {
          "id": "gmail-credentials",
          "name": "Gmail OAuth2"
        }
      }
    },
    {
      "parameters": {
        "url": "https://api.mistral.ai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"model\": \"mistral-small-latest\",\n  \"messages\": [\n    {\n      \"role\": \"system\",\n      \"content\": \"Du bist ein E-Mail-Klassifizierungs-Assistent. Kategorisiere E-Mails in: vip, clients, invoices, newsletters, promos, social, security, shipping, review. Antworte NUR mit dem Kategorienamen.\"\n    },\n    {\n      \"role\": \"user\",\n      \"content\": \"Von: {{ $json.from }}\\nBetreff: {{ $json.subject }}\\nVorschau: {{ $json.snippet }}\"\n    }\n  ],\n  \"temperature\": 0.1,\n  \"max_tokens\": 50\n}",
        "options": {}
      },
      "id": "mistral-classify",
      "name": "Mistral AI: Klassifizieren",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [650, 200],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mistral-credentials",
          "name": "Mistral API"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.choices[0].message.content.trim() }}",
              "operation": "contains",
              "value2": "newsletter"
            }
          ]
        }
      },
      "id": "if-newsletter",
      "name": "Ist Newsletter?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [850, 200]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail: E-Mail abrufen').item.json.id }}",
        "labelIds": ["EmailSorter/Newsletter"]
      },
      "id": "gmail-label-newsletter",
      "name": "Gmail: Newsletter Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1050, 100],
      "credentials": {
        "gmailOAuth2": {
          "id": "gmail-credentials",
          "name": "Gmail OAuth2"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Gmail: E-Mail abrufen').item.json.id }}",
        "labelIds": ["={{ 'EmailSorter/' + $('Mistral AI: Klassifizieren').item.json.choices[0].message.content.trim() }}"]
      },
      "id": "gmail-label-other",
      "name": "Gmail: Kategorie Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [1050, 300],
      "credentials": {
        "gmailOAuth2": {
          "id": "gmail-credentials",
          "name": "Gmail OAuth2"
        }
      }
    },
    {
      "parameters": {
        "url": "={{ $env.EMAILSORTER_API_URL }}/api/email/stats/update",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "category",
              "value": "={{ $('OpenAI: Klassifizieren').item.json.message.content.trim() }}"
            },
            {
              "name": "emailId",
              "value": "={{ $('Gmail: E-Mail abrufen').item.json.id }}"
            }
          ]
        },
        "options": {}
      },
      "id": "http-update-stats",
      "name": "Statistiken aktualisieren",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1250, 200]
    }
  ],
  "connections": {
    "Email Webhook": {
      "main": [
        [
          {
            "node": "Gmail: E-Mail abrufen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail: E-Mail abrufen": {
      "main": [
        [
          {
            "node": "Mistral AI: Klassifizieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mistral AI: Klassifizieren": {
      "main": [
        [
          {
            "node": "Ist Newsletter?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ist Newsletter?": {
      "main": [
        [
          {
            "node": "Gmail: Newsletter Label",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail: Kategorie Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail: Newsletter Label": {
      "main": [
        [
          {
            "node": "Statistiken aktualisieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail: Kategorie Label": {
      "main": [
        [
          {
            "node": "Statistiken aktualisieren",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": ["email", "automation", "ai"],
  "pinData": {}
}
