package com.mhss.app.mybrain.presentation.tgbot import androidx.compose.animation.core.animateFloatAsState 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.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton 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.rememberCoroutineScope 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.ui.Res import com.mhss.app.ui.ic_bolt import com.mhss.app.ui.ic_refresh 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 import kotlinx.coroutines.launch import org.jetbrains.compose.resources.painterResource @Composable fun BotHealthCard( bot: TgBot?, onPingHealth: suspend (botId: Long) -> Result, modifier: Modifier = Modifier ) { var isChecking by remember { mutableStateOf(false) } var currentStats by remember(bot?.healthStats) { mutableStateOf(bot?.healthStats ?: BotHealthStats()) } val coroutineScope = rememberCoroutineScope() Card( modifier = modifier .fillMaxWidth() .padding(horizontal = 14.dp, vertical = 6.dp), shape = RoundedCornerShape(20.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, horizontalArrangement = Arrangement.spacedBy(8.dp) ) { Box( modifier = Modifier .size(10.dp) .clip(CircleShape) .background( if (bot != null && currentStats.isHealthy) Green else Red ) ) Text( text = "Bot Health & Live Latency", style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold), color = MaterialTheme.colorScheme.onSurface ) } OutlinedButton( onClick = { if (bot == null) return@OutlinedButton isChecking = true coroutineScope.launch { val res = onPingHealth(bot.id) isChecking = false res.onSuccess { currentStats = it } } }, shape = RoundedCornerShape(10.dp), contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 10.dp, vertical = 4.dp), enabled = bot != null && !isChecking ) { if (isChecking) { CircularProgressIndicator( color = PrimaryColor, modifier = Modifier.size(14.dp), strokeWidth = 2.dp ) } else { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp) ) { Icon( painter = painterResource(Res.drawable.ic_bolt), contentDescription = null, tint = PrimaryColor, modifier = Modifier.size(14.dp) ) Text( text = "Ping Now", style = MaterialTheme.typography.labelSmall.copy(fontWeight = FontWeight.Bold), color = PrimaryColor ) } } } } Spacer(Modifier.height(12.dp)) // Latency & Gateway Row Row( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(12.dp)) .background(MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f)) .padding(12.dp), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { Column { Text( text = "API Latency", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) ) Row(verticalAlignment = Alignment.Bottom) { Text( text = "${currentStats.latencyMs} ms", style = MaterialTheme.typography.titleLarge.copy(fontWeight = FontWeight.Bold), color = if (currentStats.latencyMs < 200) Green else Orange ) Spacer(Modifier.width(6.dp)) Text( text = if (currentStats.latencyMs < 150) "• Excellent" else "• Good", style = MaterialTheme.typography.labelSmall, color = Green, modifier = Modifier.padding(bottom = 2.dp) ) } } Column(horizontalAlignment = Alignment.End) { Text( text = "Telegram Server", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f) ) Text( text = if (bot != null && currentStats.isHealthy) "HTTP 200 OK" else "Disconnected", style = MaterialTheme.typography.bodyMedium.copy(fontWeight = FontWeight.SemiBold), color = if (bot != null && currentStats.isHealthy) Green else Red ) } } Spacer(Modifier.height(8.dp)) Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text( text = "Mode: Telegram Long Polling", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f), fontSize = 11.sp ) Text( text = "Status: ${if (bot?.isOnline == true) "ONLINE" else "OFFLINE"}", style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f), fontSize = 11.sp ) } } } }