package com.jishisongfu.duobibi.data.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface ClusterDao {

    @Insert
    suspend fun insert(c: Cluster): Long

    @Query("UPDATE cluster SET title = :title WHERE id = :id")
    suspend fun rename(id: Long, title: String)

    @Query("SELECT * FROM cluster ORDER BY createdAt DESC")
    suspend fun getAll(): List<Cluster>

    @Query("SELECT * FROM cluster ORDER BY createdAt DESC")
    fun observeAll(): Flow<List<Cluster>>

    @Query("SELECT * FROM cluster WHERE id = :id")
    suspend fun byId(id: Long): Cluster?

    @Query("DELETE FROM cluster WHERE id = :id")
    suspend fun delete(id: Long)

    @Query("DELETE FROM cluster")
    suspend fun deleteAll()

    /** 清除"示例数据"占审用条目。仅删 isDemo=1 的,真实用户数据不受影响。 */
    @Query("DELETE FROM cluster WHERE isDemo = 1")
    suspend fun deleteDemo()

    /** "用户已有真实数据吗?"判定。给 DemoDataSeeder 防污染老用户用。 */
    @Query("SELECT COUNT(*) FROM cluster WHERE isDemo = 0")
    suspend fun countReal(): Int
}
