package com.mhss.app.mybrain.presentation.tgbot.dialogs import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer 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.items import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.AlertDialog import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Text import androidx.compose.material3.TextButton 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.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.mhss.app.mybrain.presentation.tgbot.BotLogEntry import com.mhss.app.ui.theme.Blue import com.mhss.app.ui.theme.Green import com.mhss.app.ui.theme.Orange import com.mhss.app.ui.theme.PrimaryColor import com.mhss.app.ui.theme.Red @Composable fun BotLogsViewerDialog( logs: List, onDismiss: () -> Unit, onClearLogs: () -> Unit ) { var selectedFilter by remember { mutableStateOf("ALL") } val filteredLogs = remember(logs, selectedFilter) { if (selectedFilter == "ALL") logs else logs.filter { it.level.equals(selectedFilter, ignoreCase = true) } } AlertDialog( onDismissRequest = onDismiss, title = { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Text( text = "Live Bot Logs", style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold) ) Text( text = "${logs.size} entries", style = MaterialTheme.typography.labelSmall, color = PrimaryColor ) } }, text = { Column( modifier = Modifier .fillMaxWidth() .height(380.dp), verticalArrangement = Arrangement.spacedBy(8.dp) ) { // Filter Tabs Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(6.dp) ) { listOf("ALL", "INFO", "SUCCESS", "WARN", "ERROR").forEach { level -> val isSelected = selectedFilter == level Box( modifier = Modifier .clip(RoundedCornerShape(8.dp)) .background( if (isSelected) PrimaryColor else MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f) ) .padding(horizontal = 8.dp, vertical = 4.dp), contentAlignment = Alignment.Center ) { Text( text = level, style = MaterialTheme.typography.labelSmall.copy( fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal ), color = if (isSelected) Color.White else MaterialTheme.colorScheme.onSurfaceVariant, fontSize = 10.sp ) } } } Spacer(Modifier.height(4.dp)) if (filteredLogs.isEmpty()) { Box( modifier = Modifier .fillMaxWidth() .weight(1f), contentAlignment = Alignment.Center ) { Text( text = "No logs recorded for filter '$selectedFilter'.", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f) ) } } else { LazyColumn( modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(8.dp) ) { items(filteredLogs) { entry -> LogItemRow(entry) } } } } }, confirmButton = { OutlinedButton( onClick = onClearLogs, shape = RoundedCornerShape(10.dp) ) { Text("Clear Logs", color = Red) } }, dismissButton = { TextButton(onClick = onDismiss) { Text("Close") } } ) } @Composable private fun LogItemRow(entry: BotLogEntry) { val levelColor = when (entry.level.uppercase()) { "SUCCESS" -> Green "WARN" -> Orange "ERROR" -> Red else -> Blue } Column( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(10.dp)) .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.35f)) .padding(10.dp) ) { Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp) ) { Box( modifier = Modifier .size(6.dp) .clip(CircleShape) .background(levelColor) ) Text( text = entry.title, style = MaterialTheme.typography.bodySmall.copy(fontWeight = FontWeight.SemiBold), color = MaterialTheme.colorScheme.onSurface ) } Text( text = entry.level, style = MaterialTheme.typography.labelSmall.copy(fontWeight = FontWeight.Bold), color = levelColor, fontSize = 9.sp ) } if (entry.details.isNotBlank()) { Spacer(Modifier.height(2.dp)) Text( text = entry.details, style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f), fontSize = 11.sp ) } } }