package com.example.ui.screens import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column 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.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.CheckCircle import androidx.compose.material.icons.filled.CloudDone import androidx.compose.material.icons.filled.Save import androidx.compose.material.icons.filled.Security import androidx.compose.material.icons.filled.Verified import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable 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.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.example.ui.components.StatusBadge import com.example.ui.theme.EmeraldDark import com.example.ui.theme.EmeraldLight import com.example.ui.theme.EmeraldPrimary import com.example.ui.viewmodel.MarketingViewModel @Composable fun SettingsScreen( viewModel: MarketingViewModel, modifier: Modifier = Modifier ) { val context = LocalContext.current val account by viewModel.account.collectAsStateWithLifecycle() var businessName by remember(account) { mutableStateOf(account?.businessName ?: "DEWA Gram Udhyog Pvt Ltd") } var gstNumber by remember(account) { mutableStateOf(account?.gstNumber ?: "06AAJCD0975H1ZK") } var email by remember(account) { mutableStateOf(account?.email ?: "contact@sdadigital.in") } var mobile by remember(account) { mutableStateOf(account?.mobileNumber ?: "+919461321162") } var address by remember(account) { mutableStateOf(account?.address ?: "Gali No 4, Uttam Nagar Loharu Road, Bhiwani 127021 HR") } var wabaId by remember(account) { mutableStateOf(account?.metaWabaId ?: "1378746380406848") } var phoneId by remember(account) { mutableStateOf(account?.metaPhoneNumberId ?: "913349665192073") } var accessToken by remember(account) { mutableStateOf(account?.metaAccessTokenMasked ?: "EAAl6rWdth7YBSvrGsUqF53H26vetTv1BmfGssD9wZAFW9XkTcJIawKkKEdJDD6GSgqKhGlfOceMiZCR4ilLtszYanJhMRZBdrj9fyXcZAzvMHupNGD5VEyzAgwSs649OBBtYSLp5HVFU4no0ZBEfnm5c9cLepoZCZByZAS6yVpl9CUXJlzdU8kdxVlg4VgAK4C745C7Q5nQKn7zLF65h8O7jVZCIuu2yM2IGTIMJhn40gZC6mzfRGfmnOjNr3IJ3JeuzgroDsZD") } Column( modifier = modifier .fillMaxSize() .padding(horizontal = 16.dp) .verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(14.dp) ) { Spacer(modifier = Modifier.height(4.dp)) // Meta Connection Status Banner Card( modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(14.dp), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), elevation = CardDefaults.cardElevation(defaultElevation = 2.dp) ) { Column(modifier = Modifier.padding(16.dp)) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Row(verticalAlignment = Alignment.CenterVertically) { Icon(Icons.Default.CloudDone, contentDescription = null, tint = EmeraldPrimary, modifier = Modifier.size(24.dp)) Spacer(modifier = Modifier.width(8.dp)) Text("Meta WhatsApp Cloud API v21.0", fontWeight = FontWeight.Bold, fontSize = 15.sp) } StatusBadge(status = "CONNECTED") } Spacer(modifier = Modifier.height(6.dp)) Text( text = "Official Meta Graph endpoint: https://graph.facebook.com/v21.0/{phone-number-id}/messages", fontSize = 11.sp, fontFamily = FontFamily.Monospace, color = MaterialTheme.colorScheme.onSurfaceVariant ) } } // Business Profile Form Text("Business Information", fontWeight = FontWeight.Bold, fontSize = 16.sp) OutlinedTextField( value = businessName, onValueChange = { businessName = it }, label = { Text("Registered Business Name") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = gstNumber, onValueChange = { gstNumber = it }, label = { Text("GSTIN / Tax Registration") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = email, onValueChange = { email = it }, label = { Text("Operations Email") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = mobile, onValueChange = { mobile = it }, label = { Text("WhatsApp Support Number") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = address, onValueChange = { address = it }, label = { Text("Physical Business Address") }, modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(4.dp)) // Meta Cloud API Credentials Text("Meta WhatsApp Cloud API Credentials", fontWeight = FontWeight.Bold, fontSize = 16.sp) OutlinedTextField( value = wabaId, onValueChange = { wabaId = it }, label = { Text("WhatsApp Business Account ID (WABA ID)") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = phoneId, onValueChange = { phoneId = it }, label = { Text("Meta Phone Number ID") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) OutlinedTextField( value = accessToken, onValueChange = { accessToken = it }, label = { Text("Permanent System User Access Token") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) // Webhook configuration Surface( color = Color(0xFFF1F5F9), shape = RoundedCornerShape(10.dp), modifier = Modifier.fillMaxWidth() ) { Column(modifier = Modifier.padding(12.dp)) { Text("Meta Webhook Configuration:", fontWeight = FontWeight.Bold, fontSize = 12.sp) Spacer(modifier = Modifier.height(4.dp)) Text("Callback URL: https://api.sunpower.example.com/api/v1/webhooks/meta", fontSize = 11.sp, fontFamily = FontFamily.Monospace) Text("Verify Token: sunpower_wa_secret_2026", fontSize = 11.sp, fontFamily = FontFamily.Monospace) Text("Subscriptions: messages, message_template_status_update", fontSize = 11.sp, color = EmeraldPrimary) } } // Compliance Checklist Card Card( modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(10.dp), colors = CardDefaults.cardColors(containerColor = Color(0xFFF0FDF4)) ) { Column(modifier = Modifier.padding(12.dp)) { Row(verticalAlignment = Alignment.CenterVertically) { Icon(Icons.Default.Verified, contentDescription = null, tint = EmeraldPrimary, modifier = Modifier.size(16.dp)) Spacer(modifier = Modifier.width(6.dp)) Text("WhatsApp Policy Compliance Checklist", fontWeight = FontWeight.Bold, fontSize = 13.sp, color = Color(0xFF166534)) } Spacer(modifier = Modifier.height(6.dp)) Text("✓ Verifiable opt-in recorded for 100% of campaign recipients", fontSize = 11.sp, color = Color(0xFF166534)) Text("✓ Approved templates used for outbound messaging outside 24h window", fontSize = 11.sp, color = Color(0xFF166534)) Text("✓ Automatic suppression for 'STOP' and 'UNSUBSCRIBE' keywords", fontSize = 11.sp, color = Color(0xFF166534)) Text("✓ Meta Business quality rating: High (Green)", fontSize = 11.sp, color = Color(0xFF166534)) } } // Action Buttons: Save & Test Connection Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(10.dp) ) { Button( onClick = { account?.let { acc -> viewModel.updateAccountSettings( acc.copy( businessName = businessName, gstNumber = gstNumber, email = email, mobileNumber = mobile, address = address, metaWabaId = wabaId, metaPhoneNumberId = phoneId, metaAccessTokenMasked = accessToken ) ) } Toast.makeText(context, "Account & Meta API settings saved!", Toast.LENGTH_SHORT).show() }, modifier = Modifier .weight(1f) .height(48.dp), shape = RoundedCornerShape(10.dp), colors = ButtonDefaults.buttonColors(containerColor = EmeraldPrimary) ) { Icon(Icons.Default.Save, contentDescription = null, modifier = Modifier.size(16.dp)) Spacer(modifier = Modifier.width(6.dp)) Text("Save Credentials", fontSize = 12.sp) } var isTesting by remember { mutableStateOf(false) } Button( onClick = { isTesting = true viewModel.testMetaApiConnection { success, msg -> isTesting = false Toast.makeText(context, msg, Toast.LENGTH_LONG).show() } }, modifier = Modifier .weight(1f) .height(48.dp), shape = RoundedCornerShape(10.dp), colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF0284C7)) ) { Icon(Icons.Default.CloudDone, contentDescription = null, modifier = Modifier.size(16.dp)) Spacer(modifier = Modifier.width(6.dp)) Text(if (isTesting) "Verifying..." else "Test Connection", fontSize = 12.sp) } } // Direct Authenticated WhatsApp Message Sender Card( modifier = Modifier.fillMaxWidth(), shape = RoundedCornerShape(12.dp), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), elevation = CardDefaults.cardElevation(defaultElevation = 2.dp) ) { Column(modifier = Modifier.padding(14.dp)) { Text( text = "Send Authenticated WhatsApp Test Message", fontWeight = FontWeight.Bold, fontSize = 14.sp, color = MaterialTheme.colorScheme.onSurface ) Spacer(modifier = Modifier.height(4.dp)) Text( text = "Dispatches a live message through Retrofit to https://graph.facebook.com/v21.0/$phoneId/messages using the configured Bearer token.", fontSize = 11.sp, color = MaterialTheme.colorScheme.onSurfaceVariant ) Spacer(modifier = Modifier.height(10.dp)) var testRecipientPhone by remember { mutableStateOf("+919876543210") } var testCustomerName by remember { mutableStateOf("Aarav Patel") } var isSendingTest by remember { mutableStateOf(false) } OutlinedTextField( value = testRecipientPhone, onValueChange = { testRecipientPhone = it }, label = { Text("Recipient Phone (E.164 with country code)") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(8.dp)) OutlinedTextField( value = testCustomerName, onValueChange = { testCustomerName = it }, label = { Text("Customer Name Variable {{1}}") }, singleLine = true, modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(10.dp)) Button( onClick = { isSendingTest = true viewModel.sendDirectWhatsAppMessage( phone = testRecipientPhone, templateName = "festive_solar_launch", customerName = testCustomerName, offerText = "25% Exclusive Festive Discount" ) { success, msg -> isSendingTest = false Toast.makeText(context, msg, Toast.LENGTH_LONG).show() } }, modifier = Modifier .fillMaxWidth() .height(44.dp), shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.buttonColors(containerColor = EmeraldPrimary) ) { Text(if (isSendingTest) "Sending via Meta API..." else "Dispatch Test Message", fontSize = 12.sp) } } } Spacer(modifier = Modifier.height(30.dp)) } }