package com.example.development.mainActivity.components

import android.util.Log
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.navigation.NavHostController
import com.example.development.domain.utils.Constants
import com.example.development.mainActivity.MainViewModel
import com.example.development.model.User
import io.github.jan.supabase.gotrue.auth
import io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun Loading(navController: NavHostController, viewModel: MainViewModel){
    var user by remember { mutableStateOf<List<User>>(listOf()) }
    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) {
            user = Constants.supabase.from("users").select{
                filter{
                    User::UID eq Constants.supabase.auth.currentUserOrNull()!!.id
                }
            }.decodeList<User>()
            user.forEach { it ->
                it.UID?.let { it -> Log.d("U", it) }
            }
        }
    }
    CircularProgressIndicator()
    if (user.isNotEmpty()) {
        navController.navigate("UserProfileForUnauthorizedUser")
    }
    else {
        navController.navigate("UserProfile")
    }
}