package com.webtoonviewer.db

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

@Dao
interface BookDao {

    @Query("SELECT * FROM books")
    suspend fun getAllBooks(): List<Book>

    @Query("SELECT title FROM books")
    suspend fun getAllTitles(): List<String>

    @Query("SELECT * FROM books WHERE title = :title")
    suspend fun getBookByTitle(title: String): Book?

    @Query("SELECT * FROM books WHERE id = :bookId")
    suspend fun getBookById(bookId: Int): Book?

    @Query("UPDATE books SET title = :title WHERE id = :bookId")
    suspend fun updateTitleById(bookId: Int, title: String)

    @Query("UPDATE books SET author = :author, tag = :tag, description = :description, img = :img WHERE id = :bookId")
    suspend fun updateInformationById(bookId: Int, author: String, tag: String, description: String, img: String)

    @Query("UPDATE books SET tag = :tag WHERE id = :bookId")
    suspend fun updateTagById(bookId: Int, tag: String)

    @Query("UPDATE books SET description = :description WHERE id = :bookId")
    suspend fun updateDescriptionById(bookId: Int, description: String)

    @Query("UPDATE books SET chapters = :chapters, chapterSum = :chapterSum WHERE id = :bookId")
    suspend fun updateChaptersById(bookId: Int, chapters: List<String>, chapterSum: Int)

    @Query("UPDATE books SET bookmarkChapter = :bookmarkChapter, bookmarkPosition = :bookmarkPosition, bookmarkTopOffset = :bookmarkTopOffset WHERE id = :bookId")
    suspend fun updateBookmarkById(bookId: Int, bookmarkChapter: String, bookmarkPosition: Int, bookmarkTopOffset: Int)

    @Query("UPDATE books SET isLocal = :isLocal WHERE id = :bookId")
    suspend fun updateIsLocalById(bookId: Int, isLocal: Int)

    @Query("DELETE FROM books WHERE id = :bookId")
    suspend fun deleteBookById(bookId: Int)
    // 添加新书籍
    @Insert
    suspend fun addBook(book: Book)

    @Query("UPDATE sqlite_sequence SET seq = :startId WHERE name = 'books'")
    suspend fun setAutoIncrement(startId: Int)

//
//    @Query("SELECT * FROM sqlite_sequence WHERE name = 'books'")
//    suspend fun getAutoIncrement()
}