package com.radiola.data.local.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.radiola.data.local.entity.TrackHistoryEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface TrackHistoryDao {

    @Query("SELECT * FROM track_history ORDER BY timestamp DESC LIMIT 200")
    fun getAll(): Flow<List<TrackHistoryEntity>>

    @Insert
    suspend fun insert(track: TrackHistoryEntity)

    @Query("DELETE FROM track_history WHERE id = :id")
    suspend fun deleteById(id: Int)

    @Query("DELETE FROM track_history WHERE id NOT IN (SELECT id FROM track_history ORDER BY timestamp DESC LIMIT 200)")
    suspend fun cleanupOld()

    @Query("SELECT * FROM track_history WHERE artist LIKE '%' || :query || '%' OR song LIKE '%' || :query || '%' ORDER BY timestamp DESC")
    suspend fun search(query: String): List<TrackHistoryEntity>
}
