package dev.ulfrx.recipe.ui.components.chips

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composeunstyled.UnstyledButton
import dev.ulfrx.recipe.ui.theme.RecipeTheme

/**
 * Selectable chip for meal-plan slots (śniadanie / lunch / obiad / kolacja /
 * przekąska). Flat surface — no glass refraction — because the chip row sits
 * on the editor's static background where liquid effects add visual noise
 * without revealing anything underneath. Disabled state renders for slots not
 * in the recipe's `allowedSlots`.
 */
@Composable
fun MealSlotChip(
    label: String,
    selected: Boolean,
    enabled: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val colors = RecipeTheme.colors
    val shape = RoundedCornerShape(ChipCornerRadius)
    val backgroundColor =
        when {
            !enabled -> Color.Transparent
            selected -> colors.accent.copy(alpha = SelectedBackgroundAlpha)
            else -> colors.surface
        }
    val borderColor =
        when {
            !enabled -> Color.Transparent
            selected -> colors.accent.copy(alpha = SelectedBorderAlpha)
            else -> colors.borderCard
        }
    val labelColor =
        when {
            !enabled -> colors.contentMuted.copy(alpha = DisabledLabelAlpha)
            selected -> colors.accent
            else -> colors.content
        }

    UnstyledButton(
        onClick = onClick,
        enabled = enabled,
        backgroundColor = backgroundColor,
        contentColor = labelColor,
        shape = shape,
        borderColor = borderColor,
        borderWidth = if (borderColor == Color.Transparent) 0.dp else BorderWidth,
        contentPadding = PaddingValues(horizontal = HorizontalPadding, vertical = VerticalPadding),
        modifier = modifier,
    ) {
        BasicText(
            text = label,
            style =
                RecipeTheme.typography.label.copy(
                    color = labelColor,
                    fontWeight = FontWeight.Normal,
                    fontSize = LabelTextSize,
                ),
        )
    }
}

private const val SelectedBackgroundAlpha = 0.18f
private const val SelectedBorderAlpha = 0.55f
private const val DisabledLabelAlpha = 0.45f

private val ChipCornerRadius = 14.dp
private val BorderWidth = 1.dp
private val HorizontalPadding = 10.dp
private val VerticalPadding = 7.dp
private val LabelTextSize = 11.sp
