package com.example.livingai.pages.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.livingai.domain.model.SettingsData
import com.example.livingai.domain.usecases.AppDataUseCases
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class SettingsViewModel(
    private val appDataUseCases: AppDataUseCases
) : ViewModel() {

    private val _settings = MutableStateFlow(SettingsData("en", false))
    val settings = _settings.asStateFlow()

    init {
        getSettings()
    }

    private fun getSettings() {
        appDataUseCases.getSettings().onEach {
            _settings.value = it
        }.launchIn(viewModelScope)
    }

    fun saveSettings(settings: SettingsData) {
        viewModelScope.launch {
            appDataUseCases.saveSettings(settings)
        }
    }
}