package com.chillbrew.datingapp.viewmodel import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.chillbrew.datingapp.models.PilotRegistration import com.chillbrew.datingapp.models.VendorLead import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.Query import com.google.firebase.firestore.toObjects import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch import kotlinx.coroutines.tasks.await class ChillbrewAdminViewModel : ViewModel() { // 1. Initialize Firebase connection private val db = FirebaseFirestore.getInstance() // 🔥 CRITICAL FIX: Matching the exact Firestore production document ID private val appId = "chill-brew-557ac" // 2. StateFlow to hold our incoming data for the UI to observe private val _vendorLeads = MutableStateFlow>(emptyList()) val vendorLeads: StateFlow> = _vendorLeads.asStateFlow() private val _pilotRegistrations = MutableStateFlow>(emptyList()) val pilotRegistrations: StateFlow> = _pilotRegistrations.asStateFlow() init { // Automatically fetch data when this ViewModel starts fetchVendorLeads() fetchPilotRegistrations() } // 3. Fetch Coffee Shop Partnerships (B2B Leads) fun fetchVendorLeads() { viewModelScope.launch { try { val result = db.collection("artifacts").document(appId) .collection("public").document("data") .collection("vendor_leads") .orderBy("createdAt", Query.Direction.DESCENDING) // Newest first .get() .await() val leads = result.toObjects() _vendorLeads.value = leads Log.d("ChillbrewAdmin", "Successfully fetched ${leads.size} vendor leads.") } catch (e: Exception) { Log.e("ChillbrewAdmin", "Error fetching vendor leads", e) } } } // 4. Fetch User Waitlist (B2C Leads) fun fetchPilotRegistrations() { viewModelScope.launch { try { val result = db.collection("artifacts").document(appId) .collection("public").document("data") .collection("registrations") .orderBy("timestamp", Query.Direction.DESCENDING) // Newest first .get() .await() val registrations = result.toObjects() _pilotRegistrations.value = registrations Log.d("ChillbrewAdmin", "Successfully fetched ${registrations.size} pilot registrations.") } catch (e: Exception) { Log.e("ChillbrewAdmin", "Error fetching pilot registrations", e) } } } }