package com.example.notesai.presentation.screens.starred

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.Search
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.notesai.presentation.components.EmptyState
import com.example.notesai.presentation.screens.starred.components.StarredNoteCard
import com.example.notesai.data.model.Note
import com.example.notesai.data.model.Category

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun StarredNotesScreen(
    notes: List<Note>,
    categories: List<Category>,
    searchQuery: String = "", // Tambahkan parameter ini
    onNoteClick: (Note) -> Unit,
    onUnpin: (Note) -> Unit
) {
    val starredNotes = notes
        .filter { it.isPinned && !it.isArchived && !it.isDeleted }
        .sortedByDescending { it.timestamp }

    // Filter berdasarkan search query dari ModernTopBar
    val filteredNotes = if (searchQuery.isBlank()) {
        starredNotes
    } else {
        starredNotes.filter { note ->
            note.title.contains(searchQuery, ignoreCase = true) ||
                    note.content.contains(searchQuery, ignoreCase = true) ||
                    note.description.contains(searchQuery, ignoreCase = 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.Star,
                    message = "Belum ada catatan berbintang",
                    subtitle = "Catatan yang ditandai berbintang 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 }
                    StarredNoteCard(
                        note = note,
                        categoryName = category?.name ?: "Unknown",
                        onClick = { onNoteClick(note) },
                        onUnpin = { onUnpin(note) }
                    )
                }
            }
        }
    }
}