/**
 * Test script for intelligent search selector discovery
 * This script tests the enhanced search functionality with intelligent selector fallbacks
 */

const { chromium } = require('playwright');

async function testIntelligentSearchSelectors() {
  console.log('🧪 Testing Intelligent Search Selector Discovery...');
  
  const browser = await chromium.launch({ 
    headless: false,
    args: ['--disable-web-security', '--disable-features=VizDisplayCompositor']
  });
  
  const context = await browser.newContext();
  const page = await context.newPage();
  
  try {
    // Navigate to Google
    console.log('📍 Navigating to Google...');
    await page.goto('https://www.google.com');
    await page.waitForLoadState('networkidle');
    
    // Perform a search
    console.log('🔍 Performing search...');
    const searchBox = await page.locator('textarea[name="q"], input[name="q"]').first();
    await searchBox.fill('intelligent selector discovery test');
    await searchBox.press('Enter');
    
    // Wait for results
    await page.waitForLoadState('networkidle');
    await page.waitForTimeout(3000);
    
    // Inject our enhanced search helper
    console.log('💉 Injecting enhanced search helper...');
    await page.addScriptTag({
      path: './app/chrome-extension/inject-scripts/enhanced-search-helper.js'
    });
    
    // Test the intelligent selector discovery
    console.log('🧠 Testing intelligent selector discovery...');
    const results = await page.evaluate(async () => {
      // Call the extractSearchResults function from our injected script
      if (typeof extractSearchResults === 'function') {
        return await extractSearchResults(5);
      } else {
        throw new Error('extractSearchResults function not found');
      }
    });
    
    console.log('📊 Results:', JSON.stringify(results, null, 2));
    
    // Validate results
    if (results.success && results.results.length > 0) {
      console.log('✅ SUCCESS: Intelligent selector discovery found', results.results.length, 'results');
      console.log('🎯 Selector used:', results.selectorUsed);
      console.log('🔧 Method:', results.method);
      
      // Display first few results
      results.results.slice(0, 3).forEach((result, index) => {
        console.log(`\n📄 Result ${index + 1}:`);
        console.log(`   Title: ${result.title}`);
        console.log(`   URL: ${result.url}`);
        console.log(`   Snippet: ${result.snippet.substring(0, 100)}...`);
      });
      
    } else {
      console.log('❌ FAILED: No results found');
      console.log('Error:', results.error);
    }
    
    // Test with a page that might have different structure
    console.log('\n🔄 Testing with DuckDuckGo for different page structure...');
    await page.goto('https://duckduckgo.com');
    await page.waitForLoadState('networkidle');
    
    const ddgSearchBox = await page.locator('input[name="q"]').first();
    await ddgSearchBox.fill('intelligent selector test');
    await ddgSearchBox.press('Enter');
    
    await page.waitForLoadState('networkidle');
    await page.waitForTimeout(3000);
    
    const ddgResults = await page.evaluate(async () => {
      if (typeof extractSearchResults === 'function') {
        return await extractSearchResults(3);
      } else {
        throw new Error('extractSearchResults function not found');
      }
    });
    
    console.log('📊 DuckDuckGo Results:', JSON.stringify(ddgResults, null, 2));
    
    if (ddgResults.success && ddgResults.results.length > 0) {
      console.log('✅ SUCCESS: Intelligent discovery works on DuckDuckGo too!');
      console.log('🎯 Selector used:', ddgResults.selectorUsed);
    } else {
      console.log('⚠️ DuckDuckGo test failed, but this might be expected due to different structure');
    }
    
  } catch (error) {
    console.error('❌ Test failed:', error);
  } finally {
    await browser.close();
  }
}

async function testSelectorValidation() {
  console.log('\n🧪 Testing Selector Validation Functions...');
  
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext();
  const page = await context.newPage();
  
  try {
    // Create a test page with various elements
    await page.setContent(`
      <html>
        <body>
          <div class="search-result">
            <h3>Test Result 1</h3>
            <a href="https://example.com">Example Link</a>
            <p>This is a test snippet with substantial content to validate.</p>
          </div>
          <div class="not-a-result">
            <span>Short</span>
          </div>
          <div class="another-result">
            <h2>Test Result 2</h2>
            <a href="https://test.com">Test Link</a>
            <div>Another test snippet with enough content to pass validation checks.</div>
          </div>
        </body>
      </html>
    `);
    
    // Inject our helper functions
    await page.addScriptTag({
      path: './app/chrome-extension/inject-scripts/enhanced-search-helper.js'
    });
    
    // Test validation function
    const validationResults = await page.evaluate(() => {
      const elements = document.querySelectorAll('div');
      const results = [];
      
      elements.forEach((el, index) => {
        const isValid = validateSearchResultElement(el);
        const extracted = extractResultFromElement(el, index + 1);
        
        results.push({
          className: el.className,
          isValid,
          extracted
        });
      });
      
      return results;
    });
    
    console.log('🔍 Validation Results:');
    validationResults.forEach((result, index) => {
      console.log(`Element ${index + 1} (${result.className}):`);
      console.log(`  Valid: ${result.isValid}`);
      console.log(`  Extracted: ${result.extracted ? 'Yes' : 'No'}`);
      if (result.extracted) {
        console.log(`  Title: ${result.extracted.title}`);
        console.log(`  URL: ${result.extracted.url}`);
      }
    });
    
  } catch (error) {
    console.error('❌ Validation test failed:', error);
  } finally {
    await browser.close();
  }
}

// Run tests
async function runAllTests() {
  console.log('🚀 Starting Intelligent Search Selector Tests...\n');
  
  await testIntelligentSearchSelectors();
  await testSelectorValidation();
  
  console.log('\n✨ All tests completed!');
}

// Check if this script is being run directly
if (require.main === module) {
  runAllTests().catch(console.error);
}

module.exports = {
  testIntelligentSearchSelectors,
  testSelectorValidation,
  runAllTests
};
