package com.example.myapplication.database.entities.dao

import androidx.paging.PagingSource
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.entities.model.Rent
import com.example.myapplication.database.entities.model.ItemFromRent

@Dao
interface RentDao {
    @Query("select * from rents where user_id = :userId")
    fun getAll(userId: Int?): PagingSource<Int, Rent>

    @Query(
        "SELECT o.*, s.*, os.count, os.frozen_weight " +
                "FROM rents AS o " +
                "JOIN rents_items AS os ON os.rent_id = o.uid " +
                "JOIN items AS s ON s.uid = os.item_id " +
                "WHERE o.uid = :rentId"
    )
    fun getByUid(rentId: Int?): List<ItemFromRent>

    @Insert
    suspend fun insert(vararg rent: Rent): List<Long>

    @Update
    suspend fun update(rent: Rent)

    @Delete
    suspend fun delete(rent: Rent)

    @Query("DELETE FROM rents")
    suspend fun deleteAll()
}