package com.radiola.ui.recordings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.radiola.domain.model.Recording
import com.radiola.domain.repository.RecordingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class RecordingsViewModel @Inject constructor(
    private val recordingRepository: RecordingRepository
) : ViewModel() {

    val recordings: StateFlow<List<Recording>> = recordingRepository.getRecordings()
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())

    val isRecording: StateFlow<Boolean> = recordingRepository.isRecording

    fun deleteRecording(id: Long) {
        viewModelScope.launch {
            recordingRepository.deleteRecording(id)
        }
    }
}
