#!/bin/bash
# reboot-test-v2.sh - Enhanced Reboot Testing with RecoveryManager

set -e

APP_PACKAGE="org.timesafari.dailynotification"
APP_ACTIVITY=".MainActivity"

echo "🔄 DailyNotification Reboot Test Suite v2.0"
echo "=========================================="
echo "Testing RecoveryManager-based reboot recovery"
echo "✅ Boot Receiver with RecoveryManager"
echo "✅ App Startup Recovery Coexistence"
echo "✅ Recovery Statistics Tracking"
echo "✅ Idempotent Recovery Operations"
echo ""

# Test results tracking
declare -A test_results

# Function to run test and track result
run_test() {
    local test_name="$1"
    local test_function="$2"
    
    echo "🧪 Running: $test_name"
    echo "----------------------------------------"
    
    if $test_function; then
        echo "✅ PASS: $test_name"
        test_results["$test_name"]="PASS"
    else
        echo "❌ FAIL: $test_name"
        test_results["$test_name"]="FAIL"
    fi
    echo ""
}

# Function to wait for device
wait_for_device() {
    echo "⏳ Waiting for device to be ready..."
    adb wait-for-device
    sleep 15  # Additional wait for boot completion
    
    # Check if device is fully booted
    local boot_completed=$(adb shell getprop sys.boot_completed)
    if [ "$boot_completed" != "1" ]; then
        echo "⏳ Device not fully booted, waiting more..."
        sleep 15
    fi
}

# Function to schedule notification
schedule_notification() {
    echo "📅 Scheduling test notification..."
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 3
    
    echo "⚠️  Manual step: Schedule notification in app"
    echo "   - Tap 'Test Notification' button"
    echo "   - Wait for 'Notification scheduled' message"
    read -p "Press Enter when notification is scheduled..."
}

# Function to check recovery logs
check_recovery_logs() {
    local source="$1"
    echo "🔍 Checking recovery logs for: $source"
    
    if adb logcat -d | grep -q "Recovery requested from: $source"; then
        echo "✅ Recovery triggered from: $source"
        return 0
    else
        echo "❌ Recovery not triggered from: $source"
        return 1
    fi
}

# Function to check recovery cooldown
check_recovery_cooldown() {
    echo "🔍 Checking recovery cooldown behavior..."
    
    local recovery_count=$(adb logcat -d | grep -c "Recovery requested from: APP_STARTUP")
    local cooldown_count=$(adb logcat -d | grep -c "Recovery performed recently.*skipping")
    
    if [ $recovery_count -eq 1 ] && [ $cooldown_count -gt 0 ]; then
        echo "✅ Recovery cooldown working correctly"
        echo "   - Recovery performed: $recovery_count times"
        echo "   - Cooldown skips: $cooldown_count times"
        return 0
    else
        echo "❌ Recovery cooldown not working"
        echo "   - Recovery performed: $recovery_count times"
        echo "   - Cooldown skips: $cooldown_count times"
        return 1
    fi
}

# Function to check scheduled alarms
check_scheduled_alarms() {
    echo "⏰ Checking scheduled alarms..."
    if adb shell "dumpsys alarm | grep timesafari" | grep -q "RTC_WAKEUP"; then
        echo "✅ Alarms scheduled correctly"
        return 0
    else
        echo "❌ No alarms found"
        return 1
    fi
}

# Test 1: Boot Recovery with RecoveryManager
test_boot_recovery() {
    echo "🔄 Testing Boot Recovery with RecoveryManager..."
    
    # Schedule notification
    schedule_notification
    
    # Verify initial scheduling
    if ! check_scheduled_alarms; then
        echo "❌ Initial scheduling failed"
        return 1
    fi
    
    # Clear logs before reboot
    adb logcat -c
    
    # Reboot device
    echo "🔄 Rebooting device..."
    adb reboot
    wait_for_device
    
    # Check boot recovery logs
    if ! check_recovery_logs "BOOT_COMPLETED"; then
        echo "❌ Boot recovery not triggered"
        return 1
    fi
    
    # Check alarms restored
    if ! check_scheduled_alarms; then
        echo "❌ Alarms not restored after reboot"
        return 1
    fi
    
    echo "✅ Boot recovery working correctly"
    return 0
}

# Test 2: App Startup Recovery Coexistence
test_app_startup_coexistence() {
    echo "🔄 Testing App Startup Recovery Coexistence..."
    
    # Clear logs
    adb logcat -c
    
    # Launch app multiple times to test cooldown
    echo "🚀 Testing app startup recovery cooldown..."
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    
    # Check recovery cooldown behavior
    if ! check_recovery_cooldown; then
        echo "❌ Recovery cooldown not working"
        return 1
    fi
    
    echo "✅ App startup recovery coexistence working"
    return 0
}

# Test 3: Package Replacement Recovery
test_package_replacement() {
    echo "🔄 Testing Package Replacement Recovery..."
    
    # Schedule notification
    schedule_notification
    
    # Verify initial scheduling
    if ! check_scheduled_alarms; then
        echo "❌ Initial scheduling failed"
        return 1
    fi
    
    # Clear logs
    adb logcat -c
    
    # Update app (simulate package replacement)
    echo "📦 Updating app (simulating package replacement)..."
    cd android && ./gradlew assembleDebug
    adb install -r app/build/outputs/apk/debug/app-debug.apk
    cd ..
    
    # Check package replacement recovery logs
    if ! check_recovery_logs "MY_PACKAGE_REPLACED"; then
        echo "❌ Package replacement recovery not triggered"
        return 1
    fi
    
    # Check alarms restored
    if ! check_scheduled_alarms; then
        echo "❌ Alarms not restored after package replacement"
        return 1
    fi
    
    echo "✅ Package replacement recovery working"
    return 0
}

# Test 4: Recovery Statistics
test_recovery_statistics() {
    echo "📊 Testing Recovery Statistics..."
    
    # Clear logs
    adb logcat -c
    
    # Perform multiple recovery operations
    echo "🔄 Performing multiple recovery operations..."
    
    # Launch app (triggers startup recovery)
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    
    # Launch app again (should be skipped due to cooldown)
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    
    # Check recovery statistics in logs
    local recovery_count=$(adb logcat -d | grep -c "Recovery requested from:")
    local cooldown_count=$(adb logcat -d | grep -c "Recovery performed recently.*skipping")
    
    if [ $recovery_count -gt 0 ] && [ $cooldown_count -gt 0 ]; then
        echo "✅ Recovery statistics tracking working"
        echo "   - Total recovery requests: $recovery_count"
        echo "   - Cooldown skips: $cooldown_count"
        return 0
    else
        echo "❌ Recovery statistics not tracking correctly"
        return 1
    fi
}

# Test 5: Recovery State Persistence
test_recovery_state_persistence() {
    echo "💾 Testing Recovery State Persistence..."
    
    # Clear logs
    adb logcat -c
    
    # Launch app to trigger recovery
    adb shell am start -n $APP_PACKAGE/$APP_ACTIVITY
    sleep 2
    
    # Check if recovery state is persisted
    local recovery_state=$(adb shell "run-as $APP_PACKAGE ls -la /data/data/$APP_PACKAGE/shared_prefs/ | grep recovery_state")
    
    if [ -n "$recovery_state" ]; then
        echo "✅ Recovery state persisted"
        echo "   - Recovery state file: $recovery_state"
        return 0
    else
        echo "❌ Recovery state not persisted"
        return 1
    fi
}

# Test 6: Multiple Notifications Recovery
test_multiple_notifications_recovery() {
    echo "📱 Testing Multiple Notifications Recovery..."
    
    # Schedule multiple notifications
    echo "📅 Scheduling multiple notifications..."
    echo "⚠️  Manual step: Schedule 3-4 notifications in app UI"
    echo "   - Tap 'Test Notification' multiple times"
    echo "   - Wait for each 'Notification scheduled' message"
    read -p "Press Enter when multiple notifications are scheduled..."
    
    # Count initial alarms
    local initial_alarms=$(adb shell "dumpsys alarm | grep timesafari | wc -l")
    echo "📊 Initial alarms scheduled: $initial_alarms"
    
    if [ $initial_alarms -lt 2 ]; then
        echo "❌ Not enough notifications scheduled for test"
        return 1
    fi
    
    # Clear logs
    adb logcat -c
    
    # Reboot device
    echo "🔄 Rebooting device..."
    adb reboot
    wait_for_device
    
    # Check recovery logs
    if ! check_recovery_logs "BOOT_COMPLETED"; then
        echo "❌ Boot recovery not triggered"
        return 1
    fi
    
    # Count restored alarms
    local restored_alarms=$(adb shell "dumpsys alarm | grep timesafari | wc -l")
    echo "📊 Restored alarms: $restored_alarms"
    
    if [ $restored_alarms -eq $initial_alarms ]; then
        echo "✅ All notifications recovered correctly"
        return 0
    else
        echo "❌ Not all notifications recovered"
        echo "   - Initial: $initial_alarms, Restored: $restored_alarms"
        return 1
    fi
}

# Main test execution
main() {
    echo "🚀 Starting Reboot Test Suite v2.0..."
    echo ""
    
    # Run all tests
    run_test "Boot Recovery with RecoveryManager" test_boot_recovery
    run_test "App Startup Recovery Coexistence" test_app_startup_coexistence
    run_test "Package Replacement Recovery" test_package_replacement
    run_test "Recovery Statistics" test_recovery_statistics
    run_test "Recovery State Persistence" test_recovery_state_persistence
    run_test "Multiple Notifications Recovery" test_multiple_notifications_recovery
    
    # Print results summary
    echo "📊 Reboot Test Results Summary"
    echo "============================="
    local pass_count=0
    local total_count=0
    
    for test_name in "${!test_results[@]}"; do
        local result="${test_results[$test_name]}"
        echo "$test_name: $result"
        if [ "$result" = "PASS" ]; then
            ((pass_count++))
        fi
        ((total_count++))
    done
    
    echo ""
    echo "Overall: $pass_count/$total_count tests passed"
    
    if [ $pass_count -eq $total_count ]; then
        echo "🎉 All reboot tests passed! RecoveryManager working correctly."
        echo "⏰ Wait for scheduled notifications to appear"
        exit 0
    else
        echo "❌ Some reboot tests failed. Check logs for details."
        exit 1
    fi
}

# Run main function
main
