package com.anypreta.bountyboard.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ChecklistRtl
import androidx.compose.material.icons.outlined.Link
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.anypreta.bountyboard.data.model.Task
import com.anypreta.bountyboard.ui.theme.BB
import com.anypreta.bountyboard.ui.util.htmlToPlain
import com.anypreta.bountyboard.ui.util.parseInstant

/**
 * The canonical task card used across boards. Bounty chip top-left, status/age
 * badge top-right, Fraunces title, description preview, AC count + external key,
 * optional footer (actions).
 */
@Composable
fun TaskCard(
    task: Task,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    customerName: String? = null,
    showStatus: Boolean = false,
    showAge: Boolean = false,
    footer: (@Composable () -> Unit)? = null,
) {
    val p = BB.colors
    BBCard(modifier = modifier.fillMaxWidth(), onClick = onClick, topStrip = true, padding = androidx.compose.foundation.layout.PaddingValues(16.dp)) {
        Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically) {
                BountyChip(task.bounty)
                Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) {
                    if (showAge) AgeBadge(task)
                    if (task.external?.orphaned == true) BBBadge("Orphaned", color = p.err)
                    if (showStatus) StatusBadge(task.status)
                }
            }

            Text(
                task.title,
                style = MaterialTheme.typography.titleLarge,
                color = p.text,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
            )

            if (task.description.isNotBlank()) {
                Text(
                    htmlToPlain(task.description),
                    style = MaterialTheme.typography.bodyMedium,
                    color = p.muted,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
            }

            Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp)) {
                customerName?.let {
                    Text(it, style = MaterialTheme.typography.labelSmall, color = p.muted, maxLines = 1)
                }
                task.external?.key?.let {
                    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) {
                        Icon(Icons.Outlined.Link, null, tint = p.muted, modifier = Modifier.height(13.dp).width(13.dp))
                        Text(it, style = MaterialTheme.typography.labelSmall, color = p.muted)
                    }
                }
                val acCount = task.acceptanceCriteria?.size ?: 0
                if (acCount > 0) {
                    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) {
                        Icon(Icons.Outlined.ChecklistRtl, null, tint = p.muted, modifier = Modifier.height(13.dp).width(13.dp))
                        Text("$acCount AC", style = MaterialTheme.typography.labelSmall, color = p.muted)
                    }
                }
            }

            if (footer != null) {
                Spacer(Modifier.height(2.dp))
                footer()
            }
        }
    }
}

@Composable
private fun AgeBadge(task: Task) {
    val p = BB.colors
    val published = parseInstant(task.publishedAt) ?: return
    val days = ((System.currentTimeMillis() - published.time) / 86_400_000L).toInt()
    when {
        days >= 14 -> BBBadge("14d+", color = p.err)
        days >= 7 -> BBBadge("7d+", color = p.warn)
        else -> {}
    }
}
