package de.tsschulz.timeclock.ui.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import de.tsschulz.timeclock.ui.model.StatusAction
import de.tsschulz.timeclock.ui.model.StatusRow
import de.tsschulz.timeclock.ui.theme.TcColors
import de.tsschulz.timeclock.ui.theme.TcRadius
import de.tsschulz.timeclock.ui.theme.TcSpacing

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun TcStatusBox(
    rows: List<StatusRow>,
    primaryAction: StatusAction?,
    secondaryAction: StatusAction?,
    modifier: Modifier = Modifier,
    onAction: (StatusAction) -> Unit = {},
) {
    Column(
        modifier = modifier
            .background(TcColors.Card, RoundedCornerShape(TcRadius.Card))
            .border(BorderStroke(1.dp, TcColors.Border), RoundedCornerShape(TcRadius.Card))
            .padding(TcSpacing.Md),
        verticalArrangement = Arrangement.spacedBy(TcSpacing.Md),
    ) {
        FlowRow(
            horizontalArrangement = Arrangement.spacedBy(TcSpacing.Sm),
            verticalArrangement = Arrangement.spacedBy(TcSpacing.Sm),
        ) {
            primaryAction?.let { action ->
                TcButton(text = action.label, variant = action.variant, onClick = { onAction(action) })
            }
            secondaryAction?.let { action ->
                TcButton(text = action.label, variant = action.variant, onClick = { onAction(action) })
            }
        }

        Column(verticalArrangement = Arrangement.spacedBy(3.dp)) {
            rows.forEach { row ->
                if (row.isHeading) {
                    Text(
                        text = row.label,
                        color = TcColors.Text,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.SemiBold,
                        modifier = Modifier.padding(top = TcSpacing.Xs),
                    )
                } else {
                    Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                        Text(text = "${row.label}:", color = TcColors.TextMuted, fontSize = 13.sp)
                        Text(text = row.value ?: "-", color = TcColors.Text, fontSize = 13.sp)
                    }
                }
            }
        }
    }
}
