package com.example.notesai.presentation.screens.trash

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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 com.example.notesai.presentation.components.EmptyState
import com.example.notesai.presentation.screens.trash.components.TrashNoteCard
import com.example.notesai.presentation.screens.trash.components.TrashCategoryCard
import com.example.notesai.data.model.Note
import com.example.notesai.data.model.Category

@Composable
fun TrashScreen(
    notes: List<Note>,
    categories: List<Category>,
    searchQuery: String = "", // Tambahkan parameter ini
    onRestoreNote: (Note) -> Unit,
    onDeleteNotePermanent: (Note) -> Unit,
    onRestoreCategory: (Category) -> Unit,
    onDeleteCategoryPermanent: (Category) -> Unit
) {
    // Filter kategori dan note yang dihapus
    val deletedCategories = categories.filter { it.isDeleted }
    val deletedNotes = notes.filter { it.isDeleted }

    // Filter berdasarkan search query dari ModernTopBar
    val filteredCategories = if (searchQuery.isBlank()) {
        deletedCategories
    } else {
        deletedCategories.filter { category ->
            category.name.contains(searchQuery, ignoreCase = true)
        }
    }

    val filteredNotes = if (searchQuery.isBlank()) {
        deletedNotes
    } else {
        deletedNotes.filter { note ->
            note.title.contains(searchQuery, ignoreCase = true) ||
                    note.content.contains(searchQuery, ignoreCase = true) ||
                    note.description.contains(searchQuery, ignoreCase = true) ||
                    categories.find { it.id == note.categoryId }?.name?.contains(searchQuery, ignoreCase = true) == true
        }
    }

    Column(modifier = Modifier.fillMaxSize()) {
        // Content
        if (filteredCategories.isEmpty() && filteredNotes.isEmpty()) {
            if (searchQuery.isNotEmpty()) {
                EmptyState(
                    icon = Icons.Default.Search,
                    message = "Tidak ada hasil",
                    subtitle = "Coba kata kunci lain"
                )
            } else {
                EmptyState(
                    icon = Icons.Default.Delete,
                    message = "Sampah kosong",
                    subtitle = "Kategori dan catatan yang dihapus akan muncul di sini"
                )
            }
        } else {
            LazyColumn(
                contentPadding = PaddingValues(
                    start = 16.dp,
                    end = 16.dp,
                    top = 8.dp,
                    bottom = 100.dp
                ),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                // Section: Kategori Terhapus
                if (filteredCategories.isNotEmpty()) {
                    item {
                        Text(
                            "Kategori Terhapus (${filteredCategories.size})",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                            color = Color(0xFF94A3B8),
                            modifier = Modifier.padding(vertical = 8.dp)
                        )
                    }

                    items(filteredCategories) { category ->
                        val notesInCategory = notes.count {
                            it.categoryId == category.id && it.isDeleted
                        }
                        TrashCategoryCard(
                            category = category,
                            noteCount = notesInCategory,
                            onRestore = { onRestoreCategory(category) },
                            onDeletePermanent = { onDeleteCategoryPermanent(category) }
                        )
                    }
                }

                // Section: Catatan Terhapus
                if (filteredNotes.isNotEmpty()) {
                    item {
                        Text(
                            "Catatan Terhapus (${filteredNotes.size})",
                            style = MaterialTheme.typography.titleMedium,
                            fontWeight = FontWeight.Bold,
                            color = Color(0xFF94A3B8),
                            modifier = Modifier.padding(vertical = 8.dp)
                        )
                    }

                    items(filteredNotes) { note ->
                        val category = categories.find { it.id == note.categoryId }
                        TrashNoteCard(
                            note = note,
                            categoryName = category?.name ?: "Unknown",
                            onRestore = { onRestoreNote(note) },
                            onDeletePermanent = { onDeleteNotePermanent(note) }
                        )
                    }
                }
            }
        }
    }
}