package de.tsschulz.tt_tagebuch.app.viewmodel

import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.screenModelScope
import de.tsschulz.tt_tagebuch.shared.models.Member
import de.tsschulz.tt_tagebuch.shared.repository.DiaryRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class MemberScreenModel(private val diaryRepository: DiaryRepository) : ScreenModel {
    // Note: Reusing DiaryRepository as it likely handles club-related data, 
    // but in a larger app we would have a MemberRepository.
    
    private val _state = MutableStateFlow<MemberState>(MemberState.Loading)
    val state = _state.asStateFlow()

    fun loadMembers(clubId: Int) {
        screenModelScope.launch {
            try {
                val members = diaryRepository.getMembers(clubId)
                _state.value = MemberState.Success(members)
            } catch (e: Exception) {
                _state.value = MemberState.Error(e.message ?: "Failed to load members")
            }
        }
    }
}

sealed class MemberState {
    object Loading : MemberState()
    data class Success(val members: List<Member>) : MemberState()
    data class Error(val message: String) : MemberState()
}
