import SwiftUI
import Bedrock

/// View for managing categories - presets and user-created
struct CategoryListView: View {
    @Bindable var store: CategoryStore
    
    @State private var showingAddSheet = false
    @State private var categoryToEdit: Category?
    @State private var categoryToDelete: Category?
    
    private var presetCategories: [Category] {
        store.categories.filter { $0.isPreset }
    }
    
    private var userCategories: [Category] {
        store.categories.filter { !$0.isPreset }
    }
    
    var body: some View {
        List {
            // Preset categories section
            Section {
                ForEach(presetCategories) { category in
                    categoryRow(category)
                        .onTapGesture {
                            categoryToEdit = category
                        }
                }
            } header: {
                Text(String(localized: "Default Categories"))
            } footer: {
                Text(String(localized: "Default categories cannot be renamed or deleted, but you can change their color."))
            }
            .listRowBackground(AppSurface.card)
            
            // User categories section
            Section {
                if userCategories.isEmpty {
                    Text(String(localized: "No custom categories yet"))
                        .font(.subheadline)
                        .foregroundStyle(AppTextColors.tertiary)
                } else {
                    ForEach(userCategories) { category in
                        categoryRow(category)
                            .onTapGesture {
                                categoryToEdit = category
                            }
                            .swipeActions(edge: .trailing, allowsFullSwipe: true) {
                                Button(role: .destructive) {
                                    categoryToDelete = category
                                } label: {
                                    Label(String(localized: "Delete"), systemImage: "trash")
                                }
                            }
                    }
                }
            } header: {
                Text(String(localized: "Custom Categories"))
            }
            .listRowBackground(AppSurface.card)
        }
        .scrollContentBackground(.hidden)
        .background(AppSurface.primary)
        .navigationTitle(String(localized: "Categories"))
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Button {
                    showingAddSheet = true
                } label: {
                    Image(systemName: "plus")
                        .foregroundStyle(AppAccent.primary)
                }
            }
        }
        .sheet(isPresented: $showingAddSheet) {
            CategoryEditSheet(store: store, category: nil)
        }
        .sheet(item: $categoryToEdit) { category in
            CategoryEditSheet(store: store, category: category)
        }
        .alert(String(localized: "Delete Category?"), isPresented: .init(
            get: { categoryToDelete != nil },
            set: { if !$0 { categoryToDelete = nil } }
        )) {
            Button(String(localized: "Cancel"), role: .cancel) {
                categoryToDelete = nil
            }
            Button(String(localized: "Delete"), role: .destructive) {
                if let category = categoryToDelete {
                    store.deleteCategory(category)
                }
                categoryToDelete = nil
            }
        } message: {
            Text(String(localized: "Rituals using this category will be set to no category."))
        }
    }
    
    private func categoryRow(_ category: Category) -> some View {
        HStack(spacing: Design.Spacing.medium) {
            Circle()
                .fill(category.color)
                .frame(width: 24, height: 24)
            
            Text(category.name)
                .font(.body)
                .foregroundStyle(AppTextColors.primary)
            
            Spacer()
            
            if category.isPreset {
                Text(String(localized: "Default"))
                    .font(.caption)
                    .foregroundStyle(AppTextColors.tertiary)
            }
            
            Image(systemName: "chevron.right")
                .font(.caption)
                .foregroundStyle(AppTextColors.tertiary)
        }
        .padding(.vertical, Design.Spacing.xSmall)
        .contentShape(Rectangle())
    }
}

#Preview {
    NavigationStack {
        CategoryListView(store: CategoryStore.preview)
    }
}
