package com.tt.honeyDue.ui.subscription

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
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.theme.AppRadius
import com.tt.honeyDue.ui.theme.AppSpacing

@Composable
fun FeatureComparisonDialog(
    onDismiss: () -> Unit,
    onUpgrade: () -> Unit
) {
    val featureBenefits = DataManager.featureBenefits.value

    Dialog(onDismissRequest = onDismiss) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(0.9f)
                .padding(AppSpacing.md),
            shape = MaterialTheme.shapes.large,
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.background
            )
        ) {
            Column(
                modifier = Modifier.fillMaxSize()
            ) {
                // Header
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(AppSpacing.lg),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        "Choose Your Plan",
                        style = MaterialTheme.typography.headlineSmall,
                        fontWeight = FontWeight.Bold
                    )
                    IconButton(onClick = onDismiss) {
                        Icon(Icons.Default.Close, contentDescription = "Close")
                    }
                }

                Text(
                    "Upgrade to Pro for unlimited access",
                    modifier = Modifier.padding(horizontal = AppSpacing.lg),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )

                Spacer(Modifier.height(AppSpacing.lg))

                // Comparison Table
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .verticalScroll(rememberScrollState())
                ) {
                    // Header Row
                    Surface(
                        modifier = Modifier.fillMaxWidth(),
                        color = MaterialTheme.colorScheme.surfaceVariant
                    ) {
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(AppSpacing.md),
                            horizontalArrangement = Arrangement.SpaceBetween
                        ) {
                            Text(
                                "Feature",
                                modifier = Modifier.weight(1f),
                                style = MaterialTheme.typography.titleSmall,
                                fontWeight = FontWeight.Bold
                            )
                            Text(
                                "Free",
                                modifier = Modifier.width(80.dp),
                                style = MaterialTheme.typography.titleSmall,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center,
                                color = MaterialTheme.colorScheme.onSurfaceVariant
                            )
                            Text(
                                "Pro",
                                modifier = Modifier.width(80.dp),
                                style = MaterialTheme.typography.titleSmall,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Center,
                                color = MaterialTheme.colorScheme.primary
                            )
                        }
                    }

                    HorizontalDivider()

                    // Feature Rows
                    if (featureBenefits.isNotEmpty()) {
                        featureBenefits.forEach { benefit ->
                            ComparisonRow(
                                featureName = benefit.featureName,
                                freeText = benefit.freeTierText,
                                proText = benefit.proTierText
                            )
                            HorizontalDivider()
                        }
                    } else {
                        // Default features if no data loaded
                        ComparisonRow("Properties", "1 property", "Unlimited")
                        HorizontalDivider()
                        ComparisonRow("Tasks", "10 tasks", "Unlimited")
                        HorizontalDivider()
                        ComparisonRow("Contractors", "Not available", "Unlimited")
                        HorizontalDivider()
                        ComparisonRow("Documents", "Not available", "Unlimited")
                        HorizontalDivider()
                    }
                }

                // Upgrade Button
                Button(
                    onClick = {
                        onUpgrade()
                        onDismiss()
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(AppSpacing.lg),
                    shape = MaterialTheme.shapes.medium
                ) {
                    Text("Upgrade to Pro", fontWeight = FontWeight.Bold)
                }
            }
        }
    }
}

@Composable
private fun ComparisonRow(
    featureName: String,
    freeText: String,
    proText: String
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(AppSpacing.md),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            featureName,
            modifier = Modifier.weight(1f),
            style = MaterialTheme.typography.bodyMedium
        )
        Text(
            freeText,
            modifier = Modifier.width(80.dp),
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            textAlign = TextAlign.Center
        )
        Text(
            proText,
            modifier = Modifier.width(80.dp),
            style = MaterialTheme.typography.bodyMedium,
            fontWeight = FontWeight.Medium,
            color = MaterialTheme.colorScheme.primary,
            textAlign = TextAlign.Center
        )
    }
}
