package com.example.ui.admin import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.os.Build import android.widget.Toast import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Campaign import androidx.compose.material.icons.filled.CheckCircle import androidx.compose.material.icons.filled.NotificationsActive import androidx.compose.material.icons.filled.Send import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextFieldDefaults import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.testTag import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.core.app.NotificationCompat import com.example.R import com.example.ui.theme.FreshVegGreen import com.example.ui.theme.LoveRedPrimary import com.example.ui.theme.TextPrimary import com.example.ui.theme.TextSecondary import com.example.viewmodel.AdminViewModel import java.text.SimpleDateFormat import java.util.Date import java.util.Locale @Composable fun AdminNotificationScreen( adminViewModel: AdminViewModel, modifier: Modifier = Modifier ) { val context = LocalContext.current val notifications by adminViewModel.notifications.collectAsState() var titleInput by remember { mutableStateOf("") } var bodyInput by remember { mutableStateOf("") } var selectedType by remember { mutableStateOf("OFFER") } var feedbackMessage by remember { mutableStateOf(null) } val templates = listOf( "Festive 50% OFF" to Pair("Weekend Special 50% OFF! 🔥", "Craving biryani? Order delicious meals from Love Zone today and get flat 50% discount with code LOVE50!"), "Fresh Biryani Alert" to Pair("Hot Dum Biryani Freshly Prepared! 🍗", "Aromatic Hyderabadi Chicken Dum Biryani is hot and ready in Love Zone kitchen. Order before stock runs out!"), "Free Delivery Deal" to Pair("Free Home Delivery Tonight! 🛵", "Enjoy free fast doorstep delivery on all food orders above ₹149 today."), "New Menu Added" to Pair("New Delicious Snacks Added to Menu! 🍟", "Check out newly added Crispy Chicken Lollipop, Burgers and Thick Shakes on Love Zone app.") ) fun sendDeviceNotification(title: String, message: String) { val channelId = "love_zone_notifications" val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( channelId, "Love Zone Alerts", NotificationManager.IMPORTANCE_DEFAULT ) notificationManager.createNotificationChannel(channel) } val builder = NotificationCompat.Builder(context, channelId) .setSmallIcon(R.drawable.logo_love_zone) .setContentTitle(title) .setContentText(message) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) try { notificationManager.notify((System.currentTimeMillis() % 10000).toInt(), builder.build()) } catch (_: Exception) {} } LazyColumn( modifier = modifier .fillMaxSize() .padding(16.dp) .padding(bottom = 60.dp), verticalArrangement = Arrangement.spacedBy(16.dp) ) { // Header item { Card( shape = RoundedCornerShape(16.dp), colors = CardDefaults.cardColors(containerColor = Color(0xFF1E102E)), modifier = Modifier.fillMaxWidth() ) { Row( modifier = Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically ) { Box( modifier = Modifier .size(44.dp) .background(LoveRedPrimary, CircleShape), contentAlignment = Alignment.Center ) { Icon(Icons.Default.Campaign, contentDescription = null, tint = Color.White, modifier = Modifier.size(24.dp)) } Spacer(modifier = Modifier.width(12.dp)) Column { Text(text = "Customer Notification Center", color = Color.White, fontWeight = FontWeight.Bold, fontSize = 16.sp) Text(text = "Broadcast deals, discounts, and order announcements to all customer apps instantly.", color = Color(0xFFD1C4E9), fontSize = 11.sp) } } } } // Quick Preset Templates item { Text(text = "Quick One-Tap Templates:", fontWeight = FontWeight.Bold, fontSize = 13.sp, color = TextPrimary) Spacer(modifier = Modifier.height(8.dp)) LazyRow(horizontalArrangement = Arrangement.spacedBy(8.dp)) { items(templates) { (label, data) -> Surface( color = Color.White, shape = RoundedCornerShape(12.dp), border = androidx.compose.foundation.BorderStroke(1.dp, Color(0xFFE2E8F0)), modifier = Modifier.clickable { titleInput = data.first bodyInput = data.second } ) { Text( text = label, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = LoveRedPrimary, modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp) ) } } } } // Compose Form Card item { Card( shape = RoundedCornerShape(16.dp), colors = CardDefaults.cardColors(containerColor = Color.White), elevation = CardDefaults.cardElevation(defaultElevation = 2.dp), modifier = Modifier.fillMaxWidth() ) { Column( modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp) ) { Text( text = "COMPOSE PUSH NOTIFICATION", fontWeight = FontWeight.Bold, fontSize = 12.sp, color = TextSecondary, letterSpacing = 1.sp ) OutlinedTextField( value = titleInput, onValueChange = { titleInput = it }, label = { Text("Notification Title") }, placeholder = { Text("e.g. Flat 50% OFF Weekend Biryani") }, singleLine = true, shape = RoundedCornerShape(10.dp), colors = OutlinedTextFieldDefaults.colors( focusedTextColor = Color(0xFF1E2022), unfocusedTextColor = Color(0xFF1E2022), focusedContainerColor = Color.White, unfocusedContainerColor = Color(0xFFF8FAFC), cursorColor = LoveRedPrimary, focusedBorderColor = LoveRedPrimary, unfocusedBorderColor = Color(0xFFCBD5E1) ), modifier = Modifier.fillMaxWidth().testTag("notif_title_input") ) OutlinedTextField( value = bodyInput, onValueChange = { bodyInput = it }, label = { Text("Message Body") }, placeholder = { Text("Write discount details, promo code or update...") }, maxLines = 3, shape = RoundedCornerShape(10.dp), colors = OutlinedTextFieldDefaults.colors( focusedTextColor = Color(0xFF1E2022), unfocusedTextColor = Color(0xFF1E2022), focusedContainerColor = Color.White, unfocusedContainerColor = Color(0xFFF8FAFC), cursorColor = LoveRedPrimary, focusedBorderColor = LoveRedPrimary, unfocusedBorderColor = Color(0xFFCBD5E1) ), modifier = Modifier.fillMaxWidth().testTag("notif_body_input") ) Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp) ) { listOf("OFFER" to "Special Offer", "ORDER_UPDATE" to "Store Update", "ALERT" to "Urgent Alert").forEach { (type, label) -> Surface( color = if (selectedType == type) LoveRedPrimary else Color(0xFFF1F5F9), shape = RoundedCornerShape(10.dp), modifier = Modifier .weight(1f) .clickable { selectedType = type } ) { Text( text = label, fontSize = 11.sp, fontWeight = FontWeight.Bold, color = if (selectedType == type) Color.White else TextPrimary, modifier = Modifier.padding(vertical = 8.dp), textAlign = androidx.compose.ui.text.style.TextAlign.Center ) } } } if (feedbackMessage != null) { Text( text = feedbackMessage ?: "", color = FreshVegGreen, fontSize = 12.sp, fontWeight = FontWeight.Bold ) } Button( onClick = { if (titleInput.isNotBlank() && bodyInput.isNotBlank()) { adminViewModel.broadcastNotification(titleInput, bodyInput, selectedType) sendDeviceNotification(titleInput, bodyInput) feedbackMessage = "Notification broadcasted to all customers successfully! 🚀" Toast.makeText(context, "Broadcast Sent!", Toast.LENGTH_SHORT).show() titleInput = "" bodyInput = "" } }, shape = RoundedCornerShape(10.dp), colors = ButtonDefaults.buttonColors(containerColor = LoveRedPrimary), modifier = Modifier .fillMaxWidth() .height(48.dp) .testTag("send_broadcast_button") ) { Icon(Icons.Default.Send, contentDescription = null, modifier = Modifier.size(18.dp)) Spacer(modifier = Modifier.width(8.dp)) Text(text = "BROADCAST TO CUSTOMERS", fontWeight = FontWeight.Bold, fontSize = 13.sp) } } } } // Notification History Section item { Text( text = "Pushed Notifications History (${notifications.size})", fontWeight = FontWeight.Bold, fontSize = 14.sp, color = TextPrimary ) } items(notifications) { notif -> val dateStr = SimpleDateFormat("dd MMM, hh:mm a", Locale.getDefault()).format(Date(notif.timestamp)) Card( shape = RoundedCornerShape(12.dp), colors = CardDefaults.cardColors(containerColor = Color.White), elevation = CardDefaults.cardElevation(defaultElevation = 1.dp), modifier = Modifier.fillMaxWidth() ) { Row(modifier = Modifier.padding(12.dp), verticalAlignment = Alignment.Top) { Box( modifier = Modifier .size(36.dp) .background(Color(0xFFFFF1F2), CircleShape), contentAlignment = Alignment.Center ) { Icon(Icons.Default.NotificationsActive, contentDescription = null, tint = LoveRedPrimary, modifier = Modifier.size(18.dp)) } Spacer(modifier = Modifier.width(10.dp)) Column(modifier = Modifier.weight(1f)) { Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { Text(text = notif.title, fontWeight = FontWeight.Bold, fontSize = 13.sp) Text(text = dateStr, fontSize = 10.sp, color = Color.Gray) } Spacer(modifier = Modifier.height(2.dp)) Text(text = notif.message, fontSize = 12.sp, color = TextSecondary) } } } } } }