package com.laseroptek.raman.data.source.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction

import com.laseroptek.raman.data.datasource.db.dao.BaseDao
import com.laseroptek.raman.data.source.db.model.SerialLog
import kotlinx.coroutines.flow.Flow

@Dao
abstract class SerialLogDao : BaseDao<SerialLog> {
    @Transaction
    @Query("SELECT * FROM serial_log ORDER BY ts DESC LIMIT :maxRecord")
    abstract fun selectSerialLog(maxRecord: Int) : Flow<List<SerialLog>>

    @Insert
    abstract suspend fun insertSerialLog(serialLog: SerialLog)

    @Query("""
        DELETE FROM serial_log
        WHERE id IN (
            SELECT id FROM serial_log
            ORDER BY ts ASC
            LIMIT (SELECT COUNT(*) FROM serial_log) - :limit
        )
    """)
    abstract suspend fun trimLogs(limit: Int)

}