package com.all.pdfreader.pro.app.room.dao

import androidx.room.*
import com.all.pdfreader.pro.app.room.entity.NoteEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface NoteDao {
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(note: NoteEntity): Long
    
    @Update
    suspend fun update(note: NoteEntity)
    
    @Delete
    suspend fun delete(note: NoteEntity)
    
    @Query("SELECT * FROM notes WHERE id = :noteId")
    suspend fun getById(noteId: Long): NoteEntity?
    
    @Query("SELECT * FROM notes WHERE filePath = :filePath ORDER BY pageNumber ASC, createTime ASC")
    fun getNotesByPdf(filePath: String): Flow<List<NoteEntity>>
    
    @Query("SELECT * FROM notes WHERE filePath = :filePath AND pageNumber = :pageNumber")
    suspend fun getNotesByPage(filePath: String, pageNumber: Int): List<NoteEntity>
    
    @Query("SELECT * FROM notes WHERE filePath = :filePath AND noteType = :noteType")
    fun getNotesByType(filePath: String, noteType: String): Flow<List<NoteEntity>>
    
    @Query("SELECT COUNT(*) FROM notes WHERE filePath = :filePath")
    suspend fun getNoteCount(filePath: String): Int
    
    @Query("DELETE FROM notes WHERE filePath = :filePath")
    suspend fun deleteAllByPdf(filePath: String)

    @Query("DELETE FROM notes WHERE filePath IN (:filePaths)")
    suspend fun deleteAllByPdfs(filePaths: List<String>)

    @Query("DELETE FROM notes WHERE filePath = :filePath AND pageNumber = :pageNumber")
    suspend fun deleteByPage(filePath: String, pageNumber: Int)
}