package com.anypreta.bountyboard.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowDropDown
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.anypreta.bountyboard.ui.theme.BB

@Composable
fun FilterPill(
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    icon: ImageVector? = null,
    showCaret: Boolean = true,
) {
    val p = BB.colors
    val shape = RoundedCornerShape(p.radius.dp)
    Row(
        modifier
            .clip(shape)
            .background(p.surface2)
            .border(1.dp, p.border, shape)
            .clickable(onClick = onClick)
            .padding(horizontal = 12.dp, vertical = 7.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(6.dp),
    ) {
        if (icon != null) Icon(icon, null, tint = p.muted, modifier = Modifier.size(16.dp))
        Text(text, style = MaterialTheme.typography.bodyMedium, color = p.text, maxLines = 1)
        if (showCaret) Icon(Icons.Outlined.ArrowDropDown, null, tint = p.muted, modifier = Modifier.size(18.dp))
    }
}

/**
 * Generic single-field dialog: optional or required note/text, with confirm/cancel.
 */
@Composable
fun NoteDialog(
    title: String,
    label: String,
    confirmText: String,
    onConfirm: (String) -> Unit,
    onDismiss: () -> Unit,
    message: String? = null,
    required: Boolean = false,
    placeholder: String? = null,
    initial: String = "",
    danger: Boolean = false,
    loading: Boolean = false,
) {
    val p = BB.colors
    var text by remember { mutableStateOf(initial) }
    AlertDialog(
        onDismissRequest = onDismiss,
        containerColor = p.surface,
        title = { Text(title, style = MaterialTheme.typography.headlineSmall, color = p.text) },
        text = {
            Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                if (message != null) Text(message, style = MaterialTheme.typography.bodyMedium, color = p.muted)
                BBTextField(
                    value = text,
                    onValueChange = { text = it },
                    label = label,
                    placeholder = placeholder,
                    singleLine = false,
                    minLines = 3,
                    modifier = Modifier.fillMaxWidth(),
                )
            }
        },
        confirmButton = {
            BBButton(
                confirmText,
                onClick = { onConfirm(text.trim()) },
                style = if (danger) BBButtonStyle.Danger else BBButtonStyle.Primary,
                enabled = !required || text.isNotBlank(),
                loading = loading,
                small = true,
            )
        },
        dismissButton = { BBButton("Cancel", onClick = onDismiss, style = BBButtonStyle.Ghost, small = true) },
    )
}

@Composable
fun ClaimDialog(onConfirm: (String?) -> Unit, onDismiss: () -> Unit) {
    NoteDialog(
        title = "Request assignment",
        message = "Optionally add a pitch — why you're a good fit for this task.",
        label = "Pitch note (optional)",
        placeholder = "I've done similar work…",
        confirmText = "Request",
        required = false,
        onConfirm = { onConfirm(it.ifBlank { null }) },
        onDismiss = onDismiss,
    )
}
