{
  "name": "App Wizard Academy — Inventory Agent (All Sites)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "inventory-trigger",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [-176, -32],
      "webhookId": "inventory-trigger"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id AS site_uuid, name AS site_name, sitemap_url FROM site_config WHERE agent_system_enabled = true;",
        "additionalFields": {}
      },
      "id": "load-sites",
      "name": "Load Sites",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [48, -32],
      "credentials": {
        "postgres": { "id": "REPLACE_WITH_YOUR_CREDENTIAL_ID", "name": "YOUR_POSTGRES_OR_SUPABASE_CREDENTIAL" }
      }
    },
    {
      "parameters": { "options": {} },
      "id": "current-site",
      "name": "Current Site",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [272, -32]
    },
    {
      "parameters": {
        "url": "={{ $json.sitemap_url }}",
        "options": { "response": { "response": { "responseFormat": "text" } }, "timeout": 15000 }
      },
      "id": "fetch-sitemap",
      "name": "HTTP: fetch sitemap",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [496, -32]
    },
    {
      "parameters": { "options": {} },
      "id": "xml-to-json",
      "name": "XML to JSON",
      "type": "n8n-nodes-base.xml",
      "typeVersion": 1,
      "position": [720, -32]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst config = $('Current Site').first().json;\nconst EXCLUDED = ['/privacy','/terms','/login','/sign-in','/sign-up','/dashboard','/api'];\n\nconst urlset = data?.urlset?.url ?? data?.url ?? [];\nconst entries = Array.isArray(urlset) ? urlset : [urlset];\n\nfunction isExcluded(pathname) {\n  return EXCLUDED.some((ex) => pathname === ex || pathname.startsWith(ex + '/'));\n}\nfunction getPathname(url) {\n  const m = url.match(/^https?:\\/\\/[^/]+(\\/.*)?$/);\n  return m ? (m[1] || '/') : null;\n}\n\nconst out = [];\nfor (const entry of entries) {\n  const url = typeof entry === 'string' ? entry : entry?.loc;\n  if (!url) continue;\n  const pathname = getPathname(url);\n  if (!pathname || isExcluded(pathname)) continue;\n  const slug = pathname === '/' ? '' : pathname.replace(/^\\//, '').replace(/\\/$/, '');\n  out.push({ json: { url, slug, pathname, site_uuid: config.site_uuid } });\n}\nreturn out;"
      },
      "id": "extract-urls",
      "name": "Extract URL List",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [944, -32]
    },
    {
      "parameters": { "batchSize": 10, "options": {} },
      "id": "split-batches",
      "name": "Split In Batches (10)",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [1168, -32]
    },
    {
      "parameters": {
        "url": "={{ $json.url }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [{ "name": "User-Agent", "value": "EmpireEngine-Inventory/1.0" }]
        },
        "options": { "response": { "response": { "responseFormat": "text" } }, "timeout": 15000 }
      },
      "id": "fetch-html",
      "name": "HTTP: fetch page HTML",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1392, -32],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const html = $input.item.json.data || $input.item.json.body || '';\nconst upstream = $('Extract URL List').item.json;\n\nfunction pick(re, fallback = null) { const m = html.match(re); return m ? m[1].trim() : fallback; }\nconst title = pick(/<title[^>]*>([\\s\\S]*?)<\\/title>/i);\nconst metaDescription = pick(/<meta[^>]+name=[\"']description[\"'][^>]+content=[\"']([^\"']+)[\"']/i);\n\nconst cleaned = html.replace(/<script[\\s\\S]*?<\\/script>/gi, '').replace(/<style[\\s\\S]*?<\\/style>/gi, '').replace(/<[^>]+>/g, ' ').replace(/\\s+/g, ' ').trim();\nconst wordCount = cleaned ? cleaned.split(' ').length : 0;\n\nconst allHrefs = [...html.matchAll(/href=[\"']([^\"']+)[\"']/gi)].map((m) => m[1]);\nlet origin; try { origin = new URL(upstream.url).origin; } catch { origin = ''; }\nconst internal = allHrefs.filter((h) => h.startsWith('/') || (origin && h.startsWith(origin)));\nconst external = allHrefs.filter((h) => /^https?:\\/\\//i.test(h) && (!origin || !h.startsWith(origin)));\nconst imageCount = (html.match(/<img\\s/gi) || []).length;\n\nfunction simpleHash(str) {\n  let h = 0;\n  for (let i = 0; i < str.length; i++) { h = Math.imul(31, h) + str.charCodeAt(i) | 0; }\n  return (h >>> 0).toString(16);\n}\n\nreturn {\n  json: {\n    site_id: upstream.site_uuid,\n    slug: upstream.slug,\n    url: upstream.url,\n    title: title?.replace(/&amp;/g, '&') ?? null,\n    word_count: wordCount,\n    content_hash: simpleHash(cleaned),\n    meta_description: metaDescription,\n    internal_link_count: internal.length,\n    external_link_count: external.length,\n    image_count: imageCount\n  }\n};"
      },
      "id": "extract-metadata",
      "name": "Extract Page Metadata",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1616, -32]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "insert into pages (site_id, slug, url, title, word_count, content_hash, meta_description, internal_link_count, external_link_count, image_count, last_inventoried_at) values ('{{ $json.site_id }}', '{{ $json.slug }}', '{{ $json.url }}', $title${{ $json.title }}$title$, {{ $json.word_count }}, '{{ $json.content_hash }}', $md${{ $json.meta_description }}$md$, {{ $json.internal_link_count }}, {{ $json.external_link_count }}, {{ $json.image_count }}, now()) on conflict (site_id, slug) do update set url = excluded.url, title = coalesce(excluded.title, pages.title), word_count = excluded.word_count, content_hash = excluded.content_hash, meta_description = coalesce(excluded.meta_description, pages.meta_description), internal_link_count = excluded.internal_link_count, external_link_count = excluded.external_link_count, image_count = excluded.image_count, last_inventoried_at = now();",
        "additionalFields": {}
      },
      "id": "upsert-pages",
      "name": "Postgres: upsert pages",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [1840, -32],
      "credentials": {
        "postgres": { "id": "REPLACE_WITH_YOUR_CREDENTIAL_ID", "name": "YOUR_POSTGRES_OR_SUPABASE_CREDENTIAL" }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"status\": \"ok\", \"message\": \"Inventory complete\", \"ts\": \"{{ new Date().toISOString() }}\" }",
        "options": {}
      },
      "id": "respond",
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [496, 160]
    }
  ],
  "connections": {
    "Webhook": { "main": [[{ "node": "Load Sites", "type": "main", "index": 0 }]] },
    "Load Sites": { "main": [[{ "node": "Current Site", "type": "main", "index": 0 }]] },
    "Current Site": {
      "main": [
        [{ "node": "Respond", "type": "main", "index": 0 }],
        [{ "node": "HTTP: fetch sitemap", "type": "main", "index": 0 }]
      ]
    },
    "HTTP: fetch sitemap": { "main": [[{ "node": "XML to JSON", "type": "main", "index": 0 }]] },
    "XML to JSON": { "main": [[{ "node": "Extract URL List", "type": "main", "index": 0 }]] },
    "Extract URL List": { "main": [[{ "node": "Split In Batches (10)", "type": "main", "index": 0 }]] },
    "Split In Batches (10)": {
      "main": [
        [{ "node": "Current Site", "type": "main", "index": 0 }],
        [{ "node": "HTTP: fetch page HTML", "type": "main", "index": 0 }]
      ]
    },
    "HTTP: fetch page HTML": { "main": [[{ "node": "Extract Page Metadata", "type": "main", "index": 0 }]] },
    "Extract Page Metadata": { "main": [[{ "node": "Postgres: upsert pages", "type": "main", "index": 0 }]] },
    "Postgres: upsert pages": { "main": [[{ "node": "Split In Batches (10)", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "versionId": "empire-engine-inventory-agent-v1",
  "meta": { "instanceId": "empire-engine-template" },
  "id": "empire-engine-inventory-agent",
  "tags": []
}
