#!/usr/bin/env node

/**
 * Devicely CLI Entry Point
 * AI-Powered Device Pool
 */

const { program } = require('commander');
const chalk = require('chalk');
const packageJson = require('../package.json');
const { startServer } = require('../lib/server');
const { listDevices } = require('../lib/devices');
const { checkDoctor } = require('../lib/doctor');
const { executeCommand } = require('../lib/executor');

// Configure CLI
program
  .name('devicely')
  .description('Devicely - One Command, All Devices. AI Powered Mobile Automation')
  .version(packageJson.version, '-v, --version', 'Display version number')
  .helpOption('-h, --help', 'Display help information');

// Start command - Launch server and open web UI
program
  .command('start')
  .description('Start Devicely server and open web interface')
  .option('-p, --port <port>', 'Server port', '3001')
  .option('--no-browser', 'Don\'t open browser automatically')
  .option('--debug', 'Enable debug logging')
  .option('--usb-only', 'Detect USB devices only')
  .option('--wifi-only', 'Detect WiFi devices only')
  .action(async (options) => {
    try {
      console.log(chalk.cyan.bold('\n🚀 Devicely - One Command, All Devices'));
      console.log(chalk.gray('   AI Powered Mobile Automation'));
      console.log(chalk.gray('━'.repeat(50)));
      
      await startServer(options);
    } catch (error) {
      console.error(chalk.red('\n❌ Error:'), error.message);
      process.exit(1);
    }
  });

// List command - Show connected devices
program
  .command('list')
  .description('List all connected iOS and Android devices')
  .option('-v, --verbose', 'Show detailed device information')
  .action(async (options) => {
    try {
      console.log(chalk.cyan.bold('\n📱 Scanning for devices...'));
      console.log(chalk.gray('━'.repeat(50)));
      
      await listDevices(options);
    } catch (error) {
      console.error(chalk.red('\n❌ Error:'), error.message);
      process.exit(1);
    }
  });

// Doctor command - Check system requirements
program
  .command('doctor')
  .description('Check system requirements for iOS and Android automation')
  .action(async () => {
    try {
      console.log(chalk.cyan.bold('\n🔍 Checking system requirements...'));
      console.log(chalk.gray('━'.repeat(50)));
      
      await checkDoctor();
    } catch (error) {
      console.error(chalk.red('\n❌ Error:'), error.message);
      process.exit(1);
    }
  });

// Exec command - Execute command on devices
program
  .command('exec <command>')
  .description('Execute command on connected iOS/Android devices')
  .option('-d, --device <name>', 'Execute on specific device')
  .option('-a, --all', 'Execute on all devices (default)')
  .action(async (command, options) => {
    try {
      console.log(chalk.cyan.bold('\n⚡ Executing command...'));
      console.log(chalk.gray('━'.repeat(50)));
      
      await executeCommand(command, options);
    } catch (error) {
      console.error(chalk.red('\n❌ Error:'), error.message);
      process.exit(1);
    }
  });

// Parse arguments
program.parse(process.argv);

// Show help if no command provided
if (!process.argv.slice(2).length) {
  program.outputHelp();
  console.log(chalk.cyan('\n💡 Quick start:'));
  console.log(chalk.gray('  devicely start    ') + chalk.white('# Start the server'));
  console.log(chalk.gray('  devicely list     ') + chalk.white('# List devices'));
  console.log(chalk.gray('  devicely doctor   ') + chalk.white('# Check system'));
  console.log('');
}
