package id.iiyh.inatrading.feature.news.presentation

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.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.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.layout.ContentScale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import id.iiyh.inatrading.R
import id.iiyh.inatrading.core.ui.components.InaInnerTopAppBar
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.BrandRed
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
import id.iiyh.inatrading.feature.news.data.model.NewsArticle

@Composable
fun NewsDetailScreen(
    article: NewsArticle,
    onBack: () -> Unit,
) {
    Scaffold(
        topBar = { InaInnerTopAppBar(onBack = onBack) },
        containerColor = Background,
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
                .verticalScroll(rememberScrollState()),
        ) {
            // ── Hero Image ────────────────────────────────────────────────────
            if (article.image1 != null) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .aspectRatio(16f / 9f),
                ) {
                    AsyncImage(
                        model = article.image1,
                        contentDescription = article.title,
                        contentScale = ContentScale.Crop,
                        modifier = Modifier.fillMaxSize(),
                    )
                    // Gradient overlay
                    Box(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(
                                Brush.verticalGradient(
                                    colors = listOf(
                                        Color.Transparent,
                                        Color.Black.copy(alpha = 0.75f),
                                    ),
                                    startY = 0.3f,
                                )
                            )
                    )
                    // Title overlay at bottom of hero
                    Column(
                        modifier = Modifier
                            .align(Alignment.BottomStart)
                            .padding(20.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp),
                    ) {
                        if (!article.category.isNullOrBlank()) {
                            Box(
                                modifier = Modifier
                                    .clip(RoundedCornerShape(percent = 50))
                                    .background(AccentPurpleContainer.copy(alpha = 0.85f))
                                    .padding(horizontal = 12.dp, vertical = 4.dp),
                            ) {
                                Text(
                                    text = article.category.uppercase(),
                                    style = MaterialTheme.typography.labelSmall,
                                    fontWeight = FontWeight.Bold,
                                    color = AccentPurple,
                                    letterSpacing = 1.5.sp,
                                )
                            }
                        }
                        Text(
                            text = article.title,
                            fontFamily = ManropeFontFamily,
                            fontWeight = FontWeight.ExtraBold,
                            fontSize = 22.sp,
                            color = Color.White,
                            lineHeight = 30.sp,
                        )
                        if (!article.subtitle.isNullOrBlank()) {
                            Text(
                                text = article.subtitle,
                                style = MaterialTheme.typography.bodyMedium,
                                color = Color.White.copy(alpha = 0.80f),
                                lineHeight = 22.sp,
                            )
                        }
                    }
                }
            } else {
                // No image — title block with colored background
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(SurfaceContainerHighest)
                        .padding(horizontal = 24.dp, vertical = 40.dp),
                ) {
                    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                        if (!article.category.isNullOrBlank()) {
                            Box(
                                modifier = Modifier
                                    .clip(RoundedCornerShape(percent = 50))
                                    .background(AccentPurpleContainer)
                                    .padding(horizontal = 12.dp, vertical = 4.dp),
                            ) {
                                Text(
                                    text = article.category.uppercase(),
                                    style = MaterialTheme.typography.labelSmall,
                                    fontWeight = FontWeight.Bold,
                                    color = AccentPurple,
                                    letterSpacing = 1.5.sp,
                                )
                            }
                        }
                        Text(
                            text = article.title,
                            fontFamily = ManropeFontFamily,
                            fontWeight = FontWeight.ExtraBold,
                            fontSize = 24.sp,
                            color = OnSurface,
                            lineHeight = 32.sp,
                        )
                        if (!article.subtitle.isNullOrBlank()) {
                            Text(
                                text = article.subtitle,
                                style = MaterialTheme.typography.bodyMedium,
                                color = OnSurfaceVariant,
                                lineHeight = 22.sp,
                            )
                        }
                    }
                }
            }

            // ── Content Body ──────────────────────────────────────────────────
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 24.dp)
                    .padding(top = 24.dp, bottom = 40.dp),
                verticalArrangement = Arrangement.spacedBy(20.dp),
            ) {
                // Reporter card
                if (!article.reporter.isNullOrBlank()) {
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .clip(RoundedCornerShape(12.dp))
                            .background(SurfaceContainerLow)
                            .padding(16.dp),
                        horizontalArrangement = Arrangement.spacedBy(12.dp),
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        // Avatar initials
                        Box(
                            modifier = Modifier
                                .size(44.dp)
                                .background(BrandRed, CircleShape),
                            contentAlignment = Alignment.Center,
                        ) {
                            Text(
                                text = article.reporter
                                    .split(" ")
                                    .take(2)
                                    .mapNotNull { it.firstOrNull()?.uppercaseChar() }
                                    .joinToString(""),
                                fontFamily = ManropeFontFamily,
                                fontWeight = FontWeight.Bold,
                                fontSize = 14.sp,
                                color = Color.White,
                            )
                        }
                        Column {
                            Text(
                                text = stringResource(R.string.news_reporter),
                                style = MaterialTheme.typography.labelSmall,
                                fontWeight = FontWeight.Bold,
                                color = OnSurfaceVariant,
                                letterSpacing = 1.sp,
                            )
                            Text(
                                text = article.reporter,
                                style = MaterialTheme.typography.bodyMedium,
                                fontWeight = FontWeight.SemiBold,
                                color = OnSurface,
                            )
                        }
                    }
                }

                // Summary / quote block
                val summary = article.summary?.takeIf { it.isNotBlank() }
                    ?: article.subtitle?.takeIf { it.isNotBlank() }
                if (!summary.isNullOrBlank()) {
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(12.dp),
                    ) {
                        Box(
                            modifier = Modifier
                                .width(4.dp)
                                .height(80.dp)
                                .background(BrandRed, RoundedCornerShape(2.dp))
                        )
                        Text(
                            text = "\u201C$summary\u201D",
                            style = MaterialTheme.typography.bodyLarge,
                            fontStyle = FontStyle.Italic,
                            color = OnSurfaceVariant,
                            lineHeight = 28.sp,
                        )
                    }
                }

                // Section 1
                if (!article.section1.isNullOrBlank()) {
                    Text(
                        text = article.section1,
                        style = MaterialTheme.typography.bodyLarge,
                        color = OnSurface,
                        lineHeight = 28.sp,
                    )
                }

                // Image 2 (inline)
                if (article.image2 != null) {
                    AsyncImage(
                        model = article.image2,
                        contentDescription = null,
                        contentScale = ContentScale.Crop,
                        modifier = Modifier
                            .fillMaxWidth()
                            .aspectRatio(16f / 9f)
                            .clip(RoundedCornerShape(12.dp)),
                    )
                }

                // Section 2
                if (!article.section2.isNullOrBlank()) {
                    Text(
                        text = article.section2,
                        style = MaterialTheme.typography.bodyMedium,
                        color = OnSurface,
                        lineHeight = 26.sp,
                    )
                }

                // Section 3
                if (!article.section3.isNullOrBlank()) {
                    Text(
                        text = article.section3,
                        style = MaterialTheme.typography.bodyMedium,
                        color = OnSurface,
                        lineHeight = 26.sp,
                    )
                }

                // Photo gallery (images 2–5 if multiple)
                val galleryImages = listOfNotNull(article.image2, article.image3, article.image4, article.image5)
                    .filter { article.image2 == null || it != article.image2 } // skip image2 if already shown inline
                    .ifEmpty {
                        // if section2 wasn't null, image2 was shown inline, use remaining
                        listOfNotNull(article.image3, article.image4, article.image5)
                    }

                if (galleryImages.isNotEmpty()) {
                    Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
//                        Text(
//                            text = "Dokumentasi",
//                            fontFamily = ManropeFontFamily,
//                            fontWeight = FontWeight.Bold,
//                            fontSize = 18.sp,
//                            color = OnSurface,
//                        )
                        galleryImages.forEach { imageUrl ->
                            AsyncImage(
                                model = imageUrl,
                                contentDescription = null,
                                contentScale = ContentScale.Crop,
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .aspectRatio(4f / 3f)
                                    .clip(RoundedCornerShape(12.dp)),
                            )
                        }
                    }
                }
            }
        }
    }
}
