package com.example.development.mainActivity

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.development.domain.utils.Constants
import com.example.development.model.User
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 io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.launch

class MainViewModel():ViewModel() {
    private val _users = mutableStateOf(listOf<User>())
    val users: State<List<User>> = _users

    fun onSignInEmailCode(emailUser: String) {
        viewModelScope.launch {
            try {
                Constants.supabase.auth.signInWith(OTP) {
                    email = emailUser
                    createUser = false
                }
            } catch (e: Exception) {
                println(e.message.toString())
            }
        }
    }

    fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
        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")
                FindUser()
            } catch (e: Exception) {
                println("Error")
                println(e.message.toString())
            }
        }
    }

    fun onSignUpEmail(emailUser: String, passwordUser: String) {
        viewModelScope.launch {
            try{
                var  user =  Constants.supabase.auth.signUpWith(Email) {
                    email = emailUser
                    password = passwordUser
                }
                println(user.toString())
                println(Constants.supabase.auth.currentUserOrNull()!!.id)
                println("Success")
            }
            catch (e: Exception) {
                println("Error")
                println(e.message.toString())
            }

        }
    }

    fun FindUser(){
        viewModelScope.launch{
            try{
                _users.value = Constants.supabase.from("users").select{
                    filter{
                        User::UID eq Constants.supabase.auth.currentUserOrNull()!!.id
                    }
                }.decodeList<User>()
                println("Пользователь найден")
            }
            catch (e:Exception){
                println("Пользователь не найден")
                println(e.message.toString())
            }
        }
    }

    fun CreateUser(firstName: String,
                   secondName: String,
                   email: String,
                   phoneNumber: Int
                   )
    {
        viewModelScope.launch {
            try{
                Constants.supabase.from("users").insert(
                    User(
                        role = 1,
                        firstName = firstName,
                        secondName = secondName,
                        email = email,
                        phoneNumber = phoneNumber,
                        image = "",
                        UID = Constants.supabase.auth.currentUserOrNull()!!.id
                    )
                )
                println("Success")
            }
            catch (e: Exception)
            {
                println("Error")
                println(e.message.toString())
            }
        }
    }

    fun ChangePersonalData(firstName: String,
                           secondName: String,
                           email: String,
                           phoneNumber: Int)
    {
        viewModelScope.launch {
            try{
                Constants.supabase.from("users").update(
                    User(
                        role = 1,
                        firstName = firstName,
                        secondName = secondName,
                        email = email,
                        phoneNumber = phoneNumber,
                        image = "",
                        UID = Constants.supabase.auth.currentUserOrNull()!!.id
                    )
                )
                println("Success")
            }
            catch (e: Exception)
            {
                println("Error")
                println(e.message.toString())
            }
        }
    }
}