package app.closer.notifications

import javax.inject.Inject
import javax.inject.Singleton

/**
 * Tracks which discussion thread (by questionId) the user is currently viewing, so an incoming
 * "your partner sent a message" notification can be suppressed while they're already reading that
 * conversation live. Set on entering the thread screen, cleared on leaving.
 *
 * Foreground-only by nature: when the app is backgrounded, FCM auto-displays the system-tray
 * notification and this monitor isn't consulted — which is the desired behaviour.
 */
@Singleton
class ActiveThreadMonitor @Inject constructor(
    private val messageBubbleController: MessageBubbleController
) {
    @Volatile
    var activeConversationId: String? = null
        private set

    fun enter(conversationId: String) {
        if (conversationId.isNotBlank()) {
            activeConversationId = conversationId
            // Opening the conversation counts as reading it — clear any chat bubble for it.
            messageBubbleController.dismissFor(conversationId)
        }
    }

    fun leave(conversationId: String) {
        if (activeConversationId == conversationId) activeConversationId = null
    }
}
