package id.iiyh.inatrading.core.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import id.iiyh.inatrading.core.ui.theme.AccentBlue
import id.iiyh.inatrading.core.ui.theme.AccentBlueContainer
import id.iiyh.inatrading.core.ui.theme.AccentPurple
import id.iiyh.inatrading.core.ui.theme.AccentPurpleContainer
import id.iiyh.inatrading.core.ui.theme.BrandRed
import id.iiyh.inatrading.core.ui.theme.BrandRedContainer
import id.iiyh.inatrading.core.ui.theme.InaTradingTheme
import id.iiyh.inatrading.core.ui.theme.OnAccentBlueContainer
import id.iiyh.inatrading.core.ui.theme.OnAccentPurpleContainer
import id.iiyh.inatrading.core.ui.theme.InaShape

// Full roundness sesuai design spec — chip merchant harus kontras vs md (12dp) pada kartu
private val ChipShape = RoundedCornerShape(InaShape.full.dp)

enum class InaChipVariant {
    /** Kategori produk — Secondary blue */
    Category,
    /** Featured merchant / premium — Tertiary purple */
    Featured,
    /** Status aktif / promosi — Primary red */
    Promo,
}

/**
 * Chip sesuai design spec:
 * - Shape: full roundness (9999dp) — beda dengan md (12dp) pada kartu
 * - Featured merchant → `tertiary_container` + `on_tertiary_container`
 * - Category → `secondary_container` + `on_secondary_container`
 * - Promo → `primary_container` + teks merah
 */
@Composable
fun InaChip(
    label: String,
    modifier: Modifier = Modifier,
    variant: InaChipVariant = InaChipVariant.Category,
    selected: Boolean = false,
    onClick: (() -> Unit)? = null,
) {
    val (bg, fg) = when (variant) {
        InaChipVariant.Category -> {
            if (selected) AccentBlue to Color.White
            else AccentBlueContainer to OnAccentBlueContainer
        }
        InaChipVariant.Featured -> {
            if (selected) AccentPurple to Color.White
            else AccentPurpleContainer to OnAccentPurpleContainer
        }
        InaChipVariant.Promo -> {
            if (selected) BrandRed to Color.White
            else BrandRedContainer to BrandRed
        }
    }

    val clickModifier = if (onClick != null) {
        Modifier.clickable(
            interactionSource = remember { MutableInteractionSource() },
            indication = ripple(color = fg.copy(alpha = 0.15f)),
            onClick = onClick,
        )
    } else Modifier

    Row(
        modifier = modifier
            .clip(ChipShape)
            .background(bg)
            .then(clickModifier)
            .padding(horizontal = 14.dp, vertical = 6.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center,
    ) {
        Text(
            text = label,
            style = MaterialTheme.typography.labelMedium,
            color = fg,
        )
    }
}

// ─── Preview ──────────────────────────────────────────────────────────────────
@Preview(showBackground = true, backgroundColor = 0xFFF2F4F5)
@Composable
private fun InaChipPreview() {
    InaTradingTheme {
        Row(
            modifier = Modifier.padding(24.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp),
        ) {
            InaChip(label = "Kuliner", variant = InaChipVariant.Category, onClick = {})
            InaChip(label = "Kuliner", variant = InaChipVariant.Category, selected = true, onClick = {})
            InaChip(label = "Featured", variant = InaChipVariant.Featured, onClick = {})
            InaChip(label = "Promo", variant = InaChipVariant.Promo, onClick = {})
        }
    }
}
