$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
Set-Location $ProjectRoot

$sdk = "C:\Users\Administrator\AppData\Local\Android\Sdk"
$adb = Join-Path $sdk "platform-tools\adb.exe"
if (-not (Test-Path $adb)) {
    Write-Host "adb not found: $adb" -ForegroundColor Red
    exit 1
}

Write-Host "=== Step 1/5 Build ===" -ForegroundColor Cyan
& "$ProjectRoot\scripts\build-debug.ps1"
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

$appApk = Join-Path $ProjectRoot "app\build\outputs\apk\debug\app-debug.apk"
$xposedApk = Join-Path $ProjectRoot "xposed-module\build\outputs\apk\debug\xposed-module-debug.apk"

Write-Host "`n=== Step 2/5 Device ===" -ForegroundColor Cyan
& $adb devices
$device = (& $adb devices | Select-String "device$" | Select-Object -First 1)
if (-not $device) {
    Write-Host "No authorized adb device" -ForegroundColor Red
    exit 1
}

Write-Host "`n=== Step 3/5 Install APKs ===" -ForegroundColor Cyan
& $adb shell settings put global verifier_verify_adb_installs 0 2>$null
& $adb install -r -t -g $appApk
if ($LASTEXITCODE -ne 0) {
    & $adb install -r -t -g --bypass-low-target-sdk-block $appApk
}
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& $adb install -r -t -g $xposedApk
if ($LASTEXITCODE -ne 0) {
    & $adb install -r -t -g --bypass-low-target-sdk-block $xposedApk
}
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

Write-Host "`n=== Step 4/5 LSPosed scope ===" -ForegroundColor Cyan
$apkPath = (& $adb shell pm path com.miraclegarden.smsmessage.xposed 2>$null) -replace '^package:', ''
$apkPath = $apkPath.Trim()
if (-not $apkPath) {
    Write-Host "Failed to get xposed module path" -ForegroundColor Red
    exit 1
}
Write-Host "xposed apk: $apkPath"

& $adb shell "su -c 'cp /data/adb/lspd/config/modules_config.db /sdcard/Download/modules_config.db; cp /data/adb/lspd/config/modules_config.db-wal /sdcard/Download/modules_config.db-wal 2>/dev/null; cp /data/adb/lspd/config/modules_config.db-shm /sdcard/Download/modules_config.db-shm 2>/dev/null; chmod 644 /sdcard/Download/modules_config.db*'"
$db = Join-Path $env:TEMP "modules_config_full.db"
$prevEap = $ErrorActionPreference
$ErrorActionPreference = "Continue"
& $adb pull /sdcard/Download/modules_config.db $db 2>&1 | Out-Null
& $adb pull /sdcard/Download/modules_config.db-wal "$db-wal" 2>&1 | Out-Null
& $adb pull /sdcard/Download/modules_config.db-shm "$db-shm" 2>&1 | Out-Null
$ErrorActionPreference = $prevEap
python "$ProjectRoot\scripts\configure-lsposed.py" $db $apkPath
& $adb push $db /sdcard/Download/modules_config.db | Out-Null
& $adb shell "su -c 'cp /sdcard/Download/modules_config.db /data/adb/lspd/config/modules_config.db; rm -f /data/adb/lspd/config/modules_config.db-wal /data/adb/lspd/config/modules_config.db-shm; chmod 660 /data/adb/lspd/config/modules_config.db'"

Write-Host "`n=== Step 5/5 System tweaks ===" -ForegroundColor Cyan
& $adb reverse tcp:8765 tcp:8765 2>$null
& $adb shell "su -c 'dumpsys deviceidle whitelist +com.miraclegarden.smsmessage'" 2>$null
& $adb shell "cmd notification allow_listener com.miraclegarden.smsmessage/com.miraclegarden.smsmessage.service.NotificationService" 2>$null
& $adb shell "am force-stop org.telegram.messenger.web"
& $adb shell "am force-stop com.miraclegarden.smsmessage"

Write-Host ""
Write-Host "Full install done." -ForegroundColor Green
Write-Host "Installed: app + xposed module"
Write-Host "Configured: LSPosed scope, adb reverse, battery whitelist, notification listener"
Write-Host "Next: open notiMessage -> start monitoring -> reopen Telegram"
