package com.example.lectionsupabase.view.mainActivity

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.privacysandbox.tools.core.model.Constant
import com.example.lectionsupabase.domain.utils.Constants
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.gotrue.providers.builtin.Email
import io.github.jan.supabase.gotrue.providers.builtin.OTP
import kotlinx.coroutines.launch

class MainViewModel : ViewModel() {
    private val _loginResult = MutableLiveData<Boolean>()
    val loginResult: LiveData<Boolean> = _loginResult

    fun onSignInEmailPassword(emailUser: String, passwordUser: String, navController: NavController) {
        viewModelScope.launch {
            try {
                val user = Constants.supabase.auth.signInWith(Email) {
                    email = emailUser
                    password = passwordUser
                }
                println(user.toString())
                println(Constants.supabase.auth.currentUserOrNull()!!.id)
                println("Success")
                navController.navigate("slide1")
            } catch (e: Exception) {
                println("Error")
                println(e.message.toString())
            }
        }
    }

    fun onSignUpEmail(emailUser: String, passwordUser: String) {
        viewModelScope.launch {
            try {
                val user = Constants.supabase.auth.signUpWith(Email) {
                    email = emailUser
                    password = passwordUser
                }
                println("Success: ${user.toString()}")
                _loginResult.postValue(true) // Обновляем результат
            } catch (e: Exception) {
                println("Error: ${e.message}")
                _loginResult.postValue(false) // В случае ошибки
            }
        }
    }
}

