/**
 * Main Test Suite Runner
 * Organizes and runs all UI tests in a structured manner
 */

const { spawn } = require('child_process');
const path = require('path');

// Test suite organization
const TEST_SUITES = {
    'Core': [
        'test_auth_flow.js',
        'test_research_simple.js',
        'test_research_complete.js'
    ],
    'UI/Responsive': [
        'test_responsive_ui_comprehensive.js',  // Covers all viewports
        'test_critical_ui.js'
    ],
    'Features': [
        'test_api_key_comprehensive.js',
        'test_metrics_full_flow.js',
        'test_history_page.js',
        'test_news_subscription.js',
        'test_settings_page.js'
    ],
    'Advanced': [
        'test_followup_research.js',
        'test_queue_processing.js',
        'test_cost_tracking.js'
    ]
};

async function runTest(testFile) {
    return new Promise((resolve) => {
        console.log(`  Running: ${testFile}`);
        const testPath = path.join(__dirname, testFile);

        const child = spawn('node', [testPath], {
            env: { ...process.env },
            stdio: 'inherit'
        });

        child.on('close', (code) => {
            if (code !== 0) {
                console.log(`  ❌ ${testFile} failed with code ${code}`);
                resolve(false);
            } else {
                console.log(`  ✅ ${testFile} passed`);
                resolve(true);
            }
        });

        child.on('error', (err) => {
            console.error(`  ❌ ${testFile} error:`, err);
            resolve(false);
        });
    });
}

async function runSuite(suiteName, tests) {
    console.log(`\n${'='.repeat(60)}`);
    console.log(`📦 ${suiteName} Test Suite`);
    console.log('='.repeat(60));

    let passed = 0;
    let failed = 0;

    for (const test of tests) {
        const result = await runTest(test);
        if (result) {
            passed++;
        } else {
            failed++;
        }
    }

    console.log(`\nSuite Results: ✅ ${passed} passed, ❌ ${failed} failed`);
    return failed === 0;
}

async function main() {
    const args = process.argv.slice(2);
    const specificSuite = args[0];

    console.log('\n🧪 LDR UI Test Suite Runner\n');

    let allPassed = true;

    if (specificSuite && TEST_SUITES[specificSuite]) {
        // Run specific suite
        const result = await runSuite(specificSuite, TEST_SUITES[specificSuite]);
        allPassed = result;
    } else if (specificSuite) {
        console.error(`Unknown suite: ${specificSuite}`);
        console.log('Available suites:', Object.keys(TEST_SUITES).join(', '));
        process.exit(1);
    } else {
        // Run all suites
        for (const [suiteName, tests] of Object.entries(TEST_SUITES)) {
            const result = await runSuite(suiteName, tests);
            if (!result) allPassed = false;
        }
    }

    console.log('\n' + '='.repeat(60));
    if (allPassed) {
        console.log('🎉 All tests passed!');
    } else {
        console.log('❌ Some tests failed');
        process.exit(1);
    }
}

if (require.main === module) {
    main().catch(error => {
        console.error('Test runner failed:', error);
        process.exit(1);
    });
}

module.exports = { TEST_SUITES };
