// TourViewModel.kt
package com.placeholder.sherpai2.ui.tour

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.placeholder.sherpai2.domain.repository.ImageRepository
import com.placeholder.sherpai2.data.local.model.ImageWithEverything
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class TourViewModel @Inject constructor(
    private val imageRepository: ImageRepository
) : ViewModel() {

    // Expose recent images as StateFlow
    private val _recentImages = MutableStateFlow<List<ImageWithEverything>>(emptyList())
    val recentImages: StateFlow<List<ImageWithEverything>> = _recentImages.asStateFlow()

    init {
        loadRecentImages()
    }

    private fun loadRecentImages(limit: Int = 100) {
        viewModelScope.launch {
            imageRepository.getRecentImages(limit)
                .catch { e ->
                    println("TourViewModel: error fetching images: $e")
                    _recentImages.value = emptyList()
                }
                .collect { images ->
                    println("TourViewModel: fetched ${images.size} images")
                    _recentImages.value = images
                }
        }
    }
}
