package com.nexlab.openclaw.node.ui.permission

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.nexlab.openclaw.node.domain.model.PermissionAction

/**
 * Dialog for requesting user confirmation on sensitive actions
 */
@Composable
fun PermissionDialog(
    action: PermissionAction,
    onAllow: (rememberChoice: Boolean) -> Unit,
    onDeny: (rememberChoice: Boolean) -> Unit,
    onDismiss: () -> Unit
) {
    var rememberChoice by remember { mutableStateOf(false) }

    Dialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            shape = MaterialTheme.shapes.large
        ) {
            Column(
                modifier = Modifier
                    .padding(24.dp)
                    .fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // Icon based on sensitivity
                val emoji = when (action.sensitivity) {
                    PermissionAction.Sensitivity.LOW -> "✅"
                    PermissionAction.Sensitivity.MEDIUM -> "⚠️"
                    PermissionAction.Sensitivity.HIGH -> "🔴"
                }
                
                Text(
                    text = emoji,
                    style = MaterialTheme.typography.displayMedium
                )

                Spacer(modifier = Modifier.height(16.dp))

                Text(
                    text = "Permission Request",
                    style = MaterialTheme.typography.headlineSmall,
                    textAlign = TextAlign.Center
                )

                Spacer(modifier = Modifier.height(8.dp))

                Text(
                    text = "Allow ${action.displayName}?",
                    style = MaterialTheme.typography.titleMedium,
                    textAlign = TextAlign.Center
                )

                Spacer(modifier = Modifier.height(8.dp))

                Text(
                    text = action.description,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    textAlign = TextAlign.Center
                )

                Spacer(modifier = Modifier.height(16.dp))

                // Sensitivity warning
                if (action.sensitivity == PermissionAction.Sensitivity.HIGH) {
                    Card(
                        colors = CardDefaults.cardColors(
                            containerColor = MaterialTheme.colorScheme.errorContainer
                        )
                    ) {
                        Text(
                            text = "⚠️ This is a sensitive permission",
                            style = MaterialTheme.typography.bodySmall,
                            modifier = Modifier.padding(8.dp),
                            color = MaterialTheme.colorScheme.onErrorContainer
                        )
                    }
                    Spacer(modifier = Modifier.height(16.dp))
                }

                // Remember choice checkbox
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Checkbox(
                        checked = rememberChoice,
                        onCheckedChange = { rememberChoice = it }
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Column {
                        Text(
                            text = "Remember this choice",
                            style = MaterialTheme.typography.bodyMedium
                        )
                        Text(
                            text = if (rememberChoice) "Won't ask again" else "Ask every time",
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                }

                Spacer(modifier = Modifier.height(24.dp))

                // Buttons
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    OutlinedButton(
                        onClick = { onDeny(rememberChoice) },
                        modifier = Modifier.weight(1f)
                    ) {
                        Text("Deny")
                    }

                    Button(
                        onClick = { onAllow(rememberChoice) },
                        modifier = Modifier.weight(1f)
                    ) {
                        Text("Allow")
                    }
                }
            }
        }
    }
}

/**
 * Compact permission status item for settings screen
 */
@Composable
fun PermissionStatusItem(
    action: PermissionAction,
    isAllowed: Boolean?,
    isRemembered: Boolean,
    onClearDecision: () -> Unit,
    onToggleAllowlist: (Boolean) -> Unit
) {
    var showDialog by remember { mutableStateOf(false) }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = action.displayName,
                style = MaterialTheme.typography.bodyLarge
            )
            Text(
                text = action.description,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
        }

        // Status chip
        val statusText = when {
            isRemembered && isAllowed == true -> "✅ Always allowed"
            isRemembered && isAllowed == false -> "❌ Always denied"
            isAllowed == true -> "Allowed"
            isAllowed == false -> "Denied"
            action.sensitivity == PermissionAction.Sensitivity.LOW -> "Auto"
            else -> "Ask"
        }

        AssistChip(
            onClick = { showDialog = true },
            label = { Text(statusText) }
        )
    }

    if (showDialog) {
        // Simple toggle dialog
        AlertDialog(
            onDismissRequest = { showDialog = false },
            title = { Text(action.displayName) },
            text = { Text(action.description) },
            confirmButton = {
                TextButton(onClick = { 
                    onClearDecision()
                    showDialog = false
                }) {
                    Text("Clear Decision")
                }
            },
            dismissButton = {
                TextButton(onClick = { showDialog = false }) {
                    Text("Close")
                }
            }
        )
    }
}
