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

const projectRoot = path.resolve(__dirname, '..')
const adbPath = path.join(projectRoot, 'exe', 'adb', 'adb.exe')

const deviceIp = process.argv[2] || '192.168.2.5'
const devicePort = process.argv[3] || '5555'

const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
const output = execSync(connectCommand, { encoding: 'utf-8' })
const result = output.trim()
const isConnected = result.includes('connected') || result.includes('already connected')

if (isConnected) {
  process.stdout.write(`[OK] Device connected successfully: ${deviceIp}:${devicePort}\n`)
  process.exit(0)
} else {
  process.stdout.write(`[ERROR] Failed to connect to device: ${deviceIp}:${devicePort}\n`)
  process.exit(1)
}
