package com.tt.honeyDue.ui.components.common

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.error
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.tt.honeyDue.ui.haptics.Haptics
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
fun ErrorCard(
    message: String,
    modifier: Modifier = Modifier
) {
    if (message.isNotEmpty()) {
        // P5 Stream S — form-validation / network error haptic
        LaunchedEffect(message) { Haptics.error() }
        Card(
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.errorContainer
            ),
            modifier = modifier
                .fillMaxWidth()
                .semantics {
                    liveRegion = LiveRegionMode.Polite
                    error(message)
                },
            shape = RoundedCornerShape(12.dp)
        ) {
            Text(
                text = message,
                color = MaterialTheme.colorScheme.onErrorContainer,
                modifier = Modifier.padding(16.dp),
                style = MaterialTheme.typography.bodyMedium
            )
        }
    }
}

@Preview
@Composable
fun ErrorCardPreview() {
    MaterialTheme {
        ErrorCard(
            message = "Invalid username or password. Please try again.",
            modifier = Modifier.padding(16.dp)
        )
    }
}
