package com.syncflow.data.db

import androidx.room.*
import com.syncflow.data.db.entities.SyncEventEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface SyncEventDao {
    @Query("SELECT * FROM sync_events WHERE syncPairId = :pairId ORDER BY timestamp DESC LIMIT :limit")
    fun observeRecent(pairId: Long, limit: Int = 200): Flow<List<SyncEventEntity>>

    @Query("SELECT * FROM sync_events ORDER BY timestamp DESC LIMIT :limit")
    fun observeAll(limit: Int = 500): Flow<List<SyncEventEntity>>

    @Insert
    suspend fun insert(entity: SyncEventEntity): Long

    @Query("DELETE FROM sync_events WHERE syncPairId = :pairId AND timestamp < :olderThan")
    suspend fun pruneOld(pairId: Long, olderThan: Long)

    @Query("SELECT SUM(bytesTransferred) FROM sync_events WHERE syncPairId = :pairId AND timestamp >= :since")
    suspend fun totalBytesTransferred(pairId: Long, since: Long): Long?
}
