package com.example.myapplication.database.room.viewmodel.rent

import androidx.lifecycle.ViewModel
import androidx.paging.PagingData
import com.example.myapplication.database.room.model.rent.Rent
import com.example.myapplication.database.room.repository.online.RentRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flatMapLatest

class RentListViewModel(
    private val rentRepository: RentRepository
) : ViewModel() {
    private val _userId = MutableStateFlow<Int?>(null)
    val userId: StateFlow<Int?> get() = _userId

    val rentListUiState: Flow<PagingData<Rent>> = userId.flatMapLatest { userId ->
        rentRepository.getAllRents(userId)
    }

    suspend fun deleteRent(rent: Rent) {
        rentRepository.deleteRent(rent)
    }

    fun setUserId(userId: Int) {
        _userId.value = userId
    }
}

data class RentListUiState(val rentList: List<Rent> = listOf())