package pt.whatsms.gateway.modules.incoming.repositories

import androidx.lifecycle.LiveData
import androidx.lifecycle.distinctUntilChanged
import pt.whatsms.gateway.modules.incoming.db.IncomingMessage
import pt.whatsms.gateway.modules.incoming.db.IncomingMessageTotals
import pt.whatsms.gateway.modules.incoming.db.IncomingMessageType
import pt.whatsms.gateway.modules.incoming.db.IncomingMessagesDao

class IncomingMessagesRepository(private val dao: IncomingMessagesDao) {
    fun selectLast(limit: Int): LiveData<List<IncomingMessage>> =
        dao.selectLast(limit).distinctUntilChanged()

    suspend fun count(type: IncomingMessageType?, from: Long, to: Long): Int =
        dao.count(type, from, to)

    suspend fun select(
        type: IncomingMessageType?,
        from: Long,
        to: Long,
        limit: Int,
        offset: Int
    ): List<IncomingMessage> =
        dao.select(type, from, to, limit, offset)

    fun selectById(id: String): IncomingMessage? = dao.selectById(id)

    val totals: LiveData<IncomingMessageTotals> = dao.getStats().distinctUntilChanged()

    fun insert(message: IncomingMessage) = dao.insert(message)
}
