package com.fabledsword.minstrel.cache.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.fabledsword.minstrel.cache.db.entities.CachedHomeIndexEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface CachedHomeIndexDao {
    @Query(
        "SELECT * FROM cached_home_index " +
            "WHERE section = :section ORDER BY position ASC",
    )
    fun observeBySection(section: String): Flow<List<CachedHomeIndexEntity>>

    @Query(
        "SELECT * FROM cached_home_index " +
            "WHERE section = :section ORDER BY position ASC",
    )
    suspend fun getBySection(section: String): List<CachedHomeIndexEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun upsertAll(rows: List<CachedHomeIndexEntity>)

    /** Replace-all pattern; sync wipes a section then re-inserts. */
    @Query("DELETE FROM cached_home_index WHERE section = :section")
    suspend fun deleteBySection(section: String)

    @Query("DELETE FROM cached_home_index")
    suspend fun clear()
}
