package com.design.mischiefgo.entity

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface DuplicateDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(duplicate: Duplicate): Long
    @Delete
    suspend fun delete(duplicate: Duplicate): Int
    @Query("SELECT * FROM duplicate")
    suspend fun selectAll(): List<Duplicate>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertAll(infos: List<Duplicate>): List<Long>

    //使用索引优化查询
    @Query("SELECT * FROM duplicate WHERE isLike = 1 ORDER BY id ASC")
    fun getAllLike(): List<Duplicate>

    @Query("SELECT * FROM duplicate WHERE isCustomization = 1 ORDER BY id ASC")
    fun getAllCustomization(): List<Duplicate>

    // 高效的随机查询（使用SQLite的随机函数）
    @Query("""
        SELECT * FROM duplicate 
        WHERE isCustomization = 0 
        ORDER BY RANDOM() 
        LIMIT 11
    """)
    suspend fun getMoreEfficient(): List<Duplicate>

    // 使用EXISTS进行存在性检查（更高效）
    @Query("SELECT EXISTS(SELECT 1 FROM duplicate WHERE name = :name AND isLike = 1)")
    suspend fun checkLikeExists(name: String): Boolean

    @Query("SELECT EXISTS(SELECT 1 FROM duplicate WHERE name = :name)")
    suspend fun checkNameExists(name: String): Boolean


    @Query("UPDATE duplicate SET isLike = :isLike WHERE name = :name")
    suspend fun updateLikeStatus(name: String, isLike: Boolean): Int


    @Query("UPDATE duplicate SET isLike = 0 WHERE name = :name")
    suspend fun removeLike(name: String)
}