package com.example.shawarma.data.repos

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class ProductRepository @Inject constructor(
    private val productDao: ProductDao,
    private val orderProductDao: OrderProductDao
) {
    suspend fun insert(product: ProductModel) {
        return productDao.insert(product)
    }
    suspend fun update(product: ProductModel) {
        return productDao.update(product)
    }
    suspend fun delete(product: ProductModel) {
        orderProductDao.deleteByProductId(product.id!!)
        return productDao.delete(product)
    }
    fun getById(id: Int): Flow<ProductModel> {
        return productDao.getById(id)
    }
    fun getAllProductsPaged(): Flow<PagingData<ProductModel>> = Pager(
        config = PagingConfig(
            pageSize = 6,
            enablePlaceholders = false
        ),
        pagingSourceFactory = productDao::getPaged
    ).flow

    fun getAllDiscountsPaged(): Flow<PagingData<ProductModel>> = Pager(
        config = PagingConfig(
            pageSize = 6,
            enablePlaceholders = false
        ),
        pagingSourceFactory = productDao::getPagedDiscounts
    ).flow

    fun getAllItemsPaged(): Flow<PagingData<ProductModel>> = Pager(
        config = PagingConfig(
            pageSize = 6,
            enablePlaceholders = false
        ),
        pagingSourceFactory = productDao::getPagedItems
    ).flow

}