package com.hermes.app.core.designsystem

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.darkColorScheme
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

object HermesPalette {
    val colors = HermesColors
}

object HermesColors {
    val background = Color(0xFF0A0D14)
    val surface = Color(0xFF171B24)
    val surfaceElevated = Color(0xFF222838)
    val accent = Color(0xFFDFBF5B)
    val accentSoft = Color(0x22DFBF5B)
    val positive = Color(0xFF56C28F)
    val warning = Color(0xFFF0B14C)
    val textPrimary = Color.White
    val textSecondary = Color(0xFFB9C0D0)
    val textTertiary = Color(0x80B9C0D0)
}

private val HermesColorScheme = darkColorScheme(
    primary = HermesColors.accent,
    onPrimary = HermesColors.background,
    secondary = HermesColors.surfaceElevated,
    onSecondary = HermesColors.textPrimary,
    background = HermesColors.background,
    onBackground = HermesColors.textPrimary,
    surface = HermesColors.surface,
    onSurface = HermesColors.textPrimary,
)

private val HermesShapes = androidx.compose.material3.Shapes(
    extraSmall = RoundedCornerShape(12.dp),
    small = RoundedCornerShape(16.dp),
    medium = RoundedCornerShape(24.dp),
    large = RoundedCornerShape(28.dp),
    extraLarge = RoundedCornerShape(32.dp),
)

@Composable
fun HermesTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colorScheme = HermesColorScheme,
        typography = MaterialTheme.typography,
        shapes = HermesShapes,
        content = content,
    )
}

@Composable
fun HermesCard(modifier: Modifier = Modifier, elevated: Boolean = false, content: @Composable () -> Unit) {
    Card(
        modifier = modifier.fillMaxWidth(),
        shape = HermesShapes.large,
        colors = CardDefaults.cardColors(
            containerColor = if (elevated) HermesColors.surfaceElevated else HermesColors.surface,
        ),
        elevation = CardDefaults.cardElevation(defaultElevation = if (elevated) 10.dp else 4.dp),
        content = {
            Column(modifier = Modifier.padding(20.dp)) {
                content()
            }
        },
    )
}

@Composable
fun HermesSectionHeader(title: String, subtitle: String, modifier: Modifier = Modifier) {
    Column(modifier = modifier.fillMaxWidth()) {
        Text(text = title, color = HermesColors.textPrimary, style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.Bold)
        Spacer(modifier = Modifier.size(6.dp))
        Text(text = subtitle, color = HermesColors.textSecondary, style = MaterialTheme.typography.bodyMedium)
    }
}

@Composable
fun HermesMetricChip(label: String, value: String, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .heightIn(min = 64.dp)
    ) {
        Card(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors(containerColor = HermesColors.surfaceElevated),
            shape = HermesShapes.medium,
        ) {
            Column(modifier = Modifier.padding(horizontal = 14.dp, vertical = 12.dp)) {
                Text(text = label, color = HermesColors.textTertiary, style = MaterialTheme.typography.labelSmall)
                Text(text = value, color = HermesColors.textPrimary, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
            }
        }
    }
}

@Composable
fun HermesCountdownBadge(label: String, value: String, warning: Boolean = false, modifier: Modifier = Modifier) {
    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(containerColor = if (warning) HermesColors.warning.copy(alpha = 0.18f) else HermesColors.accentSoft),
        shape = HermesShapes.medium,
    ) {
        Column(modifier = Modifier.padding(horizontal = 14.dp, vertical = 12.dp)) {
            Text(text = label, color = HermesColors.textTertiary, style = MaterialTheme.typography.labelSmall)
            Text(text = value, color = if (warning) HermesColors.warning else HermesColors.accent, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
        }
    }
}

@Composable
fun HermesPrimaryButton(text: String, onClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true) {
    Button(
        onClick = onClick,
        enabled = enabled,
        modifier = modifier.fillMaxWidth(),
        colors = ButtonDefaults.buttonColors(containerColor = HermesColors.accent, contentColor = HermesColors.background),
        shape = HermesShapes.medium,
    ) { Text(text) }
}

@Composable
fun HermesSecondaryButton(text: String, selected: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier) {
    Button(
        onClick = onClick,
        modifier = modifier,
        colors = ButtonDefaults.buttonColors(
            containerColor = if (selected) HermesColors.accent else HermesColors.surfaceElevated,
            contentColor = if (selected) HermesColors.background else HermesColors.textPrimary,
        ),
        shape = HermesShapes.small,
    ) { Text(text) }
}
