package com.example.tasks.viewModels

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.tasks.components.Constants
import com.example.tasks.model.User
import com.example.tasks.model.UserState
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.launch

class DatabaseViewModel : ViewModel() {
    private val _userState = mutableStateOf<UserState>(UserState.Loading)
    val userState: State<UserState> = _userState
    private val _users = mutableStateOf(listOf<User>())
    val users: State<List<User>> = _users
    fun profileData() {
        viewModelScope.launch {
            try {
                _userState.value = UserState.Loading
                 _users.value = Constants.supabase.from("Users").select {
                    filter {
                        User::UID eq Constants.supabase.auth.currentUserOrNull()!!.id
                    }
                }.decodeList<User>()
                Log.d("my_tag","ГУУУУД")
                _userState.value = UserState.Success("Профиль найден")

            } catch (e: Exception) {
                _userState.value = UserState.Error("Профиль не найден")
                Log.d("mu_tag",e.message!!)
            }
        }
    }
}