#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const { Worker, isMainThread } = require('worker_threads');
const { processDirectory } = require('./optimized-processor.js');

/**
 * 测试并行处理功能
 */
async function testParallelProcessing() {
    console.log('🧪 Testing Parallel Processing');
    console.log('==============================');
    console.log('');

    // 创建测试目录
    const testDir = path.join(__dirname, 'parallel-test');
    const testOptimizedDir = testDir + '-optimized';
    
    // 清理旧的测试目录
    if (fs.existsSync(testDir)) {
        fs.rmSync(testDir, { recursive: true, force: true });
    }
    if (fs.existsSync(testOptimizedDir)) {
        fs.rmSync(testOptimizedDir, { recursive: true, force: true });
    }
    
    fs.mkdirSync(testDir, { recursive: true });
    
    // 创建多个测试文件
    console.log('📁 Creating test files...');
    const testFiles = [];
    
    for (let i = 0; i < 10; i++) {
        const fileName = `test-file-${i}.js`;
        const filePath = path.join(testDir, fileName);
        
        // 生成包含console语句的测试代码
        const testCode = `
// Test file ${i}
function test${i}(){
    console.log("Test ${i} started");
    const data = {
        id: ${i},
        name: "test${i}",
        values: [1, 2, 3, 4, 5]
    };
    
    console.info("Processing data:", data);
    console.warn("This is a warning from file ${i}");
    console.error("Error simulation in file ${i}");
    
    data.processed = true;
    console.debug("Processing complete for file ${i}");
    
    return data;
}

class TestClass${i} {
    constructor() {
        console.log("TestClass${i} constructor");
        this.value = ${i} * 10;
        console.debug("Value set to:", this.value);
    }
    
    getValue() {
        console.info("Getting value from TestClass${i}");
        return this.value;
    }
}

console.log("Module ${i} loaded");
module.exports = { test${i}, TestClass${i} };
`;
        
        fs.writeFileSync(filePath, testCode);
        testFiles.push(fileName);
    }
    
    console.log(`✅ Created ${testFiles.length} test files`);
    console.log('');

    // 测试并行处理
    console.log('🚀 Testing Parallel Processing...');
    const parallelStart = Date.now();
    
    try {
        const parallelResult = await processDirectory(testDir, {
            useOllama: false,  // 使用本地方法避免Ollama依赖
            parallel: true,     // 启用并行处理
            dryRun: false       // 实际处理
        });
        
        const parallelTime = Date.now() - parallelStart;
        
        console.log('');
        console.log('📊 Parallel Processing Results:');
        console.log(`   Processing time: ${parallelTime}ms`);
        console.log(`   Files processed: ${parallelResult.successful}`);
        console.log(`   Average time per file: ${(parallelTime / parallelResult.successful).toFixed(1)}ms`);
        
        // 测试单线程处理作为对比
        console.log('');
        console.log('🐌 Testing Single-Thread Processing...');
        const singleStart = Date.now();
        
        // 清理之前的输出目录
        if (fs.existsSync(testOptimizedDir)) {
            fs.rmSync(testOptimizedDir, { recursive: true, force: true });
        }
        
        const singleResult = await processDirectory(testDir, {
            useOllama: false,  // 使用本地方法
            parallel: false,    // 关闭并行处理
            dryRun: false
        });
        
        const singleTime = Date.now() - singleStart;
        
        console.log('');
        console.log('📊 Single-Thread Processing Results:');
        console.log(`   Processing time: ${singleTime}ms`);
        console.log(`   Files processed: ${singleResult.successful}`);
        console.log(`   Average time per file: ${(singleTime / singleResult.successful).toFixed(1)}ms`);
        
        // 性能对比
        console.log('');
        console.log('🏁 Performance Comparison:');
        console.log('================================');
        
        const speedup = singleTime / parallelTime;
        const timeSaved = singleTime - parallelTime;
        const timeSavedPercent = ((timeSaved / singleTime) * 100).toFixed(1);
        
        console.log(`Parallel time:   ${parallelTime}ms`);
        console.log(`Single time:     ${singleTime}ms`);
        console.log(`Speed improvement: ${speedup.toFixed(2)}x faster`);
        console.log(`Time saved:       ${timeSaved}ms (${timeSavedPercent}%)`);
        
        if (speedup > 1.5) {
            console.log('🎉 Parallel processing is significantly faster!');
        } else if (speedup > 1.1) {
            console.log('✅ Parallel processing shows improvement');
        } else {
            console.log('⚠️  Parallel processing may not show significant improvement for small files');
        }
        
        // 验证输出文件
        console.log('');
        console.log('🔍 Verifying output files...');
        const outputFiles = fs.readdirSync(testOptimizedDir).filter(f => f.endsWith('.js'));
        console.log(`✅ Generated ${outputFiles.length} optimized files`);
        
        // 检查第一个文件的内容
        if (outputFiles.length > 0) {
            const firstOutputPath = path.join(testOptimizedDir, outputFiles[0]);
            const firstOutputContent = fs.readFileSync(firstOutputPath, 'utf8');
            const hasConsole = firstOutputContent.includes('console.');
            
            if (hasConsole) {
                console.log('⚠️  Warning: Output still contains console statements');
            } else {
                console.log('✅ Console statements successfully removed');
            }
        }
        
    } catch (error) {
        console.error('❌ Test failed:', error.message);
        console.error(error.stack);
    } finally {
        // 清理测试文件
        console.log('');
        console.log('🧹 Cleaning up test files...');
        if (fs.existsSync(testDir)) {
            fs.rmSync(testDir, { recursive: true, force: true });
        }
        if (fs.existsSync(testOptimizedDir)) {
            fs.rmSync(testOptimizedDir, { recursive: true, force: true });
        }
        console.log('✅ Cleanup completed');
    }
}

/**
 * 检查Worker线程环境
 */
function checkWorkerEnvironment() {
    console.log('🔍 Worker Thread Environment Check');
    console.log('===================================');
    console.log(`isMainThread: ${isMainThread}`);
    console.log(`Worker threads available: ${typeof Worker !== 'undefined'}`);
    console.log(`Node.js version: ${process.version}`);
    console.log('');
}

/**
 * 显示并行处理配置信息
 */
function showParallelConfig() {
    console.log('⚙️  Parallel Processing Configuration');
    console.log('==================================');
    
    const config = {
        maxConcurrentWorkers: 4,
        workerScript: path.join(__dirname, 'worker.js'),
        condition: 'jsFiles.length > 3 AND parallel=true AND isMainThread=true'
    };
    
    console.log(`Max concurrent workers: ${config.maxConcurrentWorkers}`);
    console.log(`Worker script location: ${config.workerScript}`);
    console.log(`Activation condition: ${config.condition}`);
    console.log('');
}

// 主程序
async function main() {
    console.log('🧪 Parallel Processing Test Suite');
    console.log('=================================');
    console.log('');
    
    checkWorkerEnvironment();
    showParallelConfig();
    
    if (!isMainThread) {
        console.log('⚠️  This test should only be run in the main thread');
        return;
    }
    
    try {
        await testParallelProcessing();
    } catch (error) {
        console.error('❌ Parallel test failed:', error.message);
        process.exit(1);
    }
    
    console.log('');
    console.log('🎉 Parallel processing test completed!');
    console.log('');
    console.log('💡 To use parallel processing in your projects:');
    console.log('   node scripts/optimize-dist.js          # Automatic for >3 files');
    console.log('   node scripts/optimized-processor.js ./dist --parallel');
}

if (require.main === module) {
    main().catch(console.error);
}

module.exports = {
    testParallelProcessing,
    checkWorkerEnvironment,
    showParallelConfig
};