package com.example.notesai.presentation.screens.archive

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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Archive
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.notesai.data.model.Note
import com.example.notesai.data.model.Category
import com.example.notesai.presentation.components.EmptyState
import com.example.notesai.presentation.screens.archive.components.ArchiveNoteCard

@Composable
fun ArchiveScreen(
    notes: List<Note>,
    categories: List<Category>,
    searchQuery: String = "", // Tambahkan parameter ini
    onRestore: (Note) -> Unit,
    onDelete: (Note) -> Unit
) {
    // Filter berdasarkan search query dari ModernTopBar
    val filteredNotes = if (searchQuery.isBlank()) {
        notes
    } else {
        notes.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 (filteredNotes.isEmpty()) {
            if (searchQuery.isNotEmpty()) {
                EmptyState(
                    icon = Icons.Default.Search,
                    message = "Tidak ada hasil",
                    subtitle = "Coba kata kunci lain"
                )
            } else {
                EmptyState(
                    icon = Icons.Default.Archive,
                    message = "Arsip kosong",
                    subtitle = "Catatan yang diarsipkan 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)
            ) {
                items(filteredNotes) { note ->
                    val category = categories.find { it.id == note.categoryId }
                    ArchiveNoteCard(
                        note = note,
                        categoryName = category?.name ?: "Unknown",
                        onRestore = { onRestore(note) },
                        onDelete = { onDelete(note) }
                    )
                }
            }
        }
    }
}