/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.data.local.room

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow
import org.dueattendant149.bookshelf.data.local.dto.CachedFileEntity

@Dao
interface CachedFileDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(file: CachedFileEntity): Long

    @Query("SELECT * FROM cached_file WHERE bookId = :bookId AND type = :type")
    suspend fun getByBookIdAndType(bookId: Int, type: String): List<CachedFileEntity>

    @Query("SELECT * FROM cached_file WHERE bookId = :bookId AND type = :type LIMIT 1")
    suspend fun getByBookIdAndTypeSingle(bookId: Int, type: String): CachedFileEntity?

    @Query("SELECT * FROM cached_file WHERE bookId = :bookId")
    suspend fun getByBookId(bookId: Int): List<CachedFileEntity>

    @Query("SELECT * FROM cached_file WHERE bookId = :bookId")
    fun observeByBookId(bookId: Int): Flow<List<CachedFileEntity>>

    @Query("SELECT * FROM cached_file WHERE status = :status")
    suspend fun getByStatus(status: String): List<CachedFileEntity>

    @Update
    suspend fun update(file: CachedFileEntity)

    @Delete
    suspend fun delete(file: CachedFileEntity)

    @Query("DELETE FROM cached_file WHERE bookId = :bookId")
    suspend fun deleteByBookId(bookId: Int)
}
