/*
 * 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 org.dueattendant149.bookshelf.data.local.dto.AudioFileEntity

@Dao
interface AudioFileDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(file: AudioFileEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAll(files: List<AudioFileEntity>)

    @Query("SELECT * FROM audio_file WHERE bookId = :bookId ORDER BY `order`")
    suspend fun getByBookId(bookId: Int): List<AudioFileEntity>

    @Query("SELECT * FROM audio_file WHERE bookId = :bookId AND localPath IS NOT NULL ORDER BY `order`")
    suspend fun getCachedByBookId(bookId: Int): List<AudioFileEntity>

    @Update
    suspend fun update(file: AudioFileEntity)

    @Delete
    suspend fun delete(file: AudioFileEntity)

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