package com.example.viewmodel import android.app.Application import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import com.example.data.model.AdminSettings import com.example.data.model.AppNotification import com.example.data.model.CustomerUser import com.example.data.model.DeliveryRider import com.example.data.model.FoodItem import com.example.data.model.Order import com.example.data.model.OrderStatus import com.example.data.model.RiderStatus import com.example.data.repository.LoveZoneRepository import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import com.example.data.model.FoodReview class AdminViewModel(application: Application) : AndroidViewModel(application) { private val repository = LoveZoneRepository.getInstance(application) val adminSettings: StateFlow = repository.adminSettings val foodItems: StateFlow> = repository.foodItems .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) val orders: StateFlow> = repository.orders .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) val notifications: StateFlow> = repository.notifications .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) val allRiders: StateFlow> = repository.riders .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) val allCustomers: StateFlow> = repository.allCustomers .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) val allReviews: StateFlow> = repository.allReviews .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList()) // Admin Auth State private val _isAuthenticated = MutableStateFlow(false) val isAuthenticated: StateFlow = _isAuthenticated.asStateFlow() val adminPinInput = MutableStateFlow("") val pinErrorMessage = MutableStateFlow(null) // Current tab in Admin Panel: "MENU", "ORDERS", "RIDERS", "REVIEWS", "PAYMENT_QR", "NOTIFICATIONS" val selectedAdminTab = MutableStateFlow("MENU") init { viewModelScope.launch { while (true) { repository.syncCentralCloudOrders() kotlinx.coroutines.delay(4000) } } } fun verifyPin(pin: String): Boolean { return if (pin.trim() == "799264") { _isAuthenticated.value = true pinErrorMessage.value = null true } else { pinErrorMessage.value = "Incorrect PIN" false } } fun logoutAdmin() { _isAuthenticated.value = false adminPinInput.value = "" } fun updateItemImage(foodId: Long, newImageUri: String) { viewModelScope.launch { repository.updateItemImage(foodId, newImageUri) } } fun saveFoodItem(item: FoodItem) { viewModelScope.launch { repository.saveFoodItem(item) } } fun deleteFoodItem(item: FoodItem) { viewModelScope.launch { repository.deleteFoodItem(item) } } fun toggleItemAvailability(item: FoodItem) { viewModelScope.launch { repository.saveFoodItem(item.copy(isAvailable = !item.isAvailable)) } } fun updatePaymentQr(upiId: String, qrUri: String) { val current = adminSettings.value val updated = current.copy( upiId = upiId.trim(), qrImageUri = qrUri.trim() ) repository.updateAdminSettings(updated) } fun updateDeliverySettings(freeDeliveryMin: Double, fee: Double, announcement: String) { val current = adminSettings.value val updated = current.copy( freeDeliveryThreshold = freeDeliveryMin, deliveryFee = fee, announcement = announcement ) repository.updateAdminSettings(updated) } fun updateCustomDomain(domain: String) { val current = adminSettings.value val updated = current.copy( customDomain = domain.trim() ) repository.updateAdminSettings(updated) } fun toggleStoreOpen(isOpen: Boolean) { val current = adminSettings.value repository.updateAdminSettings(current.copy(isOpen = isOpen)) } fun updateOrderStatus(orderId: String, newStatus: OrderStatus) { viewModelScope.launch { repository.updateOrderStatus(orderId, newStatus) } } fun broadcastNotification(title: String, message: String, type: String = "OFFER") { viewModelScope.launch { repository.broadcastNotification(title.trim(), message.trim(), type) } } // --- Rider Section Controls --- fun saveRider(rider: DeliveryRider) { viewModelScope.launch { repository.saveRider(rider) } } fun deleteRider(rider: DeliveryRider) { viewModelScope.launch { repository.deleteRider(rider) } } fun updateRiderStatus(riderId: Long, status: RiderStatus) { viewModelScope.launch { repository.updateRiderStatus(riderId, status) } } fun assignRiderToOrder(orderId: String, rider: DeliveryRider) { viewModelScope.launch { repository.assignRiderToOrder(orderId, rider) } } fun verifyPayment(orderId: String, upiTxnId: String) { viewModelScope.launch { repository.verifyAndCompleteOnlinePayment(orderId, upiTxnId) } } }