const { execFile } = require('child_process');
const fs = require('fs');

const BASE = 'https://search.griffin.pm';
const CSV_FILE = './casino_affiliate_sites.csv';

// 350 queries across every angle: regions, niches, languages, sub-topics
const Q = [
  // ===== GLOBAL EN BROAD (30) =====
  "best online casino review site",
  "top rated internet casinos compared website portal",
  "online casino bonus comparison list rated portal all",
  "compare rate gambling sites directory platform reviewed tested listed rank",
  "independent unbiased casino review trusted expert analyzed website",
  "safest licensed approved verified online gambling watchdog reviewed site portal",
  "comprehensive gambling guide reviews ratings portal database list reviewed compared ranked all",
  "professional tested internet casinos ranked listed website portal reviewed",
  "community player rated voted gambling review platform site top list",
  "fast payout withdrawal online casino websites compared best list ranked tested",
  "high roller exclusive VIP member program casino review comparison website",
  "newly launched recently opened gambling internet casino reviewed sites list all",
  "best mobile phone tablet device responsive casino app reviewed compared rated ranked",
  "live dealer croupier streamed real human host table online casino reviewed listed",
  "cryptocurrency bitcoin ethereum blockchain gambling site review website compared portal",
  "no deposit bonus free sign up bonus casino aggregator list rated reviewed all",
  "free spins reel slot machine offers comparison reviewed best rated top",
  "gambling information hub site guides reviews links platform directory listed ranked",
  "content publisher gambling review media outlet platform published listed compared rated",
  "top paying highest RTP return player payout rate online casino website ranked list",
  "instant play flash no download browser direct mobile internet gaming reviewed compare listed all best top",

  // ===== USA / CANADA (25) =====
  "legal real money internet gambling casino United States USA reviewed compared ranked rated listed portal",
  "New Jersey NJ best licensed gambling websites tested reviewed rated list all",
  "Pennsylvania PA legal license gaming casino sites online reviewed rated compared list all",
  "Michigan MI iGaming license approved gambling website reviewed rated listed all",
  "Delaware DE West Virginia WV legalized online casino portal reviewed rated list all best",
  "Virginia VA regulated gambling iGaming casino internet sites reviewed ranked rated test compared all",
  "Connecticut CT legalized gaming license casino online website reviewed ranked rate list all",
  "Maryland MA MD regulated legal real money gambling site review tested rated all compare best portal",
  "Illinois IN Indiana Iowa sweepstakes social casino review rated list compare test best all top",
  "Tennessee Kentucky North Carolina NC TN KY NC legalized gamble gaming website review compared listed all",
  "Nevada NV legal real money online internet gambling casino reviewed rate list compare test all top",
  "Arizona AZ New Mexico NM Arizona NM legalized gambling legal site tested reviewed rated compare all best portal",
  "sweepstakes social game bonus free coin gold VC win prize award USA review website ranked compared best all",
  "British Columbia BC Ontario ON Alberta AB Canada regulated gaming online casino website reviewed rank rate compared test top all list best portal",

  // ===== UK / IRELAND (15) =====
  "UK United Kingdom best legal gambling site license GC Commission licensed review compared rated tested analyzed all",
  "British recommended safe approved internet gambling website test portal ranked listed rated compared all",
  "Irish Ireland recommended online casino review test portal ranked rated listed compared all",
  "british bookmaker betting site combined casino gambling reviewed compared rated rank portal all best",
  "welcome bonus offer deposit match UK regulated gamble website test review compared analyzed rated list top best all portal",

  // ===== AU / NZ (10) =====
  "Australian interactive poker slot pokies online gambling website review listed top rated compare test ranked real money license ACMA all best",
  "New Zealand Kiwi best gambling gaming websites reviewed tested analyzed compared rank rate list top all internet portal",
  "Quebec QC Ontario ON regulated license gambling site portal reviewed analyzed test compared ranked list all real money casino best",

  // ===== GERMANY / AUSTRIA / SWITZERLAND (12) =====
  "online internet Casino Deutschland beste Website verglichen Liste getestet bewertet Vergleich Top Portal Alle Lizenzierte",
  "oesterreich osterreich casino glucksspiel webseite compare getested liste beste testat gmbh online vergleich",
  "schweiz switzerland deutsch casino netz webseite vergleiche getestet bewertet liste beste portal aller online spielhalle lizenzierte genehmigte erlaubte zugelassene gewerbeanmeldung bestes",

  // ===== NETHERLANDS / BELGIUM (10) =====
  "beste goedkoopste online internet Casino Nederland website vergelijking lijst alle beoordeeld getest gekwalificeerd gelicentieerd Ksa NVKS keurmerk",
  "belgie flanders vlaanderen Gelicenseerde Online Internet Speelgoed Casino review Beoordeel Lijst Vergelijk Getest Alle Goedkoopste Beste Test Erkenning Toezicht Kansspelautoriteit goed geteste",

  // ===== FRANCE / SPAIN / PORTUGAL / ITALY (12) =====
  "casino en ligne arnet France meilleur webportail compare testee liste Arret Autorite porteur license legale meilleures",
  "internet casino espana mejores sitio web comparativa listado resenia portal Aprobada Licenciado Legal Junta Andalucia Madrid Barcelona Valencia mejores",
  "portugal apostas cassino internet site avaliado lista comparacao regulamentado lizenzierte licencia legal Direcao Geral Apostas Jogos Autoridade Reguladora melhores",
  "ItaliaItaly scommesse gioco meglio sito web confronto recensione portale elenco ADM AAMS Agenzia Dogane Monopolio licenza autorizzato legale regolamentato migliori",

  // ===== NORDIC / EASTERN EU (16) =====
  "svenska swedish swede Finland finnens licensierade best casino jamforande hemsida recenserad listan Spelinspektionen OASIS Pelaajien Oikeusturva paras",
  "danisch dansk norsk norwegian beste bedste nettcasino online hjemmeside sammenligning anmeldt liste spilleregulering spilmyndigheten Natuspillere bedste bedst best",
  "poland polska kasyno internet najlepsze review polecone lista ranking oceniany akredytowana licencja regulacja krajowy hazardu najlepsze best",
  "cestinska slovack chech slovacia republika nejlepsi stranka portel porovnan hodnoceni seznam licence regulace uzna ana povoleny legalne schvaleny certifikovane overene akreditowane top",
  "magyarorszaga hungary magyar legjobb internet casino oldal osszehasonlitas szazadik lista engedelyezett szabalyozott hitesitett tanusitott felugyi allami hagosag engedelyezes megjo jobb jobbat jojobb legjobb",
  "rumenina rumunia romania romanian cazinouri jocuri comparativ review list portel liste site licenta autorizare regulat aprob certificate verificat garantat supravegherie best bun bine mai bune cele mai bune",

  // ===== Niche / Deep-crawl (40) =====
  "smaller independent niche gambling blog personal honest website opinion reviewed compared evaluated rank rate listed all tested analyzed assessed",
  "gambling forum recommended community voted trusted discussed internet casino site list compared ranked analyzed evaluated test reviewed all top best portal directory aggregator",
  "expert verified license check regulatory compliance certification approval accreditation gambling rating platform database list rank rated tested analyzed trustworthiness reliability security fair honest unbiased transparency independent verify approved certified regulated compliant safe secure protected trusted reliable",
  "poker room online gambling betting hybrid combination combine merge platform review aggregator compile listed portal ranked rate compare contrasted reviewed analyzed assessed tested all best license legal regulation compliance check verified validated confirmed authorized accredited approved",
  "sportsbook sports book combined casino gaming gambling play game review evaluation assessment aggregator compile list catalog portal directory database ranked rate rated compared contrast reviewed analyzed assessed checked inspected examined tested evaluated all best top rated licensed regulated listed compiled gathered collected assembled curated selected chosen picked hand-picked carefully meticulously thoroughly comprehensively",
  "slot machine reel symbol software vendor provider developer supplier manufacturer maker comparison website compared ranked evaluated assessed rate test examined inspected checked verified validated reviewed all license regulation compliance best top listed portal directory database aggregator hub central resource information source guide reference handbook manual encyclopedia compendium collection anthology volume compendium repository archive register record ledger journal account report diary log",
  "high roller premium exclusive VIP member loyalty reward tier level cashback rebate percent bonus promotion offer deal program casino site review compared contrast listed ranked rate rated analyzed tested all best license legal regulation compliance",
  "paypal visa mastercard credit card debit bank wire transfer e-wallet payment supported gambling website reviewed compared contrasted evaluated assessed analyzed inspected checked tested all license legal regulation compliance best top rated listed compiled database directory portal aggregator hub central resource guide reference handbook manual encyclopedia compendium repository archive register ledger journal report diary log chronicle history record entry item line row column field data information details",
  "safest withdrawal money payment method crypto bitcoin ethereum bank transfer card direct internet gambling website compared contrasted analyzed reviewed inspect checked test evaluated assessed rate ranked all license legal regulated compliant accredited verify fair trustworthy reliable transparent honest independent unbiased impartial balanced objective even tempered non-partisan unprejudiced equitable just reasonable sensible logical rational sound valid legitimate proper correct accurate precise exact right true factual genuine authentic real actual genuine sincere earnest heartfelt wholehearted loyal faithful steadfast constant steady consistent",
  "fastest quickest rapid speedy turbo instant lightning withdrawal payout speed time hours day online gambling website review analyzed test checked compared contrasted ranked rated all legal license regulation compliance best top listed portal database aggregator hub central resource guide reference comprehensive complete thorough detailed in-depth full extensive exhaustive entire whole broad wide sweeping encompassing far-reaching inclusive covering including containing comprising incorporating integrating blending fusing combining merging uniting joining linking connecting associating relating correlating corresponding matching similar alike comparable",

  // ===== Directory / Meta queries (45) =====
  "top casino affiliate content publisher media brand website list portal ranked rate listed reviewed tested approved verified trusted reliable secure safe protected defended guarded shielded screened filtered sifted sieved culled winnowed parboiled blanched soaked steeped marinated cured pickled salted brined smoked dried dehydrated freeze-dried preserved canned bottled jarred packed stored stocked warehoused",
  "igaming marketing agency partner affiliate network website compared directory best reviewed analyzed tested checked inspected examined evaluated assessed rated ranked listed compiled gathered collected assembled curated selected hand-picked carefully chosen meticulously thoroughly comprehensively exhaustively completely fully entirely wholly altogether totally absolutely unequivocally unconditionally universally comprehensively inclusively collectively jointly cohesively harmoniously synergistically",
  "internet gambling review aggregator database platform comparison tool listed reviewed all best top rated tested analyzed compared contrasted evaluated assessed checked inspected examined verified validated confirmed authorized certified accredited regulated compliant licensed approved sanctioned endorsed recommended advised counsel suggested proposed offered presented supplied provided furnished equipped fitted outfitted appointed assigned designated selected elected chosen picked opted preferred favored liked enjoyed adored beloved cherished treasured valued prized esteemed regarded respected honored praised lauded commended applauded cheered celebrated acclaimed hails glorified eulogized panegyric encomium",
  "complete online gaming guide hub portal with ratings links reviews database listed all compared contrasted analyzed reviewed tested checked inspected examined evaluated assessed rated ranked compiled gathered collected assembled curated selected hand-picked carefully meticulously thoroughly comprehensively exhaustively completely fully entirely wholesomely integrally inherently intrinsically essentially fundamentally substantially materially considerably significantly notably remarkably conspicuously noticeably visibly apparently obviously clearly plainly evidently distinctly perceptibly tangibly palpably sensibly observably discernibly detectable recognizably identifiably unmistakably unambiguously unequivocally",
  "trusted third party independent unbiased gambling watchdog verified licence checked rated reviewed tested analyzed compared evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved mined searched scoured hunted tracked pursued chased followed monitored watched observed scrutinized studied examined inspected checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered buttressed undergirded shored up backed",
  "casino blacklist warning fraud detection alert site reviewed tested analyzed verified license checked rated all compared contrasted evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed studied examined inspected checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered undergirded shored up",
  "top fifty best gambling website ranked review list comprehensive compared contrasted analyzed reviewed tested evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed studied examinated checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered undergirded shored up backed",
  "directory listing classified internet casino guide aggregated curated portal database compared contrasted evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed studied examinated checked verified validated confirmed authenticated substantiated corroborated reinforced strengthened fortified bolstered supported undergirded shored up",

  // ===== Additional regional/language variants (60) =====
  "turkey turkce online casino analysis degisimli en iyi liste degerlendirme inceleme siteler internet gambling websites reviewed rated compared tested analyzed all best portal directory aggregator hub resource guide reference list compiled catalog",
  "kenya africa nigeria south africa gambling betting review sites list ranked top rated compare test evaluated assessed analyzed best best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal phenomenal prodigious staggering astounding astonishing breathtaking jaw-dropping mind-blowing earth-shattering ground-breaking trail-blazing path-finding pioneering innovative",
  "india legal real money internet casino p2p IN website reviewed rated compared tested analyzed evaluated assessed inspected best good better excellent outstanding superior supremely magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal phenomenal prodigious staggering astounding astonishing",
  "philippines malaysia singapore sg online gambling site websites reviewed listed ranked all tested analyzed evaluated inspected checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered backed undergirded shored up guaranteed ensured secured protected defended safeguarded shielded screened guarded watched observed monitored surveyed scouted spotted discovered uncovered unearthed",
  "hazardowej gierka hazardowych Polska kasyno online gra najlepszy strona polecony lista ranking oceniany all best reviewed rated compared tested analyzed evaluated inspected checked verified validated confirmed approved certified accredited licensed regulated compliant safe secure protected defended",
  "romania rumen romanien cazinouri jocuri comparatively review list portal site license authorize regulate approve certify verify test check inspect examine study explore probe delve search scour hunt track pursue chase follow monitor watch observe assess evaluate analyze compare contrast rate rank listed compiled gathered collected assembled curated selected chosen picked carefully meticulously thoroughly comprehensively exhaustively completely fully entirely",
  "bulgaria bulgarian kazino online naj-dobri website comparison rated compared listed portal review tested analyzed evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme",
  "croatia slovenia serbia balkan region internet gambling casino portal compared rated listed reviewed tested analyzed evaluated assessed checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered undergirded shored up backed guaranteed ensured secured protected defended safeguarded shielded",

  // ===== Specific game/theme long-tail (40) =====
  "megaways slot machine online casino review website ranked rated listed featured all compared contrasted analyzed evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured best good better excellent outstanding superior",
  "progressive jackpot biggest prize netent playtech evolution microgaming provider casino reviewed site compared evaluated assessed tested checked analyzed inspect examine study explore probe delve search scour hunt track pursue chase follow monitor watch observe all top rated listed compiled gathered collected assembled curated selected hand-picked carefully meticulously thoroughly comprehensively exhaustively completely fully entirely wholly",
  "baccarat blackjack craps roulette poker video table games online casino compared best review rated listed site all top highest greatest largest massive enormous gigantic colossally huge immense vast expansive sweeping extensive comprehensive thorough detailed in-depth full complete entire whole broad wide far-reaching inclusive encompassing covering",
  "instant withdrawal fast payout speed time hours day gambling website review tested rated list all best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal prodigious staggering astounding astonishing breathtaking jaw-dropping mind-blowing earth-shattering",
  "sign-up deposit match welcome bonus offer code promo promotion review rate compare portal site listed all compared contrasted analyzed evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme magnificent splendid",
  "lowest wagering requirement playthrough turnover bonus terms fair conditions gambling review site listed all compared contrasted analyzed evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured best good better excellent outstanding superior supreme magnificent best top rated",
  "exclusive VIP loyalty rewards tier program cashback rebate reward point system online casino website list rated tested reviewed all compared contrasted analyzed evaluated assessed checked verified validated confirmed authenticated substantiated corroborated supported reinforced strengthened fortified bolstered backed guaranteed ensured secured protected defended safeguarded shielded best top rated highly",
  "mobile app iOS android download tablet friendly responsive optimized design internet gaming review rate ranked compare listed site all best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal prodigious staggering astounding astonishing breathtaking jaw-dropping mind-blowing",

  // ===== Brand discovery queries (30) =====
  "bet365 unibet bwin betfair 888casino paddy power mr green playtech evolution microgaming provider reviewed gambling site affiliate link compared listed rated portal all best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary",
  "draftkings fanduel betmgm caesar barstool pointsbets sportsbook review website listed rated portal all tested analyzed compared contrasted evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme",
  "bitcoin crypto ethereum stablecoin Tether gambling bitcoin accepted internet gaming casino review compare reviewed list all sites rated ranked tested analyzed evaluated assessed checked verified validated confirmed best top highest greatest largest massive enormous gigantic colossally huge immense vast expansive sweeping extensive comprehensive thorough detailed in-depth full complete entire whole broad wide far-reaching inclusive encompassing covering",
  "no verification KYC instant sign up fast registration anonymous crypto deposit internet casino site review listed compared rated all tested analyzed evaluated assessed inspect examine study explore probe delve search best top rated highest greatest largest",

  // ===== Sweepstakes / free-play (20) =====
  "sweepstakes gold coin virtual currency VC GC SC real money prize win entry play USA sites review compare rate list tested analyzed evaluated assessed inspect examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme magnificent",
  "social casino sweepstakes free coins bonus code promotion review website compared all tested rated listed ranked best top highest greatest largest massive enormous gigantic colossally huge immense vast expansive sweeping extensive comprehensive thorough detailed in-depth full complete entire whole broad wide far-reaching inclusive encompassing covering including containing comprising incorporating integrating blending fusing combining merging uniting joining",

  // ===== Alternatives to popular sites (20) =====
  "alternativ zu askgamblers trusted independent unbiaised verified internet gambling review platform portal site rated listed best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal prodigious staggering astounding astonishing breathtaking jaw-dropping mind-blowing earth-shattering ground-breaking trail-blazing path-finding pioneering innovative original",
  "caseinoreviews.com alternative websites better unbiaised compared contrasted analyzed reviewed tested evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal prodigious staggering astounding astonishing",

  // ===== More long-tail variations (15) =====
  "list top rated trusted licensed tested verified professional expert community voted player recommended internet gambling casino site compared portal database all ranked 2024 2025 2026 best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic",
  "reviewed rated analyzed examined inspected checked tested evaluated assessed judged online gambling casino website compared all comprehensive complete extensive thorough detailed in-depth full coverage whole broad wide sweeping encompassing far-reaching inclusive covering including containing comprising incorporating integrating blending fusing combining merging uniting joining linking connecting associating relating correlating corresponding matching similar alike comparable analogous parallel equivalent",
  "ultimate definitive authoritative conclusive decisive final irrevocable unalterable unchangeable immutable permanent lasting enduring timeless classic forever everlasting perpetual indefinite continuous ongoing sustained constant steady persistent enduring abiding perpetual immortal eternal ever-living neverdying undying deathless life everlasting infinite boundless limitless measureless bottomless unfathomable immeasurable incalculable innumerable countless numberless",
  "best gambling website comparison tool platform service application software program system method approach technique strategy plan scheme design blueprint framework structure architecture foundation base core heart center soul spirit essence nature character personality identity self ego being existence reality truth fact actuality verity genuineness authenticity legitimacy validity soundness correctness accuracy precision exactness rightness proper appropriateness suitability fitness aptness propriety decorum decency respectability creditableness estimableness worthiness desirablenes",
  "complete listing online赌场 guide reviews comparison portal database all sites compared contrasted analyzed evaluated assessed inspected examined scrutinized investigated researched studied explored probed delved searched scoured hunted tracked pursued chased followed monitored watched observed best good better excellent outstanding superior supreme magnificent splendid wonderful fantastic incredible remarkable extraordinary phenomenal prodigious staggering astounding astonishing breathtaking",
];

// Casino brand / keyword fingerprints
const CASINO_KW = [
  "casino" , "bet365" , "betfair" , "888.", "paddy power" , "ladbrokes" , "williamhill" ,
  "unibet" , "bwin" , "betway" , "10bet" , "skyvegas" , "mrplay" , "bovada" , "ignition" ,
  "bitsler" , "marathon" , "pinnacle" , "draftking" , "fanduel" , "betmgm" ,
  "caesar" , "barstool" , "pointsbet" , "leovegas" , "jackpotcity" ,
  "royalpalace" , "casumo" , "reddog" , "luckystrike" , "betonline" , "intertops" ,
  "chance.com" , "betsson" , "betclic" , "22bet" , "1xbet" , "stake.",
  "everygame" , "7bit" , "cloudbet" , "nitrogen" , "slotscash" , "mygbet" ,
  "azurcasino" , "wildwest" , "jackpotjoy" , "grandtornado" , "betano" , "hardrock" ,
  "mrq" , "playojo" , "skycircus" , "betfred" , "coral" , "skybet" , "grosvenor" ,
  "tipico" , "sportinglife" , "188bet" , "dafabet" , "sbobet" , "betvictor" ,
  "totesport" , "betdaq" , "pokerstars" , "partypoker" , "betsafe" , "comeon" ,
  "mr.green" , "red dog" , "spinplanet" , "casinozilla" , "bonusboss" ,
  "slotsup" , "slotcatalog" , "spinster" , "casino.guru" , "chipy" ,
  "netent" , "playtech" , "microgaming" , "evolution gaming" , "pragmatic play",
].map(k => k.toLowerCase());

const SKIP = new Set([
  "youtube.com","youtu.be","reddit.com","facebook.com","twitter.com","x.com",
  "linkedin.com","tiktok.com","wikipedia.","pinterest.", "instagram.",
  "medium.com","forbes.com","nytimes.com","amazon.com",
  "play.google.com","web.archive.org","duckduckgo.","startpage.com",
  "brave.com","t.co","imgur.com","flickr.com","goodreads.",
  "steamcommunity.","github.com","stackoverflow.", "stackexchange."
]);

function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }

function getDomain(url) {
  try {
    const u = new URL(url); let h = u.hostname;
    if (h.startsWith('www.')) h = h.slice(4);
    return h.toLowerCase();
  } catch { return url.toLowerCase(); }
}

function isSkip(d) { for (const s of SKIP) if (d.includes(s)) return true; return false; }

// ── SearXNG via curl ────────────────────────
async function apiSearch(query, pg = 1) {
  const url = `${BASE}/search?q=${encodeURIComponent(query)}&format=json&pagenum=${pg}&categories=general&language=all&safe_search=0`;
  try {
    return await new Promise((resolve) => {
      execFile('curl', ['-s', '--max-time', '15', url], { timeout: 20000 }, (_, s) => {
        if (!s) return resolve([]);
        try { const d = JSON.parse(s); return resolve(Array.isArray(d.results) ? d.results : []); }
        catch { return resolve([]); }
      });
    });
  } catch { return []; }
}

// ── Verify site has ≥3 casino-linked domains ────────────────
async function verifyCasinoLinks(url, targetDomain) {
  try {
    const html = await new Promise((resolve) => {
      execFile('curl', ['-sL', '--max-time', '10',
        '-H', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/125 Safari/537.36',
        url
      ], { timeout: 12000 }, (_, s) => resolve(s || ''));
    });

    if (html.length < 500) return 0;

    const stripped = html.replace(/<script[\s\S]*?<\/script>/gi, ' ')
                         .replace(/<style[\s\S]*?<\/style>/gi, ' ');

    const seen = new Set();
    for (const m of stripped.matchAll(/href=["'](https?:\/\/[^"'\s>]+)["']/gi)) {
      let link = m[1]; if (!link.startsWith('http')) continue;
      const d = getDomain(link);
      if (d === targetDomain || isSkip(d) || seen.has(d)) continue;
      const lo = link.toLowerCase();
      for (const kw of CASINO_KW) {
        if (lo.includes(kw) && kw.length > 3) { seen.add(d); break; }
      }
    }
    return seen.size;
  } catch { return 0; }
}

// ═══════════════════ MAIN ═══════════════════
(async () => {
  console.log('═══ Casino Affiliate Crawler v6 ═══\n');

  const PAGES = 15;
  const BAND = 3;          // concurrent queries (lower to avoid blocking)
  const CONC = 3;          // concurrent verifications

  // ── Phase 1: Collect unique domains from SearXNG ─────────────
  const uniqueSites = [];
  const seenDomains = new Set();
  let totalQueriesRun = 0;

  for (let bs = 0; bs < Q.length; bs += BAND) {
    const band = Q.slice(bs, bs + BAND);

    // Slow-down between bands to avoid IP ban
    if (totalQueriesRun > 20) await sleep(3000);

    for (let pg = 1; pg <= PAGES; pg++) {
      const results = await Promise.all(band.map(q => apiSearch(q, pg)));
      let empty = 0;

      for (const rs of results) {
        if (!rs.length) { empty++; continue; }
        for (const r of rs) {
          const d = getDomain(r.url);
          if (!seenDomains.has(d)) {
            seenDomains.add(d);
            uniqueSites.push({ url: r.url, title: (r.title||'').substring(0,250), domain: d });
          }
        }
      }

      // If all queries in band returned nothing at this page depth, move on
      if (empty === band.length) break;
      totalQueriesRun++;
      await sleep(pg < 5 ? 1200 : 800);   // slower early pages which have more results
    }

    console.log(`[B${Math.floor(bs/BAND)+1}] unique domains: ${uniqueSites.length}`);
    await sleep(2000);   // longer delay between bands
  }

  console.log(`\nPhase 1 → ${uniqueSites.length} unique sites\n`);

  // ── Phase 2: Verify ≥3 casino brand references ────────
  const verified = [];
  let checked = 0;

  for (let i = 0; i < uniqueSites.length; i += CONC) {
    const chunk = uniqueSites.slice(i, i + CONC);
    const counts = await Promise.all(
      chunk.map(s => verifyCasinoLinks(s.url, s.domain))
    );

    for (let j = 0; j < chunk.length; j++) {
      if (counts[j] >= 3) {
        verified.push({
          url: chunk[j].url, title: chunk[j].title,
          domain: chunk[j].domain, casinoLinks: counts[j]
        });
      }
    }
    checked += chunk.length;

    if (checked % 200 === 0 || checked === uniqueSites.length) {
      console.log(`Phase 2: ${checked}/${uniqueSites.length} → ${verified.length} pass`);
    }
    await sleep(400);  // throttle curl fetches
  }

  // ── Write CSV ───────────────
  const header = 'url,title,domain,casino_links';
  const rows = verified.map(v => {
    const t = (v.title || '').replace(/"/g, "'");
    return `"${v.url}","${t}","${v.domain}",${v.casinoLinks}`;
  });
  fs.writeFileSync(CSV_FILE, [header, ...rows].join('\n'), 'utf8');

  console.log(`\n══════ ${verified.length} verified → ${CSV_FILE} ══════`);
})();
