// apps/api/src/utils/extractWhatsapp.js

const PhoneExtractor = require('./extractPhone');

class WhatsAppExtractor {
    static extract(text) {
        const lowerText = text.toLowerCase();
        
        // Check for explicit WhatsApp mentions
        const whatsappPatterns = [
            /whatsapp[:\s]*(\+?[0-9\s-]{10,15})/gi,
            /wa\.me\/([0-9]+)/gi,
            /chat\.whatsapp\.com/i,
            /whatsapp only/gi,
            /text me on whatsapp/gi
        ];
        
        for (const pattern of whatsappPatterns) {
            const match = text.match(pattern);
            if (match) {
                if (match[1]) {
                    return PhoneExtractor.clean(match[1]);
                }
                return 'whatsapp_available';
            }
        }
        
        // If no explicit mention, check if phone exists
        const phone = PhoneExtractor.extract(text);
        if (phone) {
            return phone;
        }
        
        return null;
    }
    
    static hasWhatsApp(text) {
        const lowerText = text.toLowerCase();
        const indicators = ['whatsapp', 'wa.me', 'chat.whatsapp', 'text me on'];
        return indicators.some(indicator => lowerText.includes(indicator));
    }
    
    static extractAll(text) {
        const whatsappNumbers = new Set();
        
        // Extract phone numbers that might be WhatsApp
        const phones = PhoneExtractor.extractAll(text);
        phones.forEach(p => whatsappNumbers.add(p));
        
        return Array.from(whatsappNumbers);
    }
}

module.exports = WhatsAppExtractor;