package com.example.pmulabs.viewModels

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.pmulabs.room.models.User
import com.example.pmulabs.room.repository.UserRepository
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

class CurrentUserViewModel(private val userRepository: UserRepository) : ViewModel(){
    val argument = mutableStateOf<String?>(null)
    private val userid = mutableStateOf<Int?>(null)
    var user by mutableStateOf<User?>(null)

    fun setArgument(arg: String) {
        argument.value = arg
        userid.value = arg.toInt()
        viewModelScope.launch {
            user = userRepository.getUserById(userid.value)
                .filterNotNull()
                .first()
        }
    }
}