async function detectSubmitButton(page) {
    console.log('Auto-detecting submit button...');
    
    const submitSelector = await page.evaluate(() => {
        const buttons = Array.from(document.querySelectorAll('button[type="submit"], input[type="submit"], button, input[type="button"]'));
        let bestMatch = null;
        let bestScore = 0;
        
        buttons.forEach(btn => {
            const text = (btn.textContent || btn.value || '').toLowerCase().trim();
            const type = btn.type?.toLowerCase() || '';
            const className = (btn.className || '').toLowerCase();
            let score = 0;
            
            if (type === 'submit') score += 20;
            
            if (text.includes('subscribe')) score += 15;
            else if (text.includes('sign up')) score += 12;
            else if (text.includes('submit')) score += 10;
            else if (text.includes('join')) score += 8;
            else if (text.includes('register')) score += 8;
            else if (text.includes('send')) score += 5;
            
            if (className.includes('submit')) score += 8;
            if (className.includes('subscribe')) score += 8;
            
            if (className.includes('close') || text.includes('close')) score -= 20;
            if (className.includes('cancel') || text.includes('cancel')) score -= 20;
            if (className.includes('back') || text.includes('back')) score -= 15;
            if (className.includes('drawer')) score -= 15;
            
            const rect = btn.getBoundingClientRect();
            if (rect.width > 0 && rect.height > 0 && rect.top > 0) {
                score += 3;
            }
            
            if (score > bestScore) {
                bestScore = score;
                bestMatch = btn;
            }
        });
        
        if (bestMatch && bestScore >= 10) {
            if (bestMatch.id) return `#${bestMatch.id}`;
            if (bestMatch.name) return `[name="${bestMatch.name}"]`;
            
            const className = bestMatch.className;
            if (className) {
                const classes = className.split(' ').filter(c => c.length > 0);
                for (const cls of classes) {
                    const clsLower = cls.toLowerCase();
                    if (clsLower.includes('submit') || clsLower.includes('subscribe')) {
                        return `.${cls}`;
                    }
                }
                for (const cls of classes) {
                    if (!cls.toLowerCase().includes('close') && !cls.toLowerCase().includes('cancel')) {
                        return `.${cls}`;
                    }
                }
            }
            
            const allBtns = Array.from(document.querySelectorAll('button'));
            const index = allBtns.indexOf(bestMatch) + 1;
            return `button:nth-of-type(${index})`;
        }
        
        return null;
    });
    
    if (submitSelector) {
        console.log(`   Submit button found: ${submitSelector}`);
    } else {
        console.log('   No submit button found');
    }
    
    return submitSelector;
}

module.exports = {
    detectSubmitButton
};
