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 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(12.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            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 = 8.dp, vertical = 4.dp),
                        style = MaterialTheme.typography.labelSmall
                    )
                }
            }

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

            Spacer(modifier = Modifier.height(8.dp))

            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(16.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            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
            )
        }
    }
}
