const puppeteer = require('puppeteer-extra');

async function scrapePromofarma(query, browser) {
  const page = await browser.newPage();
  const url = `https://www.promofarma.com/es/search?q=${encodeURIComponent(query)}`;
  
  try {
    await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 30000 });
    
    // Fallback/heuristic extraction
    const products = await page.evaluate(() => {
      const results = [];
      const cards = document.querySelectorAll('.product-card, article, [class*="product"]');
      
      cards.forEach(card => {
        const titleEl = card.querySelector('h3, h2, [class*="title"], [class*="name"]');
        const priceEl = card.querySelector('[class*="price"], .current-price');
        const linkEl = card.querySelector('a');
        
        if (titleEl && priceEl && titleEl.innerText.trim().length > 0) {
          results.push({
            name: titleEl.innerText.trim(),
            price: priceEl.innerText.trim(),
            link: linkEl ? linkEl.href : null,
            source: 'Promofarma'
          });
        }
      });
      
      // Deduplicate keeping the first occurrence
      const unique = [];
      const names = new Set();
      for (const p of results) {
        if (!names.has(p.name)) {
          names.add(p.name);
          unique.push(p);
        }
      }
      return unique.slice(0, 5); // Return top 5
    });

    return products;
  } catch (error) {
    console.error(`[Promofarma] Error scraping ${query}:`, error.message);
    return [];
  } finally {
    await page.close();
  }
}

module.exports = scrapePromofarma;
