// apps/api/src/scrapers/google.scraper.js

const axios = require('axios');
const cheerio = require('cheerio');
const Helpers = require('../../../../packages/shared/helpers');
const { SCRAPE_CONFIG } = require('../../../../packages/shared/constants');

class GoogleScraper {
    async search(query, options = {}) {
        const results = [];
        
        try {
            const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(query)}&num=${options.num || 20}`;
            
            await Helpers.delay(Math.random() * 1000 + 1000);
            
            const response = await axios.get(searchUrl, {
                headers: {
                    'User-Agent': SCRAPE_CONFIG.userAgent,
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language': 'en-US,en;q=0.9'
                },
                timeout: SCRAPE_CONFIG.timeout
            });
            
            const $ = cheerio.load(response.data);
            
            $('div.g').each((i, el) => {
                const title = $(el).find('h3').text();
                const snippet = $(el).find('.VwiC3b').text() || $(el).find('.st').text();
                let link = $(el).find('a').attr('href');
                
                if (link && link.startsWith('/url?')) {
                    const urlParams = new URLSearchParams(link);
                    link = urlParams.get('q');
                }
                
                if (title && snippet && link && !link.includes('google.com')) {
                    results.push({
                        title: title.trim(),
                        snippet: snippet.trim(),
                        url: link,
                        source: 'google',
                        timestamp: new Date()
                    });
                }
            });
            
            return results;
        } catch (error) {
            console.error('Google scrape error:', error.message);
            return [];
        }
    }
}

module.exports = new GoogleScraper();