package com.example.triphelper.view.RouteScreens

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.triphelper.domain.Constants
import com.example.triphelper.model.Advice
import com.example.triphelper.model.Attractions
import com.example.triphelper.model.City
import com.example.triphelper.model.Country
import com.example.triphelper.model.Hotels
import com.example.triphelper.model.Metro
import com.example.triphelper.model.Organization_of_tours
import com.example.triphelper.model.Restaurants
import com.example.triphelper.model.Rules
import com.example.triphelper.model.Tours
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 AdviceViewModel @Inject constructor() : ViewModel() {
    private var _navigationTo = MutableStateFlow<String?>(null)
    val navigationTo = _navigationTo.asStateFlow()

    private val _advice: MutableStateFlow<List<Advice>> = MutableStateFlow(listOf())
    val advice: StateFlow<List<Advice>> = _advice.asStateFlow()

    fun GetAdvice(titleCatAdvice: String) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val columns = Columns.raw("""
                    id,
                    category_advice!inner(id,title),
                    title,
                    description
                """.trimIndent()
                )
                val responce = Constants.supabase.from("advices").select(
                    columns = columns
                ) {
                    filter {
                        eq("category_advice.title", titleCatAdvice)
                    }
                }.decodeList<Advice>()
                _advice.value = responce
                Log.i("orgTour", "${_advice.value}")
            }
        }
    }
}