{
  "name": "AI Unsubscribe Handler",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        },
        "options": {}
      },
      "id": "b1b2c3d4-2004-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": "Analyze this email to determine if the sender wants to unsubscribe from our mailing list.\n\nFrom: {{ $json.from }}\nSubject: {{ $json.subject }}\nBody: {{ $json.snippet }}\n\nReturn JSON with keys:\n- isUnsubscribeRequest (boolean)\n- confidence (0-1)\n- detectedIntent (string: 'unsubscribe', 'complaint', 'reduce_frequency', 'other')\n- suggestedAction (string)"
            }
          ]
        },
        "options": {
          "temperature": 0.2,
          "maxTokens": 300
        }
      },
      "id": "b1b2c3d4-2004-4000-8000-000000000002",
      "name": "OpenAI Detect Intent",
      "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    ...response,\n    from: emailData.from,\n    subject: emailData.subject,\n    messageId: emailData.id,\n    receivedAt: new Date().toISOString()\n  }\n}];"
      },
      "id": "b1b2c3d4-2004-4000-8000-000000000003",
      "name": "Parse Intent",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isUnsubscribeRequest }}",
              "value2": true
            }
          ]
        }
      },
      "id": "b1b2c3d4-2004-4000-8000-000000000004",
      "name": "Is Unsubscribe?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "your-unsubscribe-log-sheet-id"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "UnsubscribeLog"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Email": "={{ $json.from }}",
            "Subject": "={{ $json.subject }}",
            "Intent": "={{ $json.detectedIntent }}",
            "Confidence": "={{ $json.confidence }}",
            "Date": "={{ $json.receivedAt }}"
          }
        }
      },
      "id": "b1b2c3d4-2004-4000-8000-000000000005",
      "name": "Log to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.2,
      "position": [
        1250,
        200
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $json.from }}",
        "subject": "Unsubscribe Confirmation",
        "message": "Hi,\n\nWe've received your request to unsubscribe and have removed your email address from our mailing list. You will no longer receive marketing emails from us.\n\nIf this was done in error, you can re-subscribe at any time by visiting our website.\n\nBest regards,\nThe Team",
        "options": {}
      },
      "id": "b1b2c3d4-2004-4000-8000-000000000006",
      "name": "Send Confirmation",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1500,
        200
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "OpenAI Detect Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Detect Intent": {
      "main": [
        [
          {
            "node": "Parse Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Intent": {
      "main": [
        [
          {
            "node": "Is Unsubscribe?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Unsubscribe?": {
      "main": [
        [
          {
            "node": "Log to Sheets",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Log to Sheets": {
      "main": [
        [
          {
            "node": "Send Confirmation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}