package org.xtimms.etsudoku.core.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import org.xtimms.etsudoku.core.database.entity.SuggestionEntity
import org.xtimms.etsudoku.core.database.entity.SuggestionWithManga

@Dao
abstract class SuggestionDao {

    @Transaction
    @Query("SELECT * FROM suggestions ORDER BY relevance DESC")
    abstract fun observeAll(): Flow<List<SuggestionWithManga>>

    @Transaction
    @Query("SELECT * FROM suggestions ORDER BY relevance DESC LIMIT :limit")
    abstract fun observeAll(limit: Int): Flow<List<SuggestionWithManga>>

    @Transaction
    @Query("SELECT * FROM suggestions ORDER BY RANDOM() LIMIT 1")
    abstract suspend fun getRandom(): SuggestionWithManga?

    @Query("SELECT COUNT(*) FROM suggestions")
    abstract suspend fun count(): Int

    @Query("SELECT manga.title FROM suggestions LEFT JOIN manga ON suggestions.manga_id = manga.manga_id WHERE manga.title LIKE :query")
    abstract suspend fun getTitles(query: String): List<String>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    abstract suspend fun insert(entity: SuggestionEntity): Long

    @Update
    abstract suspend fun update(entity: SuggestionEntity): Int

    @Query("DELETE FROM suggestions")
    abstract suspend fun deleteAll()

    @Transaction
    open suspend fun upsert(entity: SuggestionEntity) {
        if (update(entity) == 0) {
            insert(entity)
        }
    }
}