{
  "name": "Phase 1 - Discovery & Tiering",
  "nodes": [
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9c346e62-e5b1-45f8-928a-4993632c6350",
              "name": "supabaseUrl",
              "value": "https://YOUR_PROJECT_REF.supabase.co",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "250e1c0e-6a1e-4cbf-bfd0-b3d998f7b5bb",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        688,
        512
      ]
    },
    {
      "parameters": {
        "jsCode": "// Tiering logic (explainable). Points -> tier, with a written reason per school.\nconst majorHubs = [\"Dubai\",\"Abu Dhabi\",\"Doha\",\"Singapore\",\"Hong Kong\",\"London\",\"New York\",\"Shanghai\",\"Bangkok\",\"Mumbai\",\"New Delhi\"];\nconst metros = [\"Bengaluru\",\"Bangalore\",\"Chennai\",\"Pune\",\"Hyderabad\"];\nconst internationalBoards = [\"IB\",\"Cambridge\",\"American/AP\"];\nconst currentYear = new Date().getFullYear();\n\nfunction uuid() {\n  return \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(/[xy]/g, function (c) {\n    const r = (Math.random() * 16) | 0;\n    const v = c === \"x\" ? r : (r & 0x3) | 0x8;\n    return v.toString(16);\n  });\n}\n\nconst out = [];\nfor (const item of $input.all()) {\n  const s = item.json;\n  let score = 0;\n  const reasons = [];\n\n  // 1) Curriculum / board\n  if (internationalBoards.includes(s.board)) { score += 3; reasons.push(s.board + \" international curriculum (+3)\"); }\n  else { reasons.push(s.board + \" national board (+0)\"); }\n\n  // 2) Location\n  let hub;\n  if (majorHubs.includes(s.city)) { score += 2; hub = \"major hub\"; reasons.push(s.city + \" is a major education hub (+2)\"); }\n  else if (metros.includes(s.city)) { score += 1; hub = \"metro\"; reasons.push(s.city + \" is a metro (+1)\"); }\n  else { hub = \"smaller city\"; reasons.push(s.city + \" is a smaller city (+0)\"); }\n\n  // 3) Established\n  const age = currentYear - s.established_year;\n  if (age > 20) { score += 2; reasons.push(\"established \" + s.established_year + \" (\" + age + \" yrs, +2)\"); }\n  else if (age >= 10) { score += 1; reasons.push(\"established \" + s.established_year + \" (\" + age + \" yrs, +1)\"); }\n  else { reasons.push(\"established \" + s.established_year + \" (\" + age + \" yrs, +0)\"); }\n\n  // 4) Size\n  if (s.student_count > 1500) { score += 2; reasons.push(s.student_count + \" students (+2)\"); }\n  else if (s.student_count >= 800) { score += 1; reasons.push(s.student_count + \" students (+1)\"); }\n  else { reasons.push(s.student_count + \" students (+0)\"); }\n\n  // Tier bands\n  let tier;\n  if (score >= 7) tier = 1;\n  else if (score >= 4) tier = 2;\n  else tier = 3;\n\n  out.push({\n    json: {\n      school_id: uuid(),\n      name: s.school_name,\n      country: s.country,\n      city: s.city,\n      education_hub: hub,\n      board: s.board,\n      student_count: s.student_count,\n      website: s.website,\n      tier: tier,\n      tier_score: score,\n      tier_reason: \"Tier \" + tier + \" (score \" + score + \"/9): \" + reasons.join(\"; \"),\n      contact_name: s.contact_name,\n      contact_role: s.contact_role,\n      linkedin_url: s.linkedin_url,\n      email: s.email\n    }\n  });\n}\nreturn out;"
      },
      "id": "3413b1a2-3cf8-490c-8617-1531d5e6010d",
      "name": "Tiering",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1136,
        512
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Config').first().json.supabaseUrl }}/rest/v1/schools",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Prefer",
              "value": "return=minimal"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $('Build Payloads').first().json.schools }}",
        "options": {}
      },
      "id": "2e0291ed-616f-4bc9-a6d8-0af149154f03",
      "name": "Insert Schools",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1568,
        512
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Collapse 18 tiered items into two arrays for bulk insert (schools + contacts).\nconst rows = $input.all().map(i => i.json);\nconst schools = rows.map(r => ({\n  id: r.school_id,\n  name: r.name,\n  country: r.country,\n  city: r.city,\n  education_hub: r.education_hub,\n  board: r.board,\n  student_count: r.student_count,\n  website: r.website,\n  tier: r.tier,\n  tier_score: r.tier_score,\n  tier_reason: r.tier_reason\n}));\nconst contacts = rows.map(r => ({\n  school_id: r.school_id,\n  name: r.contact_name,\n  role: r.contact_role,\n  linkedin_url: r.linkedin_url,\n  email: r.email\n}));\nreturn [{ json: { schools: schools, contacts: contacts } }];"
      },
      "id": "5ecc1480-415c-447b-98df-87983205e1f4",
      "name": "Build Payloads",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1344,
        512
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Config').first().json.supabaseUrl }}/rest/v1/contacts",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Prefer",
              "value": "return=minimal"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $('Build Payloads').first().json.contacts }}",
        "options": {}
      },
      "id": "f8b9ec00-19fb-4c40-9ba8-f47276ec6812",
      "name": "Insert Contacts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1792,
        512
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Mock discovery step: returns a sample set of schools (no live scraping).\nconst schools = [\n  {\n    \"school_name\": \"GEMS Wellington International School\",\n    \"country\": \"United Arab Emirates\",\n    \"city\": \"Dubai\",\n    \"board\": \"Cambridge\",\n    \"established_year\": 2007,\n    \"student_count\": 2500,\n    \"website\": \"https://www.gemswellington.com\",\n    \"contact_name\": \"Rebecca Turner\",\n    \"contact_role\": \"Head of Senior School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/rebecca-turner-8a4f21\",\n    \"email\": \"rebecca.turner@gemswellington.com\"\n  },\n  {\n    \"school_name\": \"Dubai American Academy\",\n    \"country\": \"United Arab Emirates\",\n    \"city\": \"Dubai\",\n    \"board\": \"American/AP\",\n    \"established_year\": 1998,\n    \"student_count\": 2000,\n    \"website\": \"https://www.gemsaa-dubai.com\",\n    \"contact_name\": \"Michael Brennan\",\n    \"contact_role\": \"Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/michael-brennan-6c19d0\",\n    \"email\": \"michael.brennan@gemsaa-dubai.com\"\n  },\n  {\n    \"school_name\": \"United World College of South East Asia\",\n    \"country\": \"Singapore\",\n    \"city\": \"Singapore\",\n    \"board\": \"IB\",\n    \"established_year\": 1971,\n    \"student_count\": 5000,\n    \"website\": \"https://www.uwcsea.edu.sg\",\n    \"contact_name\": \"Sarah Whitmore\",\n    \"contact_role\": \"Head of College\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/sarah-whitmore-3f72ba\",\n    \"email\": \"sarah.whitmore@uwcsea.edu.sg\"\n  },\n  {\n    \"school_name\": \"Tanglin Trust School\",\n    \"country\": \"Singapore\",\n    \"city\": \"Singapore\",\n    \"board\": \"IB\",\n    \"established_year\": 1925,\n    \"student_count\": 2800,\n    \"website\": \"https://www.tts.edu.sg\",\n    \"contact_name\": \"David Ashcroft\",\n    \"contact_role\": \"Head of Senior School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/david-ashcroft-9b41ce\",\n    \"email\": \"david.ashcroft@tts.edu.sg\"\n  },\n  {\n    \"school_name\": \"Dhirubhai Ambani International School\",\n    \"country\": \"India\",\n    \"city\": \"Mumbai\",\n    \"board\": \"IB\",\n    \"established_year\": 2003,\n    \"student_count\": 1200,\n    \"website\": \"https://www.da-is.org\",\n    \"contact_name\": \"Anjali Deshpande\",\n    \"contact_role\": \"Head of School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/anjali-deshpande-2d88f4\",\n    \"email\": \"anjali.deshpande@da-is.org\"\n  },\n  {\n    \"school_name\": \"The British School, New Delhi\",\n    \"country\": \"India\",\n    \"city\": \"New Delhi\",\n    \"board\": \"IB\",\n    \"established_year\": 1963,\n    \"student_count\": 1100,\n    \"website\": \"https://www.british-school.org\",\n    \"contact_name\": \"Emma Caldwell\",\n    \"contact_role\": \"Head of School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/emma-caldwell-7a3e12\",\n    \"email\": \"emma.caldwell@british-school.org\"\n  },\n  {\n    \"school_name\": \"American School of Bombay\",\n    \"country\": \"India\",\n    \"city\": \"Mumbai\",\n    \"board\": \"IB\",\n    \"established_year\": 1981,\n    \"student_count\": 1300,\n    \"website\": \"https://www.asbindia.org\",\n    \"contact_name\": \"Rohan Iyer\",\n    \"contact_role\": \"Secondary School Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/rohan-iyer-5e01a9\",\n    \"email\": \"rohan.iyer@asbindia.org\"\n  },\n  {\n    \"school_name\": \"Harrow International School Hong Kong\",\n    \"country\": \"Hong Kong\",\n    \"city\": \"Hong Kong\",\n    \"board\": \"Cambridge\",\n    \"established_year\": 2012,\n    \"student_count\": 1600,\n    \"website\": \"https://www.harrowschool.hk\",\n    \"contact_name\": \"Charlotte Meng\",\n    \"contact_role\": \"Head of Senior School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/charlotte-meng-4c72e8\",\n    \"email\": \"charlotte.meng@harrowschool.hk\"\n  },\n  {\n    \"school_name\": \"Delhi Public School R.K. Puram\",\n    \"country\": \"India\",\n    \"city\": \"New Delhi\",\n    \"board\": \"CBSE\",\n    \"established_year\": 1972,\n    \"student_count\": 10000,\n    \"website\": \"https://www.dpsrkp.net\",\n    \"contact_name\": \"Vikram Malhotra\",\n    \"contact_role\": \"Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/vikram-malhotra-1b93d7\",\n    \"email\": \"vikram.malhotra@dpsrkp.net\"\n  },\n  {\n    \"school_name\": \"The Cathedral and John Connon School\",\n    \"country\": \"India\",\n    \"city\": \"Mumbai\",\n    \"board\": \"ICSE\",\n    \"established_year\": 1860,\n    \"student_count\": 1400,\n    \"website\": \"https://www.cathedral-school.com\",\n    \"contact_name\": \"Farida Contractor\",\n    \"contact_role\": \"Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/farida-contractor-8f21b6\",\n    \"email\": \"farida.contractor@cathedral-school.com\"\n  },\n  {\n    \"school_name\": \"Oberoi International School\",\n    \"country\": \"India\",\n    \"city\": \"Mumbai\",\n    \"board\": \"IB\",\n    \"established_year\": 2008,\n    \"student_count\": 700,\n    \"website\": \"https://www.oberoi-is.org\",\n    \"contact_name\": \"Neha Kapoor\",\n    \"contact_role\": \"University Counsellor\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/neha-kapoor-3a67c1\",\n    \"email\": \"neha.kapoor@oberoi-is.org\"\n  },\n  {\n    \"school_name\": \"Inventure Academy\",\n    \"country\": \"India\",\n    \"city\": \"Bengaluru\",\n    \"board\": \"IB\",\n    \"established_year\": 2009,\n    \"student_count\": 750,\n    \"website\": \"https://www.inventureacademy.com\",\n    \"contact_name\": \"Arjun Rao\",\n    \"contact_role\": \"Head of School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/arjun-rao-9d42f8\",\n    \"email\": \"arjun.rao@inventureacademy.com\"\n  },\n  {\n    \"school_name\": \"Chettinad Vidyashram\",\n    \"country\": \"India\",\n    \"city\": \"Chennai\",\n    \"board\": \"CBSE\",\n    \"established_year\": 1985,\n    \"student_count\": 3000,\n    \"website\": \"https://www.chettinadvidyashram.org\",\n    \"contact_name\": \"Lakshmi Narayanan\",\n    \"contact_role\": \"Vice Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/lakshmi-narayanan-6b18e3\",\n    \"email\": \"lakshmi.narayanan@chettinadvidyashram.org\"\n  },\n  {\n    \"school_name\": \"Ecole Mondiale World School\",\n    \"country\": \"India\",\n    \"city\": \"Mumbai\",\n    \"board\": \"IB\",\n    \"established_year\": 2010,\n    \"student_count\": 600,\n    \"website\": \"https://www.ecolemondiale.org\",\n    \"contact_name\": \"Sanjana Mehta\",\n    \"contact_role\": \"IB DP Coordinator\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/sanjana-mehta-2f74a0\",\n    \"email\": \"sanjana.mehta@ecolemondiale.org\"\n  },\n  {\n    \"school_name\": \"The Doon School\",\n    \"country\": \"India\",\n    \"city\": \"Dehradun\",\n    \"board\": \"ICSE\",\n    \"established_year\": 1935,\n    \"student_count\": 550,\n    \"website\": \"https://www.doonschool.com\",\n    \"contact_name\": \"Aditya Rana\",\n    \"contact_role\": \"Housemaster & Science Teacher\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/aditya-rana-7c39b5\",\n    \"email\": \"aditya.rana@doonschool.com\"\n  },\n  {\n    \"school_name\": \"Jayshree Periwal International School\",\n    \"country\": \"India\",\n    \"city\": \"Jaipur\",\n    \"board\": \"CBSE\",\n    \"established_year\": 2006,\n    \"student_count\": 3000,\n    \"website\": \"https://www.jpischool.com\",\n    \"contact_name\": \"Meera Sharma\",\n    \"contact_role\": \"Vice Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/meera-sharma-4a81d2\",\n    \"email\": \"meera.sharma@jpischool.com\"\n  },\n  {\n    \"school_name\": \"Sanskar School\",\n    \"country\": \"India\",\n    \"city\": \"Jaipur\",\n    \"board\": \"CBSE\",\n    \"established_year\": 2000,\n    \"student_count\": 1600,\n    \"website\": \"https://www.sanskarschool.edu.in\",\n    \"contact_name\": \"Priya Agarwal\",\n    \"contact_role\": \"Head of Senior School\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/priya-agarwal-8e26f1\",\n    \"email\": \"priya.agarwal@sanskarschool.edu.in\"\n  },\n  {\n    \"school_name\": \"The Millennium School\",\n    \"country\": \"India\",\n    \"city\": \"Kurukshetra\",\n    \"board\": \"CBSE\",\n    \"established_year\": 2011,\n    \"student_count\": 900,\n    \"website\": \"https://www.themillenniumschools.com\",\n    \"contact_name\": \"Rajesh Kumar\",\n    \"contact_role\": \"Principal\",\n    \"linkedin_url\": \"https://www.linkedin.com/in/rajesh-kumar-3b57c9\",\n    \"email\": \"rajesh.kumar@themillenniumschools.com\"\n  }\n];\nreturn schools.map(s => ({ json: s }));"
      },
      "id": "bb2b1adc-e265-4776-bc74-f6eb3debeadf",
      "name": "Sample Schools",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        912,
        512
      ]
    },
    {
      "parameters": {},
      "id": "e55df126-2cfc-4e52-94da-0e29e7cd3b84",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        468,
        512
      ]
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Sample Schools",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Schools": {
      "main": [
        [
          {
            "node": "Tiering",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tiering": {
      "main": [
        [
          {
            "node": "Build Payloads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Payloads": {
      "main": [
        [
          {
            "node": "Insert Schools",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Schools": {
      "main": [
        [
          {
            "node": "Insert Contacts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}