package app.closer.ui.settings

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.OpenInNew
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.closer.R
import app.closer.core.navigation.ExternalLinks

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PrivacyScreen(
    onNavigate: (String) -> Unit = {}
) {
    val uriHandler = LocalUriHandler.current

    Scaffold(
        containerColor = Color.Transparent,
        modifier = Modifier.background(SettingsBackgroundBrush),
        topBar = {
            TopAppBar(
                title = { Text(stringResource(R.string.privacy_title), color = SettingsInk) },
                navigationIcon = {
                    IconButton(onClick = { onNavigate("back") }) {
                        Icon(
                            Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = stringResource(R.string.action_back),
                            tint = SettingsInk
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(containerColor = Color.Transparent)
            )
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .safeDrawingPadding()
                .navigationBarsPadding()
                .verticalScroll(rememberScrollState())
                .padding(padding)
                .padding(horizontal = 16.dp, vertical = 12.dp),
            verticalArrangement = Arrangement.spacedBy(20.dp)
        ) {
            Text(
                text = stringResource(R.string.privacy_rule_intro),
                style = MaterialTheme.typography.bodyLarge,
                color = SettingsMuted
            )

            // ── What your partner can see ─────────────────────────────────────
            PrivacySectionHeader(
                icon = Icons.Default.CheckCircle,
                iconTint = SettingsPrimary,
                title = stringResource(R.string.privacy_section_partner_visible)
            )

            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(containerColor = SettingsCard)
            ) {
                Column(verticalArrangement = Arrangement.spacedBy(0.dp)) {
                    PrivacyRow(
                        title = stringResource(R.string.privacy_daily_answers),
                        body = stringResource(R.string.privacy_only_after_both_answered)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_game_results),
                        body = stringResource(R.string.privacy_game_results_revealed)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_desire_mutual),
                        body = stringResource(R.string.privacy_desire_mutual_body)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_messages),
                        body = stringResource(R.string.privacy_messages_visible)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_game_history),
                        body = stringResource(R.string.privacy_game_history_body)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_streak),
                        body = stringResource(R.string.privacy_streak_body)
                    )
                }
            }

            // ── What stays private ────────────────────────────────────────────
            PrivacySectionHeader(
                icon = Icons.Default.Lock,
                iconTint = MaterialTheme.colorScheme.secondary,
                title = stringResource(R.string.privacy_section_data)
            )

            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(containerColor = SettingsCard)
            ) {
                Column(verticalArrangement = Arrangement.spacedBy(0.dp)) {
                    PrivacyRow(
                        title = stringResource(R.string.privacy_answers_before_reveal),
                        body = stringResource(R.string.privacy_answer_stays_hidden)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_unanswered_desire),
                        body = stringResource(R.string.privacy_desire_one_sided)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_capsule),
                        body = stringResource(R.string.privacy_capsule_sealed)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.notifications_title),
                        body = stringResource(R.string.notifications_footer)
                    )
                }
            }

            // ── Deleting your account ─────────────────────────────────────────
            PrivacySectionHeader(
                icon = Icons.Default.VisibilityOff,
                iconTint = SettingsDanger,
                title = stringResource(R.string.account_delete_title)
            )

            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(containerColor = SettingsCard)
            ) {
                Column(verticalArrangement = Arrangement.spacedBy(0.dp)) {
                    PrivacyRow(
                        title = stringResource(R.string.account_delete_immediate),
                        body = stringResource(R.string.account_delete_body)
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    PrivacyRow(
                        title = stringResource(R.string.privacy_no_export),
                        body = stringResource(R.string.privacy_no_export_body)
                    )
                }
            }

            Spacer(Modifier.height(4.dp))

            // ── Legal docs ────────────────────────────────────────────────────
            Text(
                text = stringResource(R.string.privacy_legal_documents),
                style = MaterialTheme.typography.labelLarge,
                color = SettingsInk,
                modifier = Modifier.padding(horizontal = 4.dp)
            )

            Card(
                modifier = Modifier.fillMaxWidth(),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(containerColor = SettingsCard)
            ) {
                Column {
                    LegalLinkRow(
                        label = stringResource(R.string.privacy_policy),
                        description = "How we handle your data",
                        onClick = { uriHandler.openUri(ExternalLinks.PRIVACY_POLICY) }
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    LegalLinkRow(
                        label = stringResource(R.string.privacy_terms),
                        description = "Your rights and our responsibilities",
                        onClick = { uriHandler.openUri(ExternalLinks.TERMS_OF_SERVICE) }
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    LegalLinkRow(
                        label = stringResource(R.string.privacy_subscription_terms),
                        description = "Billing, renewals, and cancellations",
                        onClick = { uriHandler.openUri(ExternalLinks.SUBSCRIPTION_TERMS) }
                    )
                    Divider(modifier = Modifier.padding(horizontal = 16.dp), thickness = 0.5.dp)
                    LegalLinkRow(
                        label = stringResource(R.string.privacy_support),
                        description = "Get help or contact us",
                        onClick = { uriHandler.openUri(ExternalLinks.SUPPORT) }
                    )
                }
            }

            Spacer(Modifier.height(8.dp))
        }
    }
}

@Composable
private fun PrivacySectionHeader(
    icon: ImageVector,
    iconTint: Color,
    title: String
) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        Box(
            modifier = Modifier
                .size(30.dp)
                .background(iconTint.copy(alpha = 0.12f), CircleShape),
            contentAlignment = Alignment.Center
        ) {
            Icon(icon, contentDescription = null, modifier = Modifier.size(16.dp), tint = iconTint)
        }
        Text(
            text = title,
            style = MaterialTheme.typography.titleSmall.copy(fontWeight = FontWeight.SemiBold),
            color = SettingsInk
        )
    }
}

@Composable
private fun PrivacyRow(title: String, body: String) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 14.dp),
        verticalArrangement = Arrangement.spacedBy(3.dp)
    ) {
        Text(
            text = title,
            style = MaterialTheme.typography.bodyMedium,
            fontWeight = FontWeight.Medium,
            color = SettingsInk
        )
        Text(
            text = body,
            style = MaterialTheme.typography.bodySmall,
            color = SettingsMuted
        )
    }
}

@Composable
private fun LegalLinkRow(
    label: String,
    description: String,
    onClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(horizontal = 16.dp, vertical = 14.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(2.dp)) {
            Text(
                text = label,
                style = MaterialTheme.typography.bodyLarge,
                fontWeight = FontWeight.Medium,
                color = SettingsInk
            )
            Text(
                text = description,
                style = MaterialTheme.typography.bodySmall,
                color = SettingsMuted
            )
        }
        Icon(
            Icons.AutoMirrored.Filled.OpenInNew,
            contentDescription = null,
            modifier = Modifier.size(16.dp),
            tint = SettingsPrimaryDeep
        )
    }
}

@Preview
@Composable
fun PrivacyScreenPreview() {
    PrivacyScreen()
}
