const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const fs = require('fs');

const scrapePromofarma = require('./src/promofarma');
const scrapeAmazon = require('./src/amazon');
const scrapeGoogleShopping = require('./src/googleshopping');
const scrapeOpenFoodFacts = require('./src/openfoodfacts');

puppeteer.use(StealthPlugin());

// Default terms related to parapharmacy
const QUERIES = [
  'crema hidratante cara',
  'ibuprofeno' // fallback basic test query
];

async function run() {
  console.log('🚀 Starting Parapharmacy Scraper...');
  // Launching browser
  const browser = await puppeteer.launch({
    headless: 'new',
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });

  const allResults = {};

  for (const query of QUERIES) {
    console.log(`\n🔍 Searching for: "${query}"`);
    
    // We run sequentially to be gentle on CPU and anti-bot systems
    const promofarmaResults = await scrapePromofarma(query, browser);
    console.log(`   ✅ Promofarma: found ${promofarmaResults.length} items`);

    const amazonResults = await scrapeAmazon(query, browser);
    console.log(`   ✅ Amazon: found ${amazonResults.length} items`);

    const googleShoppingResults = await scrapeGoogleShopping(query, browser);
    console.log(`   ✅ Google Shopping: found ${googleShoppingResults.length} items`);

    const ofFactsResults = await scrapeOpenFoodFacts(query, browser);
    console.log(`   ✅ OpenFoodFacts: found ${ofFactsResults.length} items`);

    allResults[query] = {
      promofarma: promofarmaResults,
      amazon: amazonResults,
      googleshopping: googleShoppingResults,
      openfoodfacts: ofFactsResults
    };
  }

  await browser.close();

  fs.writeFileSync('results.json', JSON.stringify(allResults, null, 2));
  console.log('\n💾 Scraping finished. Results saved to results.json');
}

run().catch(err => {
    console.error("Critical Error during scraping:", err);
    process.exit(1);
});
