{
  "name": "HML \u2014 Weekly review",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "weeksInterval": 1,
              "triggerAtDay": [
                6
              ],
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "cron-1",
      "name": "Saturday 09:00",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$env.SUPABASE_URL}}/rest/v1/v_brand_weekly_stats?week=gte.{{ $now.minus({weeks:1}).startOf('week').toISO() }}",
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{$env.SUPABASE_SERVICE_ROLE_KEY}}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{$env.SUPABASE_SERVICE_ROLE_KEY}}"
            }
          ]
        },
        "sendHeaders": true,
        "method": "GET",
        "options": {}
      },
      "id": "http-1",
      "name": "Pull brand weekly stats",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        440,
        220
      ]
    },
    {
      "parameters": {
        "url": "={{$env.SUPABASE_URL}}/rest/v1/v_provider_daily?day=gte.{{ $now.minus({days:7}).toISODate() }}",
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{$env.SUPABASE_SERVICE_ROLE_KEY}}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{$env.SUPABASE_SERVICE_ROLE_KEY}}"
            }
          ]
        },
        "sendHeaders": true,
        "method": "GET",
        "options": {}
      },
      "id": "http-2",
      "name": "Pull provider daily stats",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        440,
        380
      ]
    },
    {
      "parameters": {
        "functionCode": "// Score each brand (very small inline scorer; canonical scorer lives in core/scoring).\n// Inputs assumed: items[].brand_id, items[].sessions, items[].engagements, items[].conversions, items[].revenue\nconst weekly = $input.first().json;\nconst scored = (weekly||[]).map(row => {\n  const traffic = Math.min(1, (row.impressions||0) / 5000);\n  const engagement = Math.min(1, (row.engagements||0) / Math.max(1,row.impressions));\n  const conversion = Math.min(1, (row.conversions||0) / Math.max(1,row.clicks||1));\n  const monetisation = Math.min(1, (row.revenue||0) / 200);\n  const score = 0.30*traffic + 0.20*engagement + 0.25*monetisation - 0.15*0.3 - 0.10*0.2;\n  return { ...row, brand_score: Math.max(0, Math.min(1, score)) };\n});\nreturn scored.map(s => ({ json: s }));"
      },
      "id": "func-1",
      "name": "Score brands",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    },
    {
      "parameters": {
        "subject": "HML \u2014 Weekly review",
        "text": "=Brand scores for last week:\n\n{{$json}}",
        "options": {}
      },
      "id": "email-1",
      "name": "Email operator",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        980,
        300
      ]
    }
  ],
  "connections": {
    "Saturday 09:00": {
      "main": [
        [
          {
            "node": "Pull brand weekly stats",
            "type": "main",
            "index": 0
          },
          {
            "node": "Pull provider daily stats",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pull brand weekly stats": {
      "main": [
        [
          {
            "node": "Score brands",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Score brands": {
      "main": [
        [
          {
            "node": "Email operator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1",
  "id": "hml_weekly_review"
}