package com.example.triphelper.view.MainScreen

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.test.espresso.util.filter
import com.example.triphelper.domain.Constants
import com.example.triphelper.model.Attractions
import com.example.triphelper.model.Country
import com.example.triphelper.model.Rules
import com.example.triphelper.view.navigation.NavigationRoutes
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.jan.supabase.postgrest.from
import io.github.jan.supabase.postgrest.query.Columns
import io.github.jan.supabase.postgrest.query.Order
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
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 LawScreenViewModel@Inject constructor(): ViewModel(){
    private var _navigationTo = MutableStateFlow<String?>(null)
    val navigationTo = _navigationTo.asStateFlow()

    private val _ruleses: MutableStateFlow<List<Rules>> = MutableStateFlow(listOf())
    val ruleses: StateFlow<List<Rules>> = _ruleses.asStateFlow()

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

    private var _country: MutableStateFlow<List<Country>> = MutableStateFlow(listOf())
    val country: StateFlow<List<Country>> = _country.asStateFlow()

    private val _idCountry = MutableStateFlow<Int?>(null)
    val idCountry: StateFlow<Int?> = _idCountry.asStateFlow()

    fun getCountry(){
        viewModelScope.launch {
            try {
                withContext(Dispatchers.IO) {
                    val response = Constants.supabase
                        .from("country")
                        .select(){
                            order(column = "title", order = Order.ASCENDING)
                        }
                        .decodeList<Country>()
                    _country.value = response
                }
            } catch (e: Exception) {
                Log.e("getCountry", "Error retrieving country data", e)
            }
        }
    }
    suspend fun getIdCountry(titleCountry: String) {
        try {
            val response = Constants.supabase
                .from("country")
                .select {
                    filter {
                        eq("title", titleCountry)
                    }
                }
                .decodeSingleOrNull<Country>()
            _idCountry.value = response?.id
            _titleCountry.value = response?.title.toString()
            Log.i("id", "${_idCountry.value}")
        } catch (e: Exception) {
            Log.e("getCountry", "Error retrieving country data", e)
        }
    }
    fun GetRules(title:String){
        viewModelScope.launch {
           try{
               getIdCountry(title)
                val responce = Constants.supabase.from("rules").select(
                    Columns.raw(
                        "id, "+
                            "country(id,title,flag), "+
                            "laws, "+
                            "rules_of_culture, "+
                            "gestures"
                    )
                ){
                    filter {
                        _idCountry.value?.let { eq("country", it) }
                    }
                }.decodeList<Rules>()
                _ruleses.value = responce
               Log.i("Rules","${_idCountry.value}")
            }
           catch (e: Exception){
               Log.e("Error","error")
           }
        }
    }
}