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

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

class ClassifiedsScraper {
    async search(query, location = null) {
        const results = [];
        
        // Olx-style search (adapt for your region)
        const sites = [
            { name: 'jumia', url: 'https://www.jumia.co.ke' },
            { name: 'pigiame', url: 'https://www.pigiame.co.ke' }
        ];
        
        for (const site of sites) {
            try {
                const searchUrl = `${site.url}/search?q=${encodeURIComponent(query)}`;
                
                await Helpers.delay(2000);
                
                const response = await axios.get(searchUrl, {
                    headers: { 'User-Agent': SCRAPE_CONFIG.userAgent },
                    timeout: SCRAPE_CONFIG.timeout
                });
                
                const $ = cheerio.load(response.data);
                
                $('.listing, .item, .product').each((i, el) => {
                    const title = $(el).find('.title, .name').text();
                    const description = $(el).find('.description, .details').text();
                    const price = $(el).find('.price').text();
                    const link = $(el).find('a').attr('href');
                    
                    if (title && this.isServiceRequest(title + ' ' + description)) {
                        results.push({
                            title: title.trim(),
                            snippet: description.trim(),
                            url: link ? (link.startsWith('http') ? link : site.url + link) : null,
                            price: price,
                            source: site.name,
                            timestamp: new Date()
                        });
                    }
                });
            } catch (error) {
                console.error(`${site.name} scrape error:`, error.message);
            }
        }
        
        return results;
    }
    
    isServiceRequest(text) {
        const serviceKeywords = ['service', 'need', 'looking for', 'seeking', 'hire', 'want'];
        const lowerText = text.toLowerCase();
        return serviceKeywords.some(keyword => lowerText.includes(keyword));
    }
}

module.exports = new ClassifiedsScraper();