import SwiftUI

struct BookPickerView: View {

    @ObservedObject var viewModel: ShareViewModel
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        content
            .navigationTitle("Select Book")
            .navigationBarTitleDisplayMode(.inline)
    }

    @ViewBuilder
    private var content: some View {
        if viewModel.isLoading && viewModel.books.isEmpty {
            ProgressView("Loading books…")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
        } else if viewModel.books.isEmpty {
            ContentUnavailableView(
                "No books found",
                systemImage: "book.closed",
                description: Text("This shelf has no books yet.")
            )
        } else {
            List(viewModel.books) { book in
                Button {
                    Task {
                        await viewModel.selectBook(book)
                        dismiss()
                    }
                } label: {
                    HStack {
                        Text(book.name)
                            .foregroundStyle(.primary)
                        Spacer()
                        if viewModel.selectedBook?.id == book.id {
                            Image(systemName: "checkmark")
                                .foregroundStyle(Color.accentColor)
                        }
                    }
                }
            }
        }
    }
}
