package xyz.cottongin.radio247.data.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import xyz.cottongin.radio247.data.model.ConnectionSpan
import kotlinx.coroutines.flow.Flow

@Dao
interface ConnectionSpanDao {
    @Insert
    suspend fun insert(span: ConnectionSpan): Long

    @Query("UPDATE connection_spans SET endedAt = :endedAt WHERE id = :id")
    suspend fun updateEndedAt(id: Long, endedAt: Long)

    @Query("SELECT * FROM connection_spans WHERE sessionId = :sessionId ORDER BY startedAt DESC")
    fun getBySession(sessionId: Long): Flow<List<ConnectionSpan>>

    @Query("SELECT * FROM connection_spans WHERE endedAt IS NULL LIMIT 1")
    suspend fun getActiveSpan(): ConnectionSpan?
}
