package id.iiyh.inatrading.feature.info.presentation

import android.content.Intent
import android.net.Uri
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.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
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.outlined.Dataset
import androidx.compose.material.icons.outlined.HeadsetMic
import androidx.compose.material.icons.outlined.Public
import androidx.compose.material.icons.outlined.Security
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import id.iiyh.inatrading.R
import id.iiyh.inatrading.core.ui.components.InaInnerTopAppBar
import id.iiyh.inatrading.core.ui.components.InaPrimaryButton
import id.iiyh.inatrading.core.ui.components.InaSecondaryButton
import id.iiyh.inatrading.core.ui.components.InaTertiaryButton
import id.iiyh.inatrading.core.ui.theme.AccentBlue
import id.iiyh.inatrading.core.ui.theme.AccentPurple
import id.iiyh.inatrading.core.ui.theme.AccentPurpleContainer
import id.iiyh.inatrading.core.ui.theme.Background
import id.iiyh.inatrading.core.ui.theme.BrandNavy
import id.iiyh.inatrading.core.ui.theme.BrandRed
import id.iiyh.inatrading.core.ui.theme.BrandRedLight
import id.iiyh.inatrading.core.ui.theme.InterFontFamily
import id.iiyh.inatrading.core.ui.theme.ManropeFontFamily
import id.iiyh.inatrading.core.ui.theme.OnSurface
import id.iiyh.inatrading.core.ui.theme.OnSurfaceVariant
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerHighest
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerLow
import id.iiyh.inatrading.core.ui.theme.SurfaceContainerLowest

private const val WA_SUPPORT_NUMBER = ""

@Composable
fun AboutScreen(
    onBack: () -> Unit,
) {
    val context = LocalContext.current

    Scaffold(
        topBar = { InaInnerTopAppBar(onBack = onBack) },
        containerColor = Background,
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .verticalScroll(rememberScrollState()),
        ) {
            // ── Hero Section ──────────────────────────────────────────────
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 24.dp)
                    .padding(top = 24.dp, bottom = 8.dp),
            ) {
                // Decorative slope — right side
                Box(
                    modifier = Modifier
                        .align(Alignment.TopEnd)
                        .offset(x = 24.dp)
                        .width(220.dp)
                        .height(200.dp)
                        .clip(RoundedCornerShape(topStart = 0.dp, topEnd = 0.dp, bottomStart = 64.dp, bottomEnd = 0.dp))
                        .background(SurfaceContainerHighest.copy(alpha = 0.50f))
                )

                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(16.dp),
                ) {
                    Text(
                        text = stringResource(R.string.about_label).uppercase(),
                        style = MaterialTheme.typography.labelSmall,
                        fontWeight = FontWeight.SemiBold,
                        color = BrandRed,
                        letterSpacing = 1.5.sp,
                    )

                    Text(
                        text = buildAnnotatedString {
                            withStyle(SpanStyle(color = OnSurface)) {
                                append(stringResource(R.string.about_headline_part1))
                                append(" ")
                            }
                            withStyle(SpanStyle(color = BrandRed)) {
                                append(stringResource(R.string.about_headline_part2))
                            }
                            withStyle(SpanStyle(color = OnSurface)) {
                                append(" ")
                                append(stringResource(R.string.about_headline_part3))
                            }
                        },
                        fontFamily = ManropeFontFamily,
                        fontWeight = FontWeight.ExtraBold,
                        fontSize = 38.sp,
                        lineHeight = 46.sp,
                    )

                    Text(
                        text = stringResource(R.string.about_intro),
                        fontFamily = InterFontFamily,
                        fontSize = 15.sp,
                        color = OnSurfaceVariant,
                        lineHeight = 24.sp,
                    )
                }
            }

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

            // ── Bento Grid ────────────────────────────────────────────────
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 24.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp),
            ) {
                // Row 1: Large card (Secure Transactions) + Small card (Digital Integration)
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp),
                ) {
                    // Large — Secure Transactions
                    Box(
                        modifier = Modifier
                            .weight(1.6f)
                            .clip(RoundedCornerShape(16.dp))
                            .background(SurfaceContainerLowest)
                            .padding(20.dp),
                    ) {
                        // Blur decoration
                        Box(
                            modifier = Modifier
                                .size(100.dp)
                                .align(Alignment.BottomEnd)
                                .offset(x = 20.dp, y = 20.dp)
                                .background(BrandRed.copy(alpha = 0.06f), CircleShape)
                        )
                        Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
                            BentoIcon(icon = Icons.Outlined.Security, bg = BrandRed.copy(alpha = 0.10f), tint = BrandRed)
                            Text(
                                text = stringResource(R.string.about_feature1_title),
                                fontFamily = ManropeFontFamily,
                                fontWeight = FontWeight.Bold,
                                fontSize = 20.sp,
                                color = OnSurface,
                            )
                            Text(
                                text = stringResource(R.string.about_feature1_desc),
                                style = MaterialTheme.typography.bodySmall,
                                color = OnSurfaceVariant,
                                lineHeight = 18.sp,
                            )
                        }
                    }

                    // Small — Digital Integration
                    Column(
                        modifier = Modifier
                            .weight(1f)
                            .clip(RoundedCornerShape(16.dp))
                            .background(AccentBlue.copy(alpha = 0.12f))
                            .padding(20.dp),
                        verticalArrangement = Arrangement.spacedBy(12.dp),
                    ) {
                        BentoIcon(icon = Icons.Outlined.Dataset, bg = AccentBlue.copy(alpha = 0.15f), tint = AccentBlue)
                        Text(
                            text = stringResource(R.string.about_feature2_title),
                            fontFamily = ManropeFontFamily,
                            fontWeight = FontWeight.Bold,
                            fontSize = 17.sp,
                            color = BrandNavy,
                        )
                        Text(
                            text = stringResource(R.string.about_feature2_desc),
                            style = MaterialTheme.typography.bodySmall,
                            color = BrandNavy.copy(alpha = 0.70f),
                            lineHeight = 18.sp,
                        )
                    }
                }

                // Row 2: Small card (Global Reach) + Image card
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp),
                ) {
                    // Small — Global Reach
                    Column(
                        modifier = Modifier
                            .weight(1f)
                            .clip(RoundedCornerShape(16.dp))
                            .background(AccentPurple)
                            .padding(20.dp),
                        verticalArrangement = Arrangement.spacedBy(12.dp),
                    ) {
                        BentoIcon(icon = Icons.Outlined.Public, bg = Color.White.copy(alpha = 0.20f), tint = Color.White)
                        Text(
                            text = stringResource(R.string.about_feature3_title),
                            fontFamily = ManropeFontFamily,
                            fontWeight = FontWeight.Bold,
                            fontSize = 17.sp,
                            color = Color.White,
                        )
                        Text(
                            text = stringResource(R.string.about_feature3_desc),
                            style = MaterialTheme.typography.bodySmall,
                            color = Color.White.copy(alpha = 0.85f),
                            lineHeight = 18.sp,
                        )
                    }

                    // Image placeholder + quote
                    Box(
                        modifier = Modifier
                            .weight(1.6f)
                            .height(180.dp)
                            .clip(RoundedCornerShape(16.dp))
                            .background(SurfaceContainerHighest),
                    ) {
                        // Placeholder label
                        Text(
                            text = "[ Office Image ]",
                            style = MaterialTheme.typography.bodySmall,
                            color = OnSurfaceVariant.copy(alpha = 0.4f),
                            modifier = Modifier.align(Alignment.Center),
                        )
                        // Gradient overlay + quote
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(
                                    Brush.verticalGradient(
                                        colors = listOf(Color.Transparent, Color.Black.copy(alpha = 0.55f)),
                                    )
                                ),
                            contentAlignment = Alignment.BottomStart,
                        ) {
                            Text(
                                text = stringResource(R.string.about_image_quote),
                                style = MaterialTheme.typography.bodySmall,
                                color = Color.White,
                                fontFamily = InterFontFamily,
                                modifier = Modifier.padding(16.dp),
                            )
                        }
                    }
                }
            }

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

            // ── Mission Statement ─────────────────────────────────────────
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 24.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .background(SurfaceContainerLow)
                    .padding(24.dp),
                verticalArrangement = Arrangement.spacedBy(20.dp),
            ) {
                Text(
                    text = stringResource(R.string.about_mission_title),
                    fontFamily = ManropeFontFamily,
                    fontWeight = FontWeight.Bold,
                    fontSize = 28.sp,
                    color = OnSurface,
                )
                Text(
                    text = stringResource(R.string.about_mission_body),
                    fontFamily = InterFontFamily,
                    fontSize = 15.sp,
                    color = OnSurfaceVariant,
                    lineHeight = 26.sp,
                )

                // TODO: wire Learn More
                InaPrimaryButton(
                    text = stringResource(R.string.about_mission_cta),
                    onClick = { /* TODO */ },
                )

                // Stats grid
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp),
                ) {
                    StatCard(
                        modifier = Modifier.weight(1f),
                        value = "500+",
                        label = stringResource(R.string.about_stat1_label),
                        valueColor = BrandRed,
                    )
                    StatCard(
                        modifier = Modifier.weight(1f),
                        value = "12k+",
                        label = stringResource(R.string.about_stat2_label),
                        valueColor = AccentBlue,
                    )
                }
            }

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

            // ── CTA Section ───────────────────────────────────────────────
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 24.dp)
                    .padding(bottom = 40.dp),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.spacedBy(16.dp),
            ) {
                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(percent = 50))
                        .background(AccentPurpleContainer.copy(alpha = 0.25f))
                        .padding(horizontal = 16.dp, vertical = 6.dp),
                ) {
                    Text(
                        text = stringResource(R.string.about_cta_label).uppercase(),
                        style = MaterialTheme.typography.labelSmall,
                        fontWeight = FontWeight.SemiBold,
                        color = AccentPurple,
                        letterSpacing = 1.5.sp,
                    )
                }

                Text(
                    text = stringResource(R.string.about_cta_title),
                    fontFamily = ManropeFontFamily,
                    fontWeight = FontWeight.Bold,
                    fontSize = 24.sp,
                    color = OnSurface,
                    textAlign = TextAlign.Center,
                    lineHeight = 32.sp,
                )

                Text(
                    text = stringResource(R.string.about_cta_desc),
                    style = MaterialTheme.typography.bodyMedium,
                    color = OnSurfaceVariant,
                    textAlign = TextAlign.Center,
                    lineHeight = 22.sp,
                )

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

                // Register as Seller — placeholder
                InaPrimaryButton(
                    text = stringResource(R.string.about_cta_register),
                    onClick = { /* TODO: navigate to seller register */ },
                )

                // Contact Support — WA
                InaSecondaryButton(
                    text = stringResource(R.string.about_cta_contact),
                    onClick = {
                        if (WA_SUPPORT_NUMBER.isNotEmpty()) {
                            val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://wa.me/$WA_SUPPORT_NUMBER"))
                            context.startActivity(intent)
                        }
                    },
                )
            }
        }
    }
}

@Composable
private fun BentoIcon(
    icon: ImageVector,
    bg: Color,
    tint: Color,
) {
    Box(
        modifier = Modifier
            .size(44.dp)
            .clip(RoundedCornerShape(10.dp))
            .background(bg),
        contentAlignment = Alignment.Center,
    ) {
        Icon(icon, contentDescription = null, tint = tint, modifier = Modifier.size(22.dp))
    }
}

@Composable
private fun StatCard(
    value: String,
    label: String,
    valueColor: Color,
    modifier: Modifier = Modifier,
) {
    Column(
        modifier = modifier
            .clip(RoundedCornerShape(12.dp))
            .background(SurfaceContainerLowest)
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(4.dp),
    ) {
        Text(
            text = value,
            fontFamily = ManropeFontFamily,
            fontWeight = FontWeight.ExtraBold,
            fontSize = 32.sp,
            color = valueColor,
        )
        Text(
            text = label.uppercase(),
            style = MaterialTheme.typography.labelSmall,
            color = OnSurfaceVariant,
            letterSpacing = 1.sp,
            textAlign = TextAlign.Center,
        )
    }
}
