async function detectSubmissionResult(page) {
    const result = await page.evaluate(() => {
        const results = {
            success: false,
            failure: false,
            failureReason: null,
            currentUrl: window.location.href,
            signals: []
        };

        const errorSelectors = [
            '.error', '.Error', '.ERROR',
            '[class*="error" i]',
            '.alert-danger', '.alert-error',
            '[class*="failed" i]',
            '[class*="invalid" i]',
            '[class*="wrong" i]',
            '#error', '#Error',
            '[role="alert"]',
            '.message.error',
            '.error-message',
            '.form-error',
            '[data-form-error]'
        ];

        for (const selector of errorSelectors) {
            const el = document.querySelector(selector);
            if (el) {
                const text = el.textContent?.toLowerCase() || '';
                const style = window.getComputedStyle(el);
                if (style.display !== 'none' && 
                    style.visibility !== 'hidden' && 
                    style.opacity !== '0' &&
                    text.length > 0 &&
                    text.length < 500) {
                    results.failure = true;
                    results.failureReason = `Error message detected: ${text.substring(0, 100)}`;
                    results.signals.push(`Error: ${text.substring(0, 100)}`);
                    break;
                }
            }
        }

        const pageText = document.body.innerText.toLowerCase();
        
        const successKeywords = [
            'thank you', 'thanks for', 'successfully', 'subscribed', 'subscription confirmed',
            'you are now', 'welcome', 'email confirmed', 'verification sent',
            'check your email', 'confirm your email', 'almost done',
            'signed up', 'signed up successfully', 'subscription successful'
        ];
        
        for (const keyword of successKeywords) {
            if (pageText.includes(keyword)) {
                results.success = true;
                results.signals.push(`Success keyword: "${keyword}"`);
                break;
            }
        }

        const failureKeywords = [
            'incorrect', 'invalid captcha', 'wrong captcha', 'incorrect captcha', 
            'captcha incorrect', 'captcha invalid', 'captcha error',
            'please complete the captcha', 'complete the captcha',
            'try again', 'submission failed'
        ];
        
        if (!results.failure) {
            for (const keyword of failureKeywords) {
                if (pageText.includes(keyword)) {
                    results.failure = true;
                    results.failureReason = `Failure keyword detected: "${keyword}"`;
                    results.signals.push(`Failure: ${keyword}`);
                    break;
                }
            }
        }

        const formStillVisible = document.querySelector('form') !== null;
        const emailInputStillVisible = document.querySelector('input[type="email"]') !== null ||
                                       document.querySelector('input[name*="email" i]') !== null;
        
        if (formStillVisible && emailInputStillVisible && !results.success) {
            results.failure = true;
            results.failureReason = results.failureReason || 'Form still visible after submission (likely failed)';
            results.signals.push('Form still visible - submission may have failed');
        }

        return results;
    });

    return result;
}

module.exports = {
    detectSubmissionResult
};
