{
  "name": "Coffee & Peppers - Email AI",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread",
          "sender": ""
        },
        "options": {
          "downloadAttachments": false
        }
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        -200,
        0
      ],
      "id": "gmail-trigger",
      "name": "Gmail - New Email",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json.text || $input.first().json.snippet || '';\nconst subject = $input.first().json.subject || '';\nconst from = $input.first().json.from || '';\nconst threadId = $input.first().json.threadId || '';\nconst messageId = $input.first().json.id || '';\n\n// Extract order number patterns like #12345, order 12345, order #12345\nconst orderMatch = (body + ' ' + subject).match(/#?(\\d{4,6})|order\\s*#?(\\d{4,6})/i);\nconst orderNumber = orderMatch ? orderMatch[1] || orderMatch[2] : null;\n\n// Extract customer email from the From field\nconst emailMatch = from.match(/<(.+?)>/) || [null, from];\nconst customerEmail = emailMatch[1];\n\nreturn [{\n  json: {\n    body,\n    subject,\n    from,\n    threadId,\n    messageId,\n    orderNumber,\n    customerEmail\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        20,
        0
      ],
      "id": "parse-email",
      "name": "Parse Email & Extract Order #"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false
          },
          "conditions": [
            {
              "leftValue": "={{ $json.orderNumber }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "isNotEmpty"
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        240,
        0
      ],
      "id": "has-order-number",
      "name": "Has Order Number?"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://coffeeandpeppers.com/wp-json/wc/v3/orders/{{ $json.orderNumber }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        -120
      ],
      "id": "get-order",
      "name": "Get WooCommerce Order",
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Build order summary for AI context\nconst order = $input.first().json;\nconst emailData = $('Parse Email & Extract Order #').first().json;\n\nlet orderSummary = 'No order data available.';\nif (order && order.id) {\n  const items = (order.line_items || []).map(i => `${i.quantity}x ${i.name}`).join(', ');\n  const shipping = order.shipping_lines?.[0]?.method_title || 'standard';\n  const trackingNumber = order.meta_data?.find(m => m.key === '_wc_shipment_tracking_items')?.value?.[0]?.tracking_number || 'not available';\n  orderSummary = `Order #${order.id} | Status: ${order.status} | Items: ${items} | Shipping: ${shipping} | Tracking: ${trackingNumber} | Total: $${order.total}`;\n}\n\nreturn [{\n  json: {\n    ...emailData,\n    orderSummary\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        -120
      ],
      "id": "format-order",
      "name": "Format Order Data"
    },
    {
      "parameters": {
        "jsCode": "const emailData = $input.first().json;\nreturn [{\n  json: {\n    ...emailData,\n    orderSummary: 'No order number found in email.'\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        120
      ],
      "id": "no-order",
      "name": "No Order - Pass Through"
    },
    {
      "parameters": {
        "mode": "combine",
        "combinationMode": "mergeByPosition",
        "options": {}
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        900,
        0
      ],
      "id": "merge",
      "name": "Merge"
    },
    {
      "parameters": {
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a friendly, casual customer service AI for Coffee & Peppers (coffeeandpeppers.com), a peptide and supplement company. Your job is to:\n\n1. CLASSIFY the email into one of these categories:\n   - order_status (asking where their order is)\n   - missing_items (received incomplete kit or wrong items)\n   - wrong_address (address error on order)\n   - dosing_medical (ANY dosing, medical, or health advice questions)\n   - product_question (general product info, availability, batch tests)\n   - refund_return (requesting refund or return)\n   - other (anything that doesn't fit above)\n\n2. Determine a CONFIDENCE score (0.0 to 1.0) - how confident are you that your response is complete and correct without human review?\n   - High confidence (0.8+): Simple factual replies, dosing deflections, order status with full data\n   - Low confidence (below 0.8): Missing items, refunds, complaints, anything requiring a business decision\n\n3. Draft a RESPONSE that is casual and friendly in tone. Sign off as 'The C&P Team'.\n\nSpecial rules:\n- For dosing_medical: Always respond with exactly: \"Hey [name]! We totally get the curiosity, but we're not able to give medical or dosing advice - that's something to chat with your healthcare provider about! Is there anything else we can help you with? - The C&P Team\"\n- For missing_items or wrong_address: Draft a response but keep confidence LOW (0.4) since a human needs to verify and decide on reship/refund\n- For refund_return: Draft empathetically, keep confidence LOW (0.3)\n- Never promise specific outcomes (refund, reship) without human approval\n\nReturn ONLY valid JSON with this exact structure:\n{\n  \"category\": \"<category>\",\n  \"confidence\": <number>,\n  \"response\": \"<full email response>\",\n  \"summary\": \"<one sentence summary for the team>\"\n}"
            },
            {
              "role": "user",
              "content": "=Customer Email:\nFrom: {{ $json.from }}\nSubject: {{ $json.subject }}\nMessage: {{ $json.body }}\n\nOrder Data: {{ $json.orderSummary }}\n\nPlease classify and draft a response."
            }
          ]
        },
        "options": {
          "temperature": 0.3
        }
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.7,
      "position": [
        1120,
        0
      ],
      "id": "ai-classify",
      "name": "AI - Classify & Draft Response",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json.message?.content || $input.first().json.choices?.[0]?.message?.content || '{}';\nlet parsed;\ntry {\n  // Strip markdown code fences if present\n  const cleaned = raw.replace(/```json\\n?/g, '').replace(/```\\n?/g, '').trim();\n  parsed = JSON.parse(cleaned);\n} catch(e) {\n  parsed = { category: 'other', confidence: 0.1, response: raw, summary: 'Parse error - review manually' };\n}\n\nconst emailData = $('Merge').first().json;\n\nreturn [{\n  json: {\n    ...emailData,\n    category: parsed.category,\n    confidence: parsed.confidence,\n    aiResponse: parsed.response,\n    summary: parsed.summary,\n    needsReview: parsed.confidence < 0.75\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        0
      ],
      "id": "parse-ai",
      "name": "Parse AI Response"
    },
    {
      "parameters": {
        "sendTo": "={{ $json.customerEmail }}",
        "subject": "=Re: {{ $json.subject }}",
        "emailType": "text",
        "message": "={{ $json.aiResponse }}",
        "options": {
          "threadId": "={{ $json.threadId }}",
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1560,
        -160
      ],
      "id": "create-draft",
      "name": "Create Gmail Draft",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ $json.needsReview }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equal"
              }
            }
          ]
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1560,
        160
      ],
      "id": "needs-review",
      "name": "Needs Human Review?"
    },
    {
      "parameters": {
        "sendTo": "coffeepepp@gmail.com, coltonteefy@gmail.com",
        "subject": "=[REVIEW NEEDED] {{ $json.category }} - {{ $json.subject }}",
        "emailType": "text",
        "message": "=A customer email needs your review:\n\nFrom: {{ $json.from }}\nSubject: {{ $json.subject }}\nCategory: {{ $json.category }}\nAI Summary: {{ $json.summary }}\n\n---\nDraft reply is ready in Gmail drafts. Review and send when ready.\n\nOriginal message:\n{{ $json.body }}",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1780,
        80
      ],
      "id": "notify-team",
      "name": "Notify Dan & Colton",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Gmail - New Email": {
      "main": [
        [
          {
            "node": "Parse Email & Extract Order #",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Email & Extract Order #": {
      "main": [
        [
          {
            "node": "Has Order Number?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Order Number?": {
      "main": [
        [
          {
            "node": "Get WooCommerce Order",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Order - Pass Through",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get WooCommerce Order": {
      "main": [
        [
          {
            "node": "Format Order Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Order Data": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Order - Pass Through": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "AI - Classify & Draft Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI - Classify & Draft Response": {
      "main": [
        [
          {
            "node": "Parse AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Response": {
      "main": [
        [
          {
            "node": "Create Gmail Draft",
            "type": "main",
            "index": 0
          },
          {
            "node": "Needs Human Review?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Needs Human Review?": {
      "main": [
        [
          {
            "node": "Notify Dan & Colton",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}