#!/usr/bin/env node

/**
 * STAGING TEST RUNNER
 *
 * Quick script to run comprehensive staging tests with proper configuration
 * and environment variable handling.
 */

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

// Configuration
const STAGING_URL = 'https://upskill-staging.measurequick.com';
const TEST_FILE = './test-comprehensive-e2e-staging.js';

// Test account configuration (update these as needed)
const TEST_ACCOUNTS = {
    TRAINER_USERNAME: 'test_trainer',
    TRAINER_PASSWORD: 'TestTrainer123!',
    MASTER_USERNAME: 'test_master',
    MASTER_PASSWORD: 'TestMaster123!'
};

function runTests() {
    console.log('🚀 Starting Comprehensive Staging Tests');
    console.log(`🌐 Target URL: ${STAGING_URL}`);

    // Check if test file exists
    if (!fs.existsSync(TEST_FILE)) {
        console.error(`❌ Test file not found: ${TEST_FILE}`);
        process.exit(1);
    }

    // Set environment variables
    const env = {
        ...process.env,
        BASE_URL: STAGING_URL,
        HEADLESS: 'false', // Set to 'true' for headless mode
        ...TEST_ACCOUNTS
    };

    try {
        console.log('\n📋 Test Configuration:');
        console.log(`  - Headless Mode: ${env.HEADLESS}`);
        console.log(`  - Base URL: ${env.BASE_URL}`);
        console.log(`  - Trainer Account: ${env.TRAINER_USERNAME}`);
        console.log(`  - Master Account: ${env.MASTER_USERNAME}`);

        console.log('\n▶️  Starting test execution...\n');

        // Run the test
        execSync(`node ${TEST_FILE}`, {
            stdio: 'inherit',
            env: env
        });

        console.log('\n✅ Test execution completed successfully');

    } catch (error) {
        console.error('\n❌ Test execution failed');
        console.error(`Exit code: ${error.status}`);
        process.exit(error.status || 1);
    }
}

// Handle command line arguments
const args = process.argv.slice(2);

if (args.includes('--help') || args.includes('-h')) {
    console.log(`
🧪 STAGING TEST RUNNER

Usage: node staging-test-runner.js [options]

Options:
  --headless          Run tests in headless mode
  --trainer-user      Set trainer username
  --master-user       Set master trainer username
  --help, -h          Show this help message

Examples:
  node staging-test-runner.js
  node staging-test-runner.js --headless
  node staging-test-runner.js --trainer-user my_trainer --master-user my_master
    `);
    process.exit(0);
}

// Handle headless mode
if (args.includes('--headless')) {
    TEST_ACCOUNTS.HEADLESS = 'true';
}

// Handle custom usernames
const trainerUserIndex = args.indexOf('--trainer-user');
if (trainerUserIndex >= 0 && args[trainerUserIndex + 1]) {
    TEST_ACCOUNTS.TRAINER_USERNAME = args[trainerUserIndex + 1];
}

const masterUserIndex = args.indexOf('--master-user');
if (masterUserIndex >= 0 && args[masterUserIndex + 1]) {
    TEST_ACCOUNTS.MASTER_USERNAME = args[masterUserIndex + 1];
}

// Run the tests
runTests();