package com.tt.honeyDue.ui.subscription

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.tt.honeyDue.data.DataManager
import com.tt.honeyDue.ui.screens.subscription.FeatureComparisonScreen
import com.tt.honeyDue.ui.theme.AppRadius
import com.tt.honeyDue.ui.theme.AppSpacing

@Composable
fun UpgradePromptDialog(
    triggerKey: String,
    onDismiss: () -> Unit,
    onUpgrade: () -> Unit
) {
    val triggerData = DataManager.upgradeTriggers.value[triggerKey]
    var showFeatureComparison by remember { mutableStateOf(false) }
    var isProcessing by remember { mutableStateOf(false) }

    if (showFeatureComparison) {
        // P2 Stream E — migrated from FeatureComparisonDialog to the
        // full-screen FeatureComparisonScreen. Tapping the CTA invokes
        // the existing onUpgrade flow (BillingManager / StoreKit).
        FeatureComparisonScreen(
            onNavigateBack = { showFeatureComparison = false },
            onNavigateToUpgrade = {
                showFeatureComparison = false
                onUpgrade()
            },
        )
    } else {
        Dialog(onDismissRequest = onDismiss) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(AppSpacing.md),
                shape = MaterialTheme.shapes.large,
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.background
                )
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(AppSpacing.xl),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.spacedBy(AppSpacing.md)
                ) {
                    // Icon
                    Icon(
                        imageVector = Icons.Default.Stars,
                        contentDescription = null, // decorative
                        modifier = Modifier.size(60.dp),
                        tint = MaterialTheme.colorScheme.tertiary
                    )

                    // Title
                    Text(
                        triggerData?.title ?: "Upgrade to Pro",
                        style = MaterialTheme.typography.titleLarge,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center
                    )

                    // Message
                    Text(
                        triggerData?.message ?: "Unlock unlimited access to all features",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        textAlign = TextAlign.Center
                    )

                    Spacer(Modifier.height(AppSpacing.sm))

                    // Pro Features Preview
                    Surface(
                        shape = MaterialTheme.shapes.medium,
                        color = MaterialTheme.colorScheme.surfaceVariant
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(AppSpacing.md),
                            verticalArrangement = Arrangement.spacedBy(AppSpacing.sm)
                        ) {
                            FeatureRow(Icons.Default.Home, "Unlimited properties")
                            FeatureRow(Icons.Default.CheckCircle, "Unlimited tasks")
                            FeatureRow(Icons.Default.People, "Contractor management")
                            FeatureRow(Icons.Default.Description, "Document & warranty storage")
                        }
                    }

                    Spacer(Modifier.height(AppSpacing.sm))

                    // Upgrade Button
                    Button(
                        onClick = {
                            isProcessing = true
                            onUpgrade()
                        },
                        modifier = Modifier.fillMaxWidth(),
                        enabled = !isProcessing,
                        shape = MaterialTheme.shapes.medium
                    ) {
                        if (isProcessing) {
                            CircularProgressIndicator(
                                modifier = Modifier.size(20.dp),
                                color = MaterialTheme.colorScheme.onPrimary,
                                strokeWidth = 2.dp
                            )
                        } else {
                            Text(
                                triggerData?.buttonText ?: "Upgrade to Pro",
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }

                    // Compare Plans
                    TextButton(onClick = { showFeatureComparison = true }) {
                        Text("Compare Free vs Pro")
                    }

                    // Cancel
                    TextButton(onClick = onDismiss) {
                        Text("Maybe Later")
                    }
                }
            }
        }
    }
}

@Composable
private fun FeatureRow(icon: androidx.compose.ui.graphics.vector.ImageVector, text: String) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.spacedBy(AppSpacing.sm),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null, // decorative
            modifier = Modifier.size(20.dp),
            tint = MaterialTheme.colorScheme.primary
        )
        Text(
            text,
            style = MaterialTheme.typography.bodyMedium
        )
    }
}
