# ============================================================================
# NETOMI CHAT SDK - CONSUMER PROGUARD RULES
# ============================================================================
# These rules are automatically applied to apps that consume this SDK library.
# They ensure the SDK works correctly in the consumer app's release build.
# ============================================================================

# ============================================================================
# 1. PUBLIC SDK API - MUST BE PRESERVED
# ============================================================================

# Keep the main SDK entry point
-keep public class com.netomi.chat.ui.init.NCWChatSdk {
    public *;
    @kotlin.jvm.JvmStatic public *;
}

# Keep public callback interfaces
-keep public interface com.netomi.chat.utils.ncw_interface.NCWLaunchCallback { *; }

# Keep public enums exposed in SDK API
-keep public enum com.netomi.chat.utils.NCWEnvironment { *; }
-keep public enum com.netomi.chat.utils.events.NetomiEventType { *; }
-keep public enum com.netomi.chat.utils.logger.NetomiSDKLogLevel { *; }
-keep public enum com.netomi.chat.utils.animation.NCWAnimationType { *; }
-keep public enum com.netomi.chat.utils.animation.NCWDismissBehavior { *; }

# Keep public configuration classes
-keep public class com.netomi.chat.model.animation.NCWAnimationConfig { *; }
-keep public class com.netomi.chat.model.theme.light_theme.** { *; }
-keep public class com.netomi.chat.model.theme.NCWApiHeaderConfig { *; }

# ============================================================================
# 2. DATA MODELS - REQUIRED FOR SERIALIZATION
# ============================================================================

# IMPORTANT: Many SDK model classes do NOT use @SerializedName and rely on
# Kotlin field-name -> JSON-key mapping (e.g. com.netomi.chat.model.theme.**,
# NCWGenericChannelResponse, request/response payloads, etc.). If those field
# names are obfuscated in the consumer's release build, Gson (de)serialization
# silently breaks. Therefore keep the model classes and their members fully.
-keep class com.netomi.chat.model.** { *; }
-keepclassmembers class com.netomi.chat.model.** { *; }

-keep class com.netomi.chat.survey.** { *; }
-keepclassmembers class com.netomi.chat.survey.** { *; }

# Also preserve @SerializedName-annotated fields against obfuscation everywhere.
-keepclassmembers,allowobfuscation class * {
    @com.google.gson.annotations.SerializedName <fields>;
}

# ============================================================================
# GENERIC SIGNATURES - CRITICAL FOR RETROFIT & GSON
# ============================================================================
# Without these attributes, R8 strips the JVM generic Signature metadata in the
# consumer's release build. Retrofit/Gson then read a nested generic return type
# such as Response<NCWBaseResponse<T>> as a raw Class instead of a
# ParameterizedType, causing the runtime crash:
#   "java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType".
# InnerClasses + EnclosingMethod are required for Signature to be usable.
-keepattributes Signature, InnerClasses, EnclosingMethod
-keepattributes AnnotationDefault
-keepattributes *Annotation*


# ============================================================================
# 3. RETROFIT & NETWORKING
# ============================================================================

# Keep Retrofit API interface
-keep interface com.netomi.chat.data.network.NCWApiInterface { *; }

# Keep Retrofit annotations
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# R8 full-mode safe Retrofit rules. In full mode, service interfaces (created
# via dynamic Proxy) have no visible subtypes, so R8 may otherwise replace their
# return values with null. Explicitly keep them and the generic signatures of
# Call/Response/Continuation that Retrofit reflects on.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface * extends <1>
-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn javax.annotation.**
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*

# OkHttp / Okio
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn org.conscrypt.**

# Gson core - keep so TypeToken / TypeAdapter reflection keeps working.
-keep class com.google.gson.** { *; }
-keep class sun.misc.Unsafe { *; }
-keep class * extends com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken


# ============================================================================
# 4. THIRD-PARTY LIBRARIES
# ============================================================================

# Note: Third-party libraries should provide their own consumer rules.
# Only keep SDK-specific integrations if needed.

# Project Reactor - optional Micrometer context propagation dependency.
# reactor.util.context.ReactorContextAccessor references
# io.micrometer.context.ContextAccessor, which is only present when the
# optional micrometer-context library is on the classpath. Suppress the R8
# "Missing class" warnings so consumer release builds don't fail.
-dontwarn io.micrometer.context.**
-dontwarn reactor.util.context.**

# ---------------------------------------------------------------------------
# AWS IoT Device SDK for Java v2 + AWS CRT - used by the "Chat Now" real-time
# chat flow (MQTT5-over-WebSocket, SigV4). The CRT layer binds native methods
# via JNI by fully-qualified class + method name, so its classes and native
# methods must NOT be renamed or removed in the consumer's release build, or
# the JNI lookups fail at runtime and the MQTT connect thread crashes.
# ---------------------------------------------------------------------------
-keep class software.amazon.awssdk.crt.** { *; }
-keep interface software.amazon.awssdk.crt.** { *; }
-keepnames class software.amazon.awssdk.crt.** { *; }
-keep class software.amazon.awssdk.iot.** { *; }
-keepnames class software.amazon.awssdk.iot.** { *; }
-dontwarn software.amazon.awssdk.crt.**
-dontwarn software.amazon.awssdk.iot.**

# Preserve native (JNI) method names on all CRT classes - they are bound from
# the aws-crt native library by name.
-keepclasseswithmembernames class software.amazon.awssdk.crt.** {
    native <methods>;
}



# ---------------------------------------------------------------------------
# Analytics
# ---------------------------------------------------------------------------
# NOTE: Mixpanel is no longer a hard dependency of the core SDK. Its
# ProGuard/R8 rules now live in the optional
# `com.netomi.chat:chat-widget-android-analytics` artifact's own
# consumer-rules.pro, so they only apply to apps that add that module.
#
# The Microsoft Cognitive Services Speech SDK (voice recognition) is likewise
# no longer a dependency of the core SDK — its rules now live in the optional
# `com.netomi.chat:chat-widget-android-voicestt` artifact's own
# consumer-rules.pro.

# ---------------------------------------------------------------------------
# Lottie animations (reflection on model classes)
# ---------------------------------------------------------------------------
-keep class com.airbnb.lottie.** { *; }
-dontwarn com.airbnb.lottie.**

# ============================================================================
# 4b. SDK INTERNAL PACKAGES THAT WRAP THIRD-PARTY LIBS
# ============================================================================
# These are kept in the SDK's own proguard-rules.pro, but that file is NOT
# propagated to consumer apps - only this consumer-rules.pro is. They interact
# with reflection-heavy libraries (AWS IoT, Mixpanel, Speech), so keep
# them intact to avoid runtime crashes when launching the chat ("Chat Now").
-keep class com.netomi.chat.awsiot.** { *; }
-keep class com.netomi.chat.VoiceEngine.** { *; }
-keep class com.netomi.chat.voiceRecognition.** { *; }
-keep class com.netomi.chat.utils.datadogLogger.** { *; }
-keep class com.netomi.chat.utils.analyticsManager.** { *; }


# ============================================================================
# 5. ANDROID COMPONENTS
# ============================================================================

# Keep SDK UI components
-keep public class com.netomi.chat.ui.view.** { *; }

# Keep SDK ViewModels
-keep class com.netomi.chat.ui.viewmodel.** { *; }

# Keep SDK Android components instantiated by the framework via class name.
# The manifest declares these activities/services, and Android reflects on the
# class name to instantiate them - obfuscation/removal causes ClassNotFound /
# crashes when the chat launches or the foreground service starts.
-keep class com.netomi.chat.utils.service.** { *; }
-keep class * extends android.app.Service { *; }
-keep class * extends android.content.BroadcastReceiver { *; }
-keep class * extends android.app.Activity { *; }
-keep class * extends androidx.fragment.app.Fragment { *; }
-keep class * extends android.content.ContentProvider { *; }

# ---------------------------------------------------------------------------
# WebView JavaScript bridges (used by chat forms / HTML messages).
# Methods annotated with @JavascriptInterface are invoked from JS by name, so
# they must NOT be renamed or removed. Classes: WebAppInterface ("AndroidBridge")
# in NCWChatAdapter and the anonymous bridge in NCWFormURLBottomSheet.
# ---------------------------------------------------------------------------
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# ---------------------------------------------------------------------------
# Enums - valueOf()/values() are resolved reflectively. proguard-android-optimize
# keeps these by default, but the SDK calls NCWInternalEnvironment.valueOf(...)
# and NCWInternalChatEventsTopic.valueOf(...) explicitly, so keep them safe.
# ---------------------------------------------------------------------------
-keepclassmembers enum com.netomi.chat.** {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# ---------------------------------------------------------------------------
# JNI - the native symbol Java_com_netomi_chat_NativeLib_stringFromJNI is bound
# by fully-qualified class name, so NativeLib must keep its name + native method.
# ---------------------------------------------------------------------------
-keep class com.netomi.chat.NativeLib { *; }


# ============================================================================
# 6. KOTLIN & COROUTINES
# ============================================================================

# Keep Kotlin metadata
-keep class kotlin.Metadata { *; }

# Keep Kotlin coroutines
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
-keepclassmembers class kotlinx.coroutines.** {
    volatile <fields>;
}

# Keep companion objects
-keepclassmembers class **$Companion {
    <fields>;
    <methods>;
}

# ============================================================================
# 7. NATIVE CODE (JNI)
# ============================================================================

-keepclasseswithmembernames class * {
    native <methods>;
}

# ============================================================================
# 8. PARCELABLE & SERIALIZABLE
# ============================================================================

-keepclassmembers class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# ============================================================================
# 9. OPTIMIZATION & DEBUGGING
# ============================================================================

# Preserve line numbers for debugging
-keepattributes SourceFile,LineNumberTable
-renamesourcefileattribute SourceFile

# Keep exception information
-keepattributes Exceptions,InnerClasses,EnclosingMethod

# ============================================================================
# END OF CONSUMER RULES
# ============================================================================
