#!/usr/bin/env node
const { execSync } = require('child_process')
const path = require('path')

// Get adb path relative to project root
const projectRoot = path.resolve(__dirname, '../..')
const adbPath = path.join(projectRoot, 'exe', 'adb', 'adb.exe')

// Get list of connected devices
const devicesCommand = `"${adbPath}" devices`
const devicesOutput = execSync(devicesCommand, { encoding: 'utf-8' })

// Parse device list
const deviceLines = devicesOutput.split('\n').filter(line => line.trim() && !line.startsWith('List'))
const devices = deviceLines.map(line => {
  const parts = line.trim().split('\t')
  return parts[0]
}).filter(id => id)

if (devices.length === 0) {
  console.error('No devices found. Please connect a device via USB.')
  process.exit(1)
}

// Use first device if multiple devices are connected
const deviceId = devices[0]
if (devices.length > 1) {
  console.log(`Multiple devices found. Using first device: ${deviceId}`)
  console.log(`All devices: ${devices.join(', ')}`)
}

// Enable TCP/IP debugging on port 5555 via USB
const port = 5555
const command = `"${adbPath}" -s ${deviceId} tcpip ${port}`
const output = execSync(command, { encoding: 'utf-8' })
console.log(output.trim())
