package dev.ulfrx.recipe.ui.components.calendar

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
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.shape.CircleShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.composeunstyled.UnstyledButton
import dev.ulfrx.recipe.ui.theme.RecipeTheme
import kotlinx.datetime.LocalDate

/**
 * Single day cell — circle, optional outline ring for "today", optional fill
 * for "selected", optional dot indicator below the date number. Disabled days
 * render as a non-interactive box.
 */
@Composable
internal fun CalendarDayCell(
    date: LocalDate,
    state: DayState,
    isSelected: Boolean,
    isToday: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    val baseColor = colors.content
    val mutedColor = colors.contentMuted
    val accent = colors.accent

    val background: Color =
        when {
            isSelected -> accent.copy(alpha = 0.18f)
            else -> Color.Transparent
        }
    val textColor: Color =
        when {
            state.disabled -> mutedColor.copy(alpha = 0.45f)
            state.dimmed && !isSelected -> mutedColor.copy(alpha = 0.55f)
            isSelected -> accent
            else -> baseColor
        }
    val ringColor: Color =
        when {
            isSelected -> accent.copy(alpha = 0.55f)
            isToday -> baseColor.copy(alpha = 0.35f)
            else -> Color.Transparent
        }

    val cellModifier =
        modifier
            .height(36.dp)
            .fillMaxWidth()

    if (state.disabled) {
        Box(modifier = cellModifier, contentAlignment = Alignment.Center) {
            DayCellInner(
                date = date,
                textColor = textColor,
                indicator = state.indicator,
                indicatorColor = mutedColor.copy(alpha = 0.5f),
            )
        }
        return
    }

    UnstyledButton(
        onClick = onClick,
        backgroundColor = background,
        contentColor = textColor,
        shape = CircleShape,
        borderColor = ringColor,
        borderWidth = if (ringColor == Color.Transparent) 0.dp else 1.dp,
        modifier = cellModifier,
    ) {
        DayCellInner(
            date = date,
            textColor = textColor,
            indicator = state.indicator,
            indicatorColor = if (isSelected) accent else mutedColor.copy(alpha = 0.65f),
        )
    }
}

@Composable
private fun DayCellInner(
    date: LocalDate,
    textColor: Color,
    indicator: Boolean,
    indicatorColor: Color,
) {
    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        BasicText(
            text = date.dayOfMonth.toString(),
            style =
                RecipeTheme.typography.label.copy(
                    color = textColor,
                    fontWeight = FontWeight.SemiBold,
                ),
        )
        if (indicator) {
            Spacer(modifier = Modifier.height(2.dp))
            Box(
                modifier =
                    Modifier
                        .size(4.dp)
                        .clip(CircleShape)
                        .background(indicatorColor),
            )
        }
    }
}
