import { AccessibilityScanner } from './core/scanner';
import type { AccessibilityScanOptions } from '@wcag-ada/shared';
import { createLogger } from '@lib/service/core-logger';

const logger = createLogger('scanner-example');

async function runExample() {
  const scanner = new AccessibilityScanner();
  
  try {
    // Initialize the browser
    await scanner.initialize();
    
    // Define scan options
    const scanOptions: AccessibilityScanOptions = {
      url: 'https://example.com',
      wcagLevel: {
        level: 'AA',
        version: '2.1'
      },
      includeScreenshots: true,
      viewport: {
        width: 1920,
        height: 1080
      },
      excludeSelectors: [
        '.cookie-banner',
        '[aria-hidden="true"]'
      ]
    };
    
    logger.info('Starting accessibility scan...');
    const result = await scanner.scan(scanOptions);
    
    // Display summary
    logger.info('\n=== SCAN SUMMARY ===');
    logger.info(`URL: ${result.url}`);
    logger.info(`Compliance Score: ${result.summary.score}%`);
    logger.info(`WCAG ${result.wcagCompliance.level.version} Level ${result.wcagCompliance.level.level}: ${result.wcagCompliance.isCompliant ? 'PASSED' : 'FAILED'}`);
    logger.info(`\nViolations: ${result.summary.violationCount}`);
    logger.info(`- Critical: ${result.summary.criticalIssues}`);
    logger.info(`- Serious: ${result.summary.seriousIssues}`);
    logger.info(`- Moderate: ${result.summary.moderateIssues}`);
    logger.info(`- Minor: ${result.summary.minorIssues}`);
    
    // Display top violations
    if (result.violations.length > 0) {
      logger.info('\n=== TOP VIOLATIONS ===');
      result.violations.slice(0, 5).forEach((violation, index) => {
        logger.info(`\n${index + 1}. ${violation.help}`);
        logger.info(`   Impact: ${violation.impact}`);
        logger.info(`   Affected elements: ${violation.nodes.length}`);
        logger.info(`   Fix: ${violation.fixSuggestion}`);
        logger.info(`   More info: ${violation.helpUrl}`);
      });
    }
    
    // Display page metadata
    logger.info('\n=== PAGE METADATA ===');
    logger.info(`Title: ${result.pageMetadata.title}`);
    logger.info(`Language: ${result.pageMetadata.language || 'Not specified'}`);
    logger.info(`Images: ${result.pageMetadata.imagesCount}`);
    logger.info(`Forms: ${result.pageMetadata.formsCount}`);
    logger.info(`Links: ${result.pageMetadata.linksCount}`);
    logger.info(`Has landmarks: ${result.pageMetadata.hasLandmarks ? 'Yes' : 'No'}`);
    logger.info(`Has headings: ${result.pageMetadata.hasHeadings ? 'Yes' : 'No'}`);
    
  } catch (error) {
    logger.error('Scan failed:', error);
  } finally {
    // Always close the browser
    await scanner.close();
  }
}

// Run the example
if (require.main === module) {
  runExample().catch((error) => logger.error('Example failed:', error));
}