package com.syncflow.ui.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.syncflow.data.db.entities.CloudAccountEntity
import com.syncflow.data.preferences.AppPreferences
import com.syncflow.data.repository.AccountRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SettingsViewModel @Inject constructor(
    private val accountRepository: AccountRepository,
    private val appPreferences: AppPreferences,
) : ViewModel() {

    val accounts = accountRepository.observeAll()
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

    val biometricEnabled = appPreferences.biometricLockEnabled
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)

    fun setBiometricLock(enabled: Boolean) {
        viewModelScope.launch { appPreferences.setBiometricLock(enabled) }
    }

    fun removeAccount(account: CloudAccountEntity) {
        viewModelScope.launch { accountRepository.delete(account) }
    }
}
