/**
 * @fileoverview Runs mobile tests based on available platforms and devices
 * 
 * This script intelligently detects available mobile platforms and their
 * associated devices/simulators, then runs tests only for the available
 * configurations. This allows for flexible testing across different
 * development environments without failing when a platform is unavailable.
 * 
 * Platform detection:
 * - Android: Checks for SDK and connected devices/emulators
 * - iOS: Checks for macOS, Xcode, and running simulators
 * 
 * Features:
 * - Smart platform detection
 * - Graceful handling of unavailable platforms
 * - Clear logging of test execution
 * - Comprehensive error reporting
 * 
 * Exit codes:
 * - 0: Tests completed successfully on available platforms
 * - 1: Tests failed or no platforms available
 * 
 * @example
 * // Run directly
 * node scripts/run-available-mobile-tests.js
 * 
 * // Run via npm script
 * npm run test:mobile:available
 * 
 * @requires child_process
 * @requires fs
 * 
 * @author TimeSafari Team
 * @license MIT
 */

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

/**
 * Executes mobile tests on available platforms
 * 
 * This function performs the following steps:
 * 1. Checks Android environment and device availability
 * 2. Checks iOS environment and simulator availability (on macOS)
 * 3. Runs tests on available platforms
 * 4. Reports results and handles errors
 * 
 * Platform-specific checks:
 * Android:
 * - ANDROID_HOME environment variable
 * - Android platform files existence
 * - Connected devices via ADB
 * 
 * iOS:
 * - macOS operating system
 * - iOS platform files existence
 * - Running simulators via xcrun
 * 
 * @async
 * @throws {Error} If tests fail or no platforms are available
 * 
 * @example
 * runAvailableMobileTests().catch(error => {
 *     console.error('Test execution failed:', error);
 *     process.exit(1);
 * });
 */
async function runAvailableMobileTests() {
    try {
        // Check Android availability
        // Requires both SDK (ANDROID_HOME) and platform files
        const androidAvailable = existsSync('android') && process.env.ANDROID_HOME;
        let androidDeviceAvailable = false;

        if (androidAvailable) {
            try {
                // Check for connected devices using ADB
                const devices = execSync('adb devices').toString();
                // Parse ADB output for actually connected devices
                // Filters out unauthorized or offline devices
                androidDeviceAvailable = devices.split('\n').slice(1).some(line => line.includes('device'));
            } catch (e) {
                console.log('⚠️ Android SDK available but no devices connected');
            }
        }

        // Check iOS availability
        // Only possible on macOS with Xcode installed
        const iosAvailable = process.platform === 'darwin' && existsSync('ios');
        let iosSimulatorAvailable = false;

        if (iosAvailable) {
            try {
                // Check for running simulators using xcrun
                const simulators = execSync('xcrun simctl list devices available').toString();
                // Look for 'Booted' state in simulator list
                iosSimulatorAvailable = simulators.includes('Booted');
            } catch (e) {
                console.log('⚠️ iOS platform available but no simulator running');
            }
        }

        // Execute tests for available platforms
        if (androidDeviceAvailable) {
            console.log('🤖 Running Android tests...');
            // Run Android tests via npm script
            execSync('npm run test:android', { stdio: 'inherit' });
        }

        if (iosSimulatorAvailable) {
            console.log('🍎 Running iOS tests...');
            // Run iOS tests via npm script
            execSync('npm run test:ios', { stdio: 'inherit' });
        }

        // Error if no platforms are available for testing
        if (!androidDeviceAvailable && !iosSimulatorAvailable) {
            console.error('❌ No mobile platforms available for testing');
            process.exit(1);
        }

        console.log('✅ Available mobile tests completed successfully');
    } catch (error) {
        // Handle any errors during test execution
        console.error('❌ Mobile tests failed:', error);
        process.exit(1);
    }
}

// Execute the test runner
runAvailableMobileTests();