package com.tsmobile.app.data

import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.floatPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

private val Context.voiceDataStore by preferencesDataStore(name = "voice_settings")

class VoicePreferences(private val context: Context) {

    companion object {
        private val OUTPUT_VOLUME_KEY = floatPreferencesKey("output_volume")
    }

    val outputVolume: Flow<Float> = context.voiceDataStore.data.map { prefs ->
        (prefs[OUTPUT_VOLUME_KEY] ?: 1.0f).coerceIn(0f, 1f)
    }

    suspend fun setOutputVolume(volume: Float) {
        context.voiceDataStore.edit { prefs ->
            prefs[OUTPUT_VOLUME_KEY] = volume.coerceIn(0f, 1f)
        }
    }
}
