package com.rugvedsarika.shoplist.data.local.dao

import androidx.lifecycle.LiveData
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 com.rugvedsarika.shoplist.data.local.entities.ShoppingItem

@Dao
interface ShoppingItemDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertShoppingItem(item: ShoppingItem)

    @Update
    suspend fun updateShoppingItem(item: ShoppingItem)

    @Delete
    suspend fun deleteShoppingItem(item: ShoppingItem)

    @Query("DELETE FROM shopping_items WHERE listId = :listId")
    suspend fun deleteItemsByListId(listId: Int)

    @Query("SELECT * FROM shopping_items WHERE listId = :listId ORDER BY isPurchased ASC")
    fun getItemsByListId(listId: Int): LiveData<List<ShoppingItem>>

    @Query("SELECT * FROM shopping_items WHERE listId = :listId AND name LIKE '%' || :query || '%' ORDER BY isPurchased ASC")
    fun searchItems(listId: Int, query: String): LiveData<List<ShoppingItem>>
    
    @Query("DELETE FROM shopping_items")
    suspend fun deleteAllShoppingItems()
}
