package com.example.triphelper.view.ProfileScreen


import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.triphelper.domain.Constants
import com.example.triphelper.model.Attractions
import com.example.triphelper.model.Country
import com.example.triphelper.model.Users
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.postgrest.from
import io.github.jan.supabase.postgrest.query.Columns
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel@Inject constructor() : ViewModel() {
    private var _navigationTo = MutableStateFlow<String?>(null) //Переменные для навигации страницы из авторизации на главный экран
    val navigationTo = _navigationTo.asStateFlow()

    private val _users: MutableStateFlow<List<Users>> = MutableStateFlow(listOf())
    val users: StateFlow<List<Users>> = _users.asStateFlow()

    private val _titleCountry = MutableStateFlow("")
    val titleCountry: StateFlow<String> = _titleCountry.asStateFlow()

    private val _userEmail = MutableStateFlow<String?>(null)
    val userEmail: StateFlow<String?> = _userEmail.asStateFlow()

    fun GetUser(){
        viewModelScope.launch {
            try {
                withContext(Dispatchers.IO){
                    val idUser = Constants.supabase.auth.currentUserOrNull()?.id.toString()
                    val responce = Constants.supabase.from("users").select(){
                        filter {
                            eq("id", idUser)
                        }
                    }.decodeList<Users>()
                    _users.value = responce
                    val authUser = Constants.supabase.auth.currentUserOrNull()
                    if (authUser != null) {
                        _userEmail.value = authUser.email
                    }
                    Log.i("user", "${_users.value}")
                }
            }catch (e:Exception){
                Log.e("GetUser", "Error retrieving user data", e)
            }
        }
    }
    fun getTitleCountry(idCountry: Int?) {
        viewModelScope.launch {
            try {
                val response = Constants.supabase
                    .from("country")
                    .select {
                        filter {
                            if (idCountry != null) {
                                eq("id", idCountry)
                            }
                        }
                    }
                    .decodeSingleOrNull<Country>()
                _titleCountry.value = response?.title.toString()
                Log.i("id", _titleCountry.value)

            } catch (e: Exception) {
                Log.e("getCountry", "Error retrieving country data", e)
            }
        }
    }
}