{
  "name": "AI Email Categorizer",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        },
        "options": {}
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000001",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "model": "gpt-4",
        "messages": {
          "values": [
            {
              "content": "Categorize the following email into exactly one of these categories: URGENT, SALES, SUPPORT, BILLING, NEWSLETTER, SPAM, PERSONAL, OTHER.\n\nAlso provide a priority level: HIGH, MEDIUM, LOW.\n\nFrom: {{ $json.from }}\nSubject: {{ $json.subject }}\nBody: {{ $json.snippet }}\n\nReturn JSON with keys: category, priority, reason (brief explanation)"
            }
          ]
        },
        "options": {
          "temperature": 0.3,
          "maxTokens": 300
        }
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000002",
      "name": "OpenAI Categorize",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.3,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = JSON.parse($input.first().json.message.content);\nconst emailData = $('Gmail Trigger').first().json;\nreturn [{\n  json: {\n    messageId: emailData.id,\n    from: emailData.from,\n    subject: emailData.subject,\n    category: response.category,\n    priority: response.priority,\n    reason: response.reason\n  }\n}];"
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000003",
      "name": "Parse Category",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $json.messageId }}",
        "labelIds": [
          "={{ $json.category }}"
        ]
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000004",
      "name": "Apply Gmail Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.priority }}",
              "value2": "HIGH"
            }
          ]
        }
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000005",
      "name": "Is High Priority?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "channel": "#urgent-emails",
        "text": ":rotating_light: *High Priority Email*\n*From:* {{ $('Parse Category').first().json.from }}\n*Subject:* {{ $('Parse Category').first().json.subject }}\n*Category:* {{ $('Parse Category').first().json.category }}\n*Reason:* {{ $('Parse Category').first().json.reason }}",
        "otherOptions": {}
      },
      "id": "b1b2c3d4-2001-4000-8000-000000000006",
      "name": "Slack Alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1500,
        200
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "OpenAI Categorize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Categorize": {
      "main": [
        [
          {
            "node": "Parse Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Category": {
      "main": [
        [
          {
            "node": "Apply Gmail Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply Gmail Label": {
      "main": [
        [
          {
            "node": "Is High Priority?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is High Priority?": {
      "main": [
        [
          {
            "node": "Slack Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}