package com.example.notesai.presentation.dialogs

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.notesai.data.model.Note
import com.example.notesai.util.AppColors
import com.example.notesai.util.Constants

@Composable
fun NoteDialog(
    categoryId: String, // Parameter untuk kategori ID
    note: Note? = null, // Null jika buat baru, isi jika edit
    onDismiss: () -> Unit,
    onSave: (String, String) -> Unit,
    onDelete: (() -> Unit)? = null
) {
    var title by remember { mutableStateOf(note?.title ?: "") }
    var description by remember { mutableStateOf(note?.description ?: "") }
    var showDeleteConfirm by remember { mutableStateOf(false) }

    // Delete confirmation dialog
    if (showDeleteConfirm) {
        AlertDialog(
            onDismissRequest = { showDeleteConfirm = false },
            containerColor = AppColors.Surface,
            shape = RoundedCornerShape(Constants.Radius.Large.dp),
            title = {
                Text(
                    "Hapus Catatan?",
                    color = AppColors.OnBackground,
                    fontWeight = FontWeight.Bold
                )
            },
            text = {
                Text(
                    "Catatan ini akan dipindahkan ke sampah.",
                    color = AppColors.OnSurfaceVariant
                )
            },
            confirmButton = {
                Button(
                    onClick = {
                        onDelete?.invoke()
                        showDeleteConfirm = false
                    },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = AppColors.Error
                    ),
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp)
                ) {
                    Text("Hapus", color = Color.White, fontWeight = FontWeight.Bold)
                }
            },
            dismissButton = {
                TextButton(
                    onClick = { showDeleteConfirm = false },
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp)
                ) {
                    Text("Batal", color = AppColors.OnSurfaceVariant)
                }
            }
        )
    }

    AlertDialog(
        onDismissRequest = onDismiss,
        containerColor = AppColors.Surface,
        shape = RoundedCornerShape(Constants.Radius.Large.dp),
        title = {
            Text(
                if (note == null) "Catatan Baru" else "Edit Catatan",
                color = AppColors.OnBackground,
                fontWeight = FontWeight.Bold,
                fontSize = 20.sp
            )
        },
        text = {
            Column(
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                // Title Input
                OutlinedTextField(
                    value = title,
                    onValueChange = { title = it },
                    label = {
                        Text(
                            "Judul",
                            color = AppColors.OnSurfaceVariant
                        )
                    },
                    placeholder = {
                        Text(
                            "Masukkan judul catatan",
                            color = AppColors.OnSurfaceTertiary,
                            fontSize = 14.sp
                        )
                    },
                    modifier = Modifier.fillMaxWidth(),
                    colors = OutlinedTextFieldDefaults.colors(
                        focusedTextColor = AppColors.OnBackground,
                        unfocusedTextColor = AppColors.OnSurface,
                        focusedContainerColor = AppColors.SurfaceVariant,
                        unfocusedContainerColor = AppColors.SurfaceVariant,
                        cursorColor = AppColors.Primary,
                        focusedBorderColor = AppColors.Primary,
                        unfocusedBorderColor = Color.Transparent
                    ),
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp),
                    singleLine = true
                )

                // Description Input
                OutlinedTextField(
                    value = description,
                    onValueChange = { description = it },
                    label = {
                        Text(
                            "Deskripsi",
                            color = AppColors.OnSurfaceVariant
                        )
                    },
                    placeholder = {
                        Text(
                            "Tambahkan deskripsi singkat...",
                            color = AppColors.OnSurfaceTertiary,
                            fontSize = 14.sp
                        )
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .heightIn(min = 120.dp, max = 200.dp),
                    colors = OutlinedTextFieldDefaults.colors(
                        focusedTextColor = AppColors.OnBackground,
                        unfocusedTextColor = AppColors.OnSurface,
                        focusedContainerColor = AppColors.SurfaceVariant,
                        unfocusedContainerColor = AppColors.SurfaceVariant,
                        cursorColor = AppColors.Primary,
                        focusedBorderColor = AppColors.Primary,
                        unfocusedBorderColor = Color.Transparent
                    ),
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp),
                    maxLines = 8
                )
            }
        },
        confirmButton = {
            Row(
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                // Delete button (if editing)
                if (onDelete != null) {
                    IconButton(
                        onClick = { showDeleteConfirm = true },
                        modifier = Modifier.size(48.dp)
                    ) {
                        Icon(
                            Icons.Default.Delete,
                            contentDescription = "Hapus",
                            tint = AppColors.Error
                        )
                    }
                }

                Spacer(modifier = Modifier.weight(1f))

                // Cancel button
                TextButton(
                    onClick = onDismiss,
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp),
                    modifier = Modifier.height(48.dp)
                ) {
                    Text(
                        "Batal",
                        color = AppColors.OnSurfaceVariant,
                        fontSize = 15.sp
                    )
                }

                // Save button
                Button(
                    onClick = {
                        if (title.isNotBlank()) {
                            onSave(title, description)
                        }
                    },
                    enabled = title.isNotBlank(),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = AppColors.Primary,
                        disabledContainerColor = AppColors.Primary.copy(alpha = 0.5f)
                    ),
                    shape = RoundedCornerShape(Constants.Radius.Medium.dp),
                    modifier = Modifier.height(48.dp)
                ) {
                    Text(
                        "Simpan",
                        color = Color.White,
                        fontWeight = FontWeight.Bold,
                        fontSize = 15.sp
                    )
                }
            }
        }
    )
}