package app.closer.ui.components

import app.closer.ui.theme.closerCardColor
import app.closer.ui.theme.closerSoftPurpleColor
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.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Cake
import androidx.compose.material.icons.filled.CardGiftcard
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
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.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

private val Purple = Color(0xFF56306F)
private val PurpleLight = Color(0xFFF0EDF9)
private val PinkPill = Color(0xFFB98AF4)

@Composable
fun SpecialDatesSection(
    modifier: Modifier = Modifier,
    compact: Boolean = false
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        shape = RoundedCornerShape(if (compact) 24.dp else 28.dp),
        colors = CardDefaults.cardColors(containerColor = closerCardColor(alpha = 0.94f)),
        elevation = CardDefaults.cardElevation(defaultElevation = if (compact) 4.dp else 8.dp)
    ) {
        Column(
            modifier = Modifier.padding(if (compact) 16.dp else 20.dp),
            verticalArrangement = Arrangement.spacedBy(if (compact) 12.dp else 16.dp)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = "Your Special Dates",
                    style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.SemiBold),
                    color = MaterialTheme.colorScheme.onSurface
                )
                if (compact) {
                    DateCountPill()
                }
            }

            // Anniversary featured card
            Surface(
                shape = RoundedCornerShape(20.dp),
                color = closerSoftPurpleColor(),
                modifier = Modifier.fillMaxWidth()
            ) {
                Row(
                    modifier = Modifier.padding(if (compact) 13.dp else 16.dp),
                    horizontalArrangement = Arrangement.spacedBy(14.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    DateBlock(day = "14", month = "Jul", tint = Purple, compact = compact)

                    Column(
                        modifier = Modifier.weight(1f),
                        verticalArrangement = Arrangement.spacedBy(4.dp)
                    ) {
                        Row(
                            horizontalArrangement = Arrangement.spacedBy(8.dp),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Text(
                                text = "Your Anniversary",
                                style = MaterialTheme.typography.bodyLarge.copy(fontWeight = FontWeight.SemiBold),
                                color = MaterialTheme.colorScheme.onSurface
                            )
                            if (!compact) {
                                TodayPill()
                            }
                        }
                        Text(
                            text = "Added by Jessica",
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }

                    Icon(
                        imageVector = Icons.Filled.CardGiftcard,
                        contentDescription = null,
                        tint = Purple,
                        modifier = Modifier.size(24.dp)
                    )
                }
            }

            if (compact) {
                Text(
                    text = "Next up: Jessica's birthday May 10, Mark's birthday Aug 25",
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            } else {
                // Birthday rows
                BirthdayRow(name = "Jessica", day = "10", month = "May")
                BirthdayRow(name = "Mark", day = "25", month = "Aug")
            }
        }
    }
}

@Composable
private fun BirthdayRow(name: String, day: String, month: String) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.spacedBy(14.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        DateBlock(day = day, month = month, tint = Purple, compact = true)

        Text(
            text = "$name's Birthday",
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurface,
            modifier = Modifier.weight(1f)
        )

        Icon(
            imageVector = Icons.Filled.Cake,
            contentDescription = null,
            tint = Purple.copy(alpha = 0.7f),
            modifier = Modifier.size(20.dp)
        )
    }
}

@Composable
private fun DateBlock(
    day: String,
    month: String,
    tint: Color,
    compact: Boolean = false
) {
    val size = if (compact) 44.dp else 54.dp
    Box(
        modifier = Modifier
            .size(size)
            .background(tint.copy(alpha = 0.12f), RoundedCornerShape(12.dp)),
        contentAlignment = Alignment.Center
    ) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(
                text = day,
                style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
                color = tint,
                fontSize = if (compact) 14.sp else 18.sp,
                lineHeight = if (compact) 16.sp else 20.sp
            )
            Text(
                text = month,
                style = MaterialTheme.typography.labelSmall,
                color = tint,
                fontSize = if (compact) 9.sp else 10.sp
            )
        }
    }
}

@Composable
private fun TodayPill() {
    Surface(
        shape = RoundedCornerShape(999.dp),
        color = PinkPill
    ) {
        Text(
            text = "Today",
            modifier = Modifier.padding(horizontal = 8.dp, vertical = 3.dp),
            style = MaterialTheme.typography.labelSmall,
            color = Color.White,
            fontWeight = FontWeight.SemiBold
        )
    }
}

@Composable
private fun DateCountPill() {
    Surface(
        shape = RoundedCornerShape(999.dp),
        color = PurpleLight
    ) {
        Text(
            text = "3 saved",
            modifier = Modifier.padding(horizontal = 8.dp, vertical = 3.dp),
            style = MaterialTheme.typography.labelSmall,
            color = Purple,
            fontWeight = FontWeight.SemiBold
        )
    }
}

@Preview(showBackground = true, backgroundColor = 0xFFFFFBFE)
@Composable
fun SpecialDatesSectionPreview() {
    SpecialDatesSection()
}
