#!/bin/bash
# Diagnostic script to determine why prefetch logs aren't appearing in APK

set -e

echo "=== Prefetch Scheduling Diagnostic ==="
echo ""

# 1. Check source files
echo "1. Checking source files..."
echo ""
SOURCE_FILE="android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.java"
NODEMODS_FILE="test-apps/daily-notification-test/node_modules/@timesafari/daily-notification-plugin/android/plugin/src/main/java/com/timesafari/dailynotification/DailyNotificationPlugin.java"

if [ -f "$NODEMODS_FILE" ]; then
    echo "   ✓ Symlinked source exists"
    diff -q "$SOURCE_FILE" "$NODEMODS_FILE" 2>/dev/null && echo "   ✓ Symlink matches source" || echo "   ✗ Symlink is out of sync"
else
    echo "   ✗ Symlinked source missing!"
fi

# 2. Check which log strings exist in source
echo ""
echo "2. Checking for expected log strings in source..."
echo ""
grep -n "DN|SCHEDULE_CALLBACK" "$SOURCE_FILE" 2>/dev/null | head -3 && echo "   ✓ Found DN|SCHEDULE_CALLBACK" || echo "   ✗ Missing DN|SCHEDULE_CALLBACK"
grep -n "DN|SCHEDULE_FETCH_START" "$SOURCE_FILE" 2>/dev/null | head -1 && echo "   ✓ Found DN|SCHEDULE_FETCH_START" || echo "   ✗ Missing DN|SCHEDULE_FETCH_START"
grep -n "DN|SCHEDULE_RESULT" "$SOURCE_FILE" 2>/dev/null | head -1 && echo "   ✓ Found DN|SCHEDULE_RESULT" || echo "   ✗ Missing DN|SCHEDULE_RESULT"

# 3. Check what's actually compiled in the APK
echo ""
echo "3. Checking compiled APK..."
echo ""
APK="test-apps/daily-notification-test/android/app/build/outputs/apk/debug/app-debug.apk"

if [ -f "$APK" ]; then
    echo "   Checking for log strings in classes.dex..."
    unzip -p "$APK" classes.dex 2>/dev/null | strings | grep -i "DN|SCHEDULE" | sort -u
    echo ""
    HAS_CALLBACK=$(unzip -p "$APK" classes.dex 2>/dev/null | strings | grep -c "DN|SCHEDULE_CALLBACK" || echo "0")
    HAS_FETCH=$(unzip -p "$APK" classes.dex 2>/dev/null | strings | grep -c "DN|SCHEDULE_FETCH" || echo "0")
    HAS_RESULT=$(unzip -p "$APK" classes.dex 2>/dev/null | strings | grep -c "DN|SCHEDULE_RESULT" || echo "0")
    
    [ "$HAS_CALLBACK" -gt "0" ] && echo "   ✓ APK contains DN|SCHEDULE_CALLBACK" || echo "   ✗ APK missing DN|SCHEDULE_CALLBACK"
    [ "$HAS_FETCH" -gt "0" ] && echo "   ✓ APK contains DN|SCHEDULE_FETCH_*" || echo "   ✗ APK missing DN|SCHEDULE_FETCH_*"
    [ "$HAS_RESULT" -gt "0" ] && echo "   ✓ APK contains DN|SCHEDULE_RESULT" || echo "   ✗ APK missing DN|SCHEDULE_RESULT"
else
    echo "   ✗ APK not found at $APK"
    echo "   Run: cd test-apps/daily-notification-test/android && ./gradlew assembleDebug"
fi

# 4. Check capacitor.settings.gradle
echo ""
echo "4. Checking capacitor.settings.gradle..."
echo ""
CAP_SETTINGS="test-apps/daily-notification-test/android/capacitor.settings.gradle"
if grep -q "android/plugin" "$CAP_SETTINGS"; then
    echo "   ✓ Points to correct path (android/plugin)"
else
    echo "   ✗ Points to wrong path"
    echo "   Current setting:"
    grep "projectDir" "$CAP_SETTINGS" || echo "   Not found"
fi

echo ""
echo "=== Diagnosis Complete ==="
echo ""
echo "If APK is missing the log strings:"
echo "  1. Ensure capacitor.settings.gradle points to 'android/plugin'"
echo "  2. Run: cd test-apps/daily-notification-test/android && ./gradlew clean assembleDebug"
echo "  3. Re-run this diagnostic script"
echo ""

