import SwiftUI

struct FoodLibraryView: View {
    @State private var vm = FoodSearchViewModel()
    @State private var selectedFood: Food?

    var body: some View {
        VStack(spacing: 0) {
            // Search bar
            HStack {
                Image(systemName: "magnifyingglass")
                    .foregroundStyle(Color.textTertiary)
                TextField("Search foods...", text: $vm.searchText)
                    .textFieldStyle(.plain)
                    .autocorrectionDisabled()
                if !vm.searchText.isEmpty {
                    Button {
                        vm.searchText = ""
                        vm.searchResults = []
                    } label: {
                        Image(systemName: "xmark.circle.fill")
                            .foregroundStyle(Color.textTertiary)
                    }
                }
            }
            .padding(12)
            .background(Color.surfaceCard)
            .clipShape(RoundedRectangle(cornerRadius: 10))
            .padding(.horizontal)
            .padding(.top, 8)

            ScrollView {
                LazyVStack(spacing: 0) {
                    let displayFoods = vm.searchText.isEmpty ? vm.allFoods : vm.searchResults

                    if vm.isLoadingInitial {
                        LoadingView()
                    } else if displayFoods.isEmpty {
                        EmptyStateView(
                            icon: "tray",
                            title: "No foods found",
                            subtitle: vm.searchText.isEmpty
                                ? "Foods you add will appear here"
                                : "Try a different search term"
                        )
                    } else {
                        ForEach(displayFoods) { food in
                            Button {
                                selectedFood = food
                            } label: {
                                HStack {
                                    VStack(alignment: .leading, spacing: 2) {
                                        Text(food.name)
                                            .font(.subheadline.weight(.medium))
                                            .foregroundStyle(Color.textPrimary)
                                            .lineLimit(1)
                                        HStack(spacing: 8) {
                                            if let brand = food.brand, !brand.isEmpty {
                                                Text(brand)
                                                    .font(.caption)
                                                    .foregroundStyle(Color.textTertiary)
                                            }
                                            Text("\(Int(food.caloriesPerBase)) kcal")
                                                .font(.caption.weight(.medium))
                                                .foregroundStyle(Color.textSecondary)
                                            Text("\(Int(food.proteinPerBase))p \(Int(food.carbsPerBase))c \(Int(food.fatPerBase))f")
                                                .font(.caption)
                                                .foregroundStyle(Color.textTertiary)
                                        }
                                    }
                                    Spacer()
                                    Image(systemName: "chevron.right")
                                        .font(.caption2)
                                        .foregroundStyle(Color.textTertiary)
                                }
                                .padding(.horizontal, 20)
                                .padding(.vertical, 10)
                            }
                            Divider().padding(.leading, 20)
                        }
                    }
                }
            }
        }
        .background(Color.canvas)
        .task {
            await vm.loadInitial()
        }
        .onChange(of: vm.searchText) {
            vm.search()
        }
        .sheet(item: $selectedFood) { food in
            AddFoodSheet(food: food)
        }
    }
}
