{
  "name": "UPC Validation Workflow",
  "nodes": [
    {
      "id": 1,
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -200,
        300
      ],
      "parameters": {}
    },
    {
      "id": 2,
      "name": "Read Input Excel",
      "type": "n8n-nodes-base.microsoftExcel",
      "typeVersion": 2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "resource": "worksheet",
        "operation": "readRows",
        "workbook": {
          "value": "<OneDrive_File_ID_or_URL>",
          "mode": "identify"
        },
        "worksheet": {
          "value": "<Input_Sheet_Name_or_ID>",
          "mode": "identify"
        }
      },
      "credentials": {
        "microsoftExcelOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": 3,
      "name": "Validate Fields & Initial Flags",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        200,
        300
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// \u2026 original validation code \u2026"
      }
    },
    {
      "id": 4,
      "name": "Deduplicate UPCs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        400,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// \u2026 original dedupe code \u2026"
      }
    },
    {
      "id": 5,
      "name": "Filter Valid Primary",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        620,
        300
      ],
      "parameters": {
        "conditions": {
          "all": [
            {
              "value1": "={{ $json.valid }}",
              "operation": "true"
            },
            {
              "value1": "={{ $json.duplicate }}",
              "operation": "false"
            }
          ]
        }
      }
    },
    {
      "id": 6,
      "name": "Google Search API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        840,
        200
      ],
      "parameters": {
        "url": "https://customsearch.googleapis.com/customsearch/v1",
        "method": "GET",
        "queryParameters": [
          {
            "name": "key",
            "value": "={{ $env.GOOGLE_API_KEY || '<YOUR_GOOGLE_API_KEY>' }}"
          },
          {
            "name": "cx",
            "value": "={{ $env.GOOGLE_CSE_ID || '<YOUR_GOOGLE_CSE_ID>' }}"
          },
          {
            "name": "q",
            "value": "={{ $json.UPC + ' ' + $json[\"Product Name\"] }}"
          },
          {
            "name": "num",
            "value": "10"
          }
        ],
        "options": {
          "continueOnFail": true
        }
      }
    },
    {
      "id": 7,
      "name": "Check Snippet Similarity",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1020,
        200
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// \u2026 original similarity code \u2026"
      }
    },
    {
      "id": 8,
      "name": "Re-evaluate Duplicates (Post-Google)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        1200,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// \u2026 original re-evaluation code \u2026"
      }
    },
    {
      "id": 9,
      "name": "Route to Processed vs Unqualified",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1400,
        300
      ],
      "parameters": {
        "conditions": {
          "all": [
            {
              "value1": "={{ $json.valid }}",
              "operation": "true"
            },
            {
              "value1": "={{ $json.duplicate }}",
              "operation": "false"
            },
            {
              "value1": "={{ $json.confirmed }}",
              "operation": "true"
            }
          ]
        }
      }
    },
    {
      "id": 10,
      "name": "Append to Processed",
      "type": "n8n-nodes-base.microsoftExcel",
      "typeVersion": 2,
      "position": [
        1600,
        200
      ],
      "parameters": {
        "resource": "worksheet",
        "operation": "appendRows",
        "workbook": {
          "value": "<OneDrive_File_ID_or_URL>",
          "mode": "identify"
        },
        "worksheet": {
          "value": "<Processed_Sheet_Name_or_ID>",
          "mode": "identify"
        },
        "options": {
          "continueOnFail": true
        }
      },
      "credentials": {
        "microsoftExcelOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": 11,
      "name": "Append to Unqualified",
      "type": "n8n-nodes-base.microsoftExcel",
      "typeVersion": 2,
      "position": [
        1600,
        400
      ],
      "parameters": {
        "resource": "worksheet",
        "operation": "appendRows",
        "workbook": {
          "value": "<OneDrive_File_ID_or_URL>",
          "mode": "identify"
        },
        "worksheet": {
          "value": "<Unqualified_Sheet_Name_or_ID>",
          "mode": "identify"
        },
        "options": {
          "continueOnFail": true
        }
      },
      "credentials": {
        "microsoftExcelOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": 12,
      "name": "Log Error",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 1,
      "position": [
        1200,
        0
      ],
      "parameters": {
        "operation": "append",
        "documentId": "<Google_Sheet_ID_for_Error_Log>",
        "sheetName": "<Error_Log_Sheet_Name>",
        "fields": [
          {
            "name": "Timestamp",
            "value": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}"
          },
          {
            "name": "Node Name",
            "value": "={{ $json.Node || 'Unknown Node' }}"
          },
          {
            "name": "UPC",
            "value": "={{ $json.UPC || 'N/A' }}"
          },
          {
            "name": "Error Message",
            "value": "={{ $json.Error || 'Unknown Error' }}"
          }
        ]
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Read Input Excel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Input Excel": {
      "main": [
        [
          {
            "node": "Validate Fields & Initial Flags",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Fields & Initial Flags": {
      "main": [
        [
          {
            "node": "Deduplicate UPCs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deduplicate UPCs": {
      "main": [
        [
          {
            "node": "Filter Valid Primary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Valid Primary": {
      "main": [
        [
          {
            "node": "Google Search API",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append to Unqualified",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Search API": {
      "main": [
        [
          {
            "node": "Check Snippet Similarity",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Snippet Similarity": {
      "main": [
        [
          {
            "node": "Re-evaluate Duplicates (Post-Google)",
            "type": "main",
            "index": 0
          }
        ]
      ],
      "error": [
        [
          {
            "node": "Log Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Re-evaluate Duplicates (Post-Google)": {
      "main": [
        [
          {
            "node": "Route to Processed vs Unqualified",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route to Processed vs Unqualified": {
      "main": [
        [
          {
            "node": "Append to Processed",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append to Unqualified",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append to Processed": {
      "main": [],
      "error": [
        [
          {
            "node": "Log Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append to Unqualified": {
      "main": [],
      "error": [
        [
          {
            "node": "Log Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}