/**
 * Device listing utility for Devicely CLI
 */

// Import deviceDetection from appropriate location
let discoverAllDevices;
try {
    ({ discoverAllDevices } = require('./deviceDetection'));
} catch (err) {
    ({ discoverAllDevices } = require('../../deviceDetection'));
}

async function listDevices(options = {}) {
    const devices = await discoverAllDevices();
    
    if (!devices || devices.length === 0) {
        console.log('\n⚠️  No devices found');
        console.log('\n💡 Tips:');
        console.log('   • For iOS: Connect device via USB or ensure Xcode pairing');
        console.log('   • For Android: Enable USB debugging and connect device');
        return;
    }

    console.log(`\n✅ Found ${devices.length} device(s):\n`);
    
    devices.forEach((device, index) => {
        const status = device.status === 'online' ? '🟢' : '🔴';
        const platform = device.platform === 'ios' ? '🍎' : '🤖';
        
        console.log(`${index + 1}. ${status} ${platform} ${device.name}`);
        
        if (options.verbose) {
            console.log(`   Platform: ${device.platform}`);
            console.log(`   UDID: ${device.udid}`);
            console.log(`   Type: ${device.type}`);
            console.log(`   Status: ${device.status}`);
            if (device.osVersion) {
                console.log(`   OS Version: ${device.osVersion}`);
            }
            if (device.port) {
                console.log(`   Port: ${device.port}`);
            }
            console.log('');
        }
    });
    
    if (!options.verbose) {
        console.log('\n💡 Use --verbose for detailed information');
    }
}

module.exports = {
    listDevices
};
