package com.tt.honeyDue.ui.components.task

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.tt.honeyDue.util.DateUtils
import com.tt.honeyDue.ui.theme.AppRadius
import com.tt.honeyDue.ui.theme.AppSpacing
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
fun SimpleTaskListItem(
    title: String,
    description: String?,
    priority: String?,
    status: String?,
    dueDate: String?,
    isOverdue: Boolean = false,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        shape = RoundedCornerShape(AppRadius.md),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(AppSpacing.lg)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = title,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.SemiBold
                )
                // Priority badge
                Surface(
                    color = when (priority?.lowercase()) {
                        "urgent" -> MaterialTheme.colorScheme.error
                        "high" -> MaterialTheme.colorScheme.errorContainer
                        "medium" -> MaterialTheme.colorScheme.primaryContainer
                        else -> MaterialTheme.colorScheme.surfaceVariant
                    },
                    shape = MaterialTheme.shapes.small
                ) {
                    Text(
                        text = priority?.uppercase() ?: "LOW",
                        modifier = Modifier.padding(horizontal = AppSpacing.sm, vertical = AppSpacing.xs),
                        style = MaterialTheme.typography.labelSmall
                    )
                }
            }

            if (description != null) {
                Spacer(modifier = Modifier.height(AppSpacing.xs))
                Text(
                    text = description,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }

            Spacer(modifier = Modifier.height(AppSpacing.sm))

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = "Status: ${status?.replaceFirstChar { it.uppercase() } ?: "Unknown"}",
                    style = MaterialTheme.typography.bodySmall
                )
                if (dueDate != null) {
                    Text(
                        text = "Due: ${DateUtils.formatDate(dueDate)}",
                        style = MaterialTheme.typography.bodySmall,
                        color = if (isOverdue)
                            MaterialTheme.colorScheme.error
                        else
                            MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }
            }
        }
    }
}

@Preview
@Composable
fun SimpleTaskListItemPreview() {
    MaterialTheme {
        Column(
            modifier = Modifier.padding(AppSpacing.lg),
            verticalArrangement = Arrangement.spacedBy(AppSpacing.sm)
        ) {
            SimpleTaskListItem(
                title = "Fix leaky faucet",
                description = "Kitchen sink is dripping",
                priority = "high",
                status = "pending",
                dueDate = "2024-12-20",
                isOverdue = false
            )

            SimpleTaskListItem(
                title = "Paint living room",
                description = null,
                priority = "medium",
                status = "in_progress",
                dueDate = "2024-12-15",
                isOverdue = true
            )
        }
    }
}
