package com.example.myapplication.database.room.dao.item

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.myapplication.database.room.model.item.Item

@Dao
interface ItemDao {
    @Query("select * from items where items.uid = :uid")
    suspend fun getByUid(uid: Int): Item

    @Insert
    suspend fun insert(vararg item: Item)

    @Update
    suspend fun update(item: Item)

    @Delete
    suspend fun delete(item: Item)

    @Query("DELETE FROM items")
    suspend fun deleteAll()

    @Query(
        "SELECT s.max_count-IFNULL(SUM(os.count), 0) as available_count " +
                "FROM items AS s " +
                "LEFT JOIN rents_items AS os ON os.item_id = s.uid " +
                "WHERE s.uid = :itemId " +
                "GROUP BY s.uid"
    )
    suspend fun getAvailableCountOfItem(itemId: Int): Int
}