package com.laseroptek.raman.repository

import com.laseroptek.raman.data.source.db.DatabaseService
import com.laseroptek.raman.data.source.db.model.SerialLog
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@ViewModelScoped
class DatabaseRepository @Inject constructor(
    private val databaseService: DatabaseService
) {
    suspend fun selectSerialLog(maxRecord: Int): Flow<List<SerialLog>> {
        return databaseService.selectSerialLog(maxRecord)
    }

    suspend fun insertSerialLog(serialLog: SerialLog) {
        withContext(Dispatchers.IO) {
            databaseService.insertSerialLog(serialLog)
        }
    }

    suspend fun trimLogs(limit: Int) {
        withContext(Dispatchers.IO) {
            databaseService.trimLogs(limit)
        }
    }
}