{
  "nodes": [
    {
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -4400,
        384
      ]
    },
    {
      "name": "Normalise & Prefilter",
      "type": "n8n-nodes-base.code",
      "position": [
        -4176,
        384
      ]
    },
    {
      "name": "Invoice Candidate?",
      "type": "n8n-nodes-base.if",
      "position": [
        -3968,
        384
      ]
    },
    {
      "name": "Stop: Not A Candidate",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -3744,
        560
      ]
    },
    {
      "name": "Has PDF Attachment?",
      "type": "n8n-nodes-base.if",
      "position": [
        -3744,
        304
      ]
    },
    {
      "name": "Extract PDF Text",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        -3520,
        208
      ]
    },
    {
      "name": "Attach PDF Text",
      "type": "n8n-nodes-base.code",
      "position": [
        -3296,
        208
      ]
    },
    {
      "name": "Use Email Body",
      "type": "n8n-nodes-base.code",
      "position": [
        -3520,
        432
      ]
    },
    {
      "name": "Text Quality Gate",
      "type": "n8n-nodes-base.code",
      "position": [
        -3088,
        304
      ]
    },
    {
      "name": "Text Usable?",
      "type": "n8n-nodes-base.if",
      "position": [
        -2864,
        304
      ]
    },
    {
      "name": "Read Processed IDs",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -2640,
        224
      ]
    },
    {
      "name": "Decide: Already Seen",
      "type": "n8n-nodes-base.code",
      "position": [
        -2416,
        224
      ]
    },
    {
      "name": "Already Processed?",
      "type": "n8n-nodes-base.if",
      "position": [
        -2208,
        224
      ]
    },
    {
      "name": "Stop: Already Processed",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -1984,
        112
      ]
    },
    {
      "name": "Build Claude Request",
      "type": "n8n-nodes-base.code",
      "position": [
        -1984,
        320
      ]
    },
    {
      "name": "Claude: Classify & Extract",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1760,
        320
      ]
    },
    {
      "name": "Parse Claude Response",
      "type": "n8n-nodes-base.code",
      "position": [
        -1536,
        240
      ]
    },
    {
      "name": "Parsed OK?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1328,
        240
      ]
    },
    {
      "name": "Is Invoice?",
      "type": "n8n-nodes-base.if",
      "position": [
        -1104,
        192
      ]
    },
    {
      "name": "Mark Not Invoice",
      "type": "n8n-nodes-base.code",
      "position": [
        -880,
        352
      ]
    },
    {
      "name": "Validate Fields",
      "type": "n8n-nodes-base.code",
      "position": [
        -880,
        112
      ]
    },
    {
      "name": "Read Existing Invoices",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -656,
        112
      ]
    },
    {
      "name": "Decide: Duplicate",
      "type": "n8n-nodes-base.code",
      "position": [
        -448,
        112
      ]
    },
    {
      "name": "Is Duplicate?",
      "type": "n8n-nodes-base.if",
      "position": [
        -224,
        112
      ]
    },
    {
      "name": "Build Invoice Row",
      "type": "n8n-nodes-base.code",
      "position": [
        0,
        0
      ]
    },
    {
      "name": "Append Invoice Row",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        224,
        0
      ]
    },
    {
      "name": "Build Exception Row",
      "type": "n8n-nodes-base.code",
      "position": [
        0,
        432
      ]
    },
    {
      "name": "Append Exception Row",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        224,
        432
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Normalise & Prefilter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalise & Prefilter": {
      "main": [
        [
          {
            "node": "Invoice Candidate?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Invoice Candidate?": {
      "main": [
        [
          {
            "node": "Has PDF Attachment?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Stop: Not A Candidate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has PDF Attachment?": {
      "main": [
        [
          {
            "node": "Extract PDF Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Use Email Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF Text": {
      "main": [
        [
          {
            "node": "Attach PDF Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Attach PDF Text": {
      "main": [
        [
          {
            "node": "Text Quality Gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use Email Body": {
      "main": [
        [
          {
            "node": "Text Quality Gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text Quality Gate": {
      "main": [
        [
          {
            "node": "Text Usable?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text Usable?": {
      "main": [
        [
          {
            "node": "Read Processed IDs",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Processed IDs": {
      "main": [
        [
          {
            "node": "Decide: Already Seen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decide: Already Seen": {
      "main": [
        [
          {
            "node": "Already Processed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Processed?": {
      "main": [
        [
          {
            "node": "Stop: Already Processed",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Claude Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Claude Request": {
      "main": [
        [
          {
            "node": "Claude: Classify & Extract",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude: Classify & Extract": {
      "main": [
        [
          {
            "node": "Parse Claude Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Claude Response": {
      "main": [
        [
          {
            "node": "Parsed OK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parsed OK?": {
      "main": [
        [
          {
            "node": "Is Invoice?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Invoice?": {
      "main": [
        [
          {
            "node": "Validate Fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mark Not Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Not Invoice": {
      "main": [
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Fields": {
      "main": [
        [
          {
            "node": "Read Existing Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Existing Invoices": {
      "main": [
        [
          {
            "node": "Decide: Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decide: Duplicate": {
      "main": [
        [
          {
            "node": "Is Duplicate?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Duplicate?": {
      "main": [
        [
          {
            "node": "Build Exception Row",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Invoice Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Invoice Row": {
      "main": [
        [
          {
            "node": "Append Invoice Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Exception Row": {
      "main": [
        [
          {
            "node": "Append Exception Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
