# ProGuard/R8 consumer rules for Appstack Attribution SDK
#
# This file is packaged inside the AAR produced by the :sdk module.
# When an app depends on this AAR, its contents are automatically merged into
# the final R8/ProGuard rules applied during the consumer app's minification.
#
# These rules prevent runtime crashes (ClassNotFoundException, NoSuchMethodException)
# that occur when reflection-based or serialized classes are obfuscated.

# ================================================================================
# Appstack SDK Core Classes
# ================================================================================

# Keep all public API classes and their members
-keep class com.appstack.attribution.AppstackAttributionSdk { *; }
-keep class com.appstack.attribution.Config { *; }
-keep class com.appstack.attribution.EventType { *; }
-keep class com.appstack.attribution.LogLevel { *; }

# Keep listener interfaces
-keep interface com.appstack.attribution.InitListener { *; }

# ================================================================================
# Data Classes (Moshi JSON Serialization)
# ================================================================================

# Keep all data classes used in JSON serialization
-keep class com.appstack.attribution.TrackedEvent { *; }
-keep class com.appstack.attribution.EventsBatchPayload { *; }
-keep class com.appstack.attribution.RemoteConfig { *; }
-keep class com.appstack.attribution.UTMParameters { *; }
-keep class com.appstack.attribution.UserData { *; }

# Keep @Json annotated fields and constructors
-keepclassmembers class com.appstack.attribution.** {
    @com.squareup.moshi.Json <fields>;
    <init>(...);
}

# ================================================================================
# Reflection-based Classes
# ================================================================================

# RetrofitClient - network layer implementation
-keep class com.appstack.attribution.RetrofitClient {
    <init>(...);
    *;
}

# LazyNetworkClient - lazy wrapper for network client
-keep class com.appstack.attribution.LazyNetworkClient {
    <init>(...);
    *;
}

# SharedPrefsStorage is loaded via reflection
-keep class com.appstack.attribution.SharedPrefsStorage {
    <init>(...);
    *;
}

# ================================================================================
# WorkManager Workers
# ================================================================================

# Keep WorkManager worker classes (called by system via reflection)
-keep class com.appstack.attribution.FlushWorker { *; }
-keep class com.appstack.attribution.ConfigRefreshWorker { *; }

# ================================================================================
# Moshi
# ================================================================================

# Keep Moshi-generated adapters
-keep class com.appstack.attribution.**JsonAdapter { *; }

# Keep @JsonClass annotated classes
-keep @com.squareup.moshi.JsonClass class * { *; }

# Keep Kotlin metadata for Moshi
-keepclassmembers class * {
    @com.squareup.moshi.FromJson <methods>;
    @com.squareup.moshi.ToJson <methods>;
}

# ================================================================================
# Retrofit
# ================================================================================

# Keep generic signature of Retrofit service methods
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
-keepattributes AnnotationDefault

# Keep Retrofit service interfaces
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

# Retrofit does reflection on generic parameters
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# ================================================================================
# OkHttp
# ================================================================================

-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn javax.annotation.**

# Keep OkHttp platform classes
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# ================================================================================
# Kotlin Coroutines
# ================================================================================

-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}

# Keep Continuation for suspend functions
-keepclassmembers class kotlin.coroutines.Continuation { *; }

# ================================================================================
# Kotlin
# ================================================================================

# Keep Kotlin metadata (needed for reflection on Kotlin classes)
-keepattributes *Annotation*, Signature, Exception

# Keep Kotlin data class component functions
-keepclassmembers class com.appstack.attribution.** {
    public ** component*();
    public ** copy(...);
}

# ================================================================================
# Android Lifecycle
# ================================================================================

# Keep lifecycle observer
-keep class com.appstack.attribution.AppLifecycleObserver { *; }

# ================================================================================
# Install Referrer
# ================================================================================

-keep class com.android.installreferrer.** { *; }
-keep interface com.android.installreferrer.** { *; }
