package com.example.triphelper.view.MainScreen

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.triphelper.domain.Constants
import com.example.triphelper.model.Attractions
import dagger.hilt.android.lifecycle.HiltViewModel
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 MainScreenViewModel@Inject constructor(): ViewModel(){
    private var _navigationTo = MutableStateFlow<String?>(null)
    val navigationTo = _navigationTo.asStateFlow()

    private val _attractions: MutableStateFlow<List<Attractions>> = MutableStateFlow(listOf())
    val attractions: StateFlow<List<Attractions>> = _attractions.asStateFlow()

    private val _flagCountry = MutableStateFlow("")
    val flagCountry: StateFlow<String> = _flagCountry.asStateFlow()

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

    suspend fun GetAttractions(){
        viewModelScope.launch {
            withContext(Dispatchers.IO){
                val responce = Constants.supabase.from("attractions").select(
                    Columns.raw(
                        "id, "+
                        "city(id,country:country(id,title,flag),title), "+
                        "currency(id,country:country(id,title,flag),title), "+
                        "name, "+
                        "visiting, "+
                        "work_schedule, "+
                        "price_showing, "+
                        "photo, "+
                        "history, "+
                        "address, "+
                        "route, "+
                        "schedule_more, "+
                        "price, "+
                        "coordinates"
                    )
                ).decodeList<Attractions>()
                _attractions.value = responce
            }
        }
    }
}