import SwiftUI

struct ShelfSectionView: View {
    let title: String
    let books: [BookRemote]
    let gradient: LinearGradient
    let imageCache: ImageCacheProtocol
    let onTapCategory: () -> Void
    let onTapBook: (BookRemote) -> Void

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack {
                Button(action: onTapCategory) {
                    Text(title)
                        .font(.title3.weight(.bold))
                        .foregroundStyle(Theme.textPrimary)
                }
                Spacer()
                Text("\(books.count) books")
                    .font(.caption)
                    .foregroundStyle(Theme.textSecondary)
                Image(systemName: "chevron.left")
                    .font(.caption2)
                    .foregroundStyle(.gray)
                Image(systemName: "chevron.right")
                    .font(.caption2)
                    .foregroundStyle(.gray)
            }

            ZStack(alignment: .bottom) {
                RoundedRectangle(cornerRadius: 16)
                    .fill(gradient)
                    .frame(height: 74)
                    .shadow(color: .black.opacity(0.12), radius: 10, y: 8)
                    .overlay {
                        HStack {
                            ScrewView()
                            Spacer()
                            ScrewView()
                        }
                        .padding(.horizontal, 12)
                    }

                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 12) {
                        if books.isEmpty {
                            ghostCovers
                        } else {
                            ForEach(books) { book in
                                Button {
                                    onTapBook(book)
                                } label: {
                                    BookCoverCard(book: book, imageCache: imageCache)
                                }
                            }
                        }
                    }
                    .padding(.horizontal, 16)
                    .padding(.bottom, 18)
                }
                .frame(height: 190)
            }
        }
        .padding(.horizontal, 20)
    }

    private var ghostCovers: some View {
        HStack(spacing: 10) {
            ForEach(0..<3, id: \.self) { _ in
                RoundedRectangle(cornerRadius: 10)
                    .fill(Color.white.opacity(0.5))
                    .frame(width: 92, height: 140)
                    .overlay {
                        Image(systemName: "book")
                            .foregroundStyle(.white.opacity(0.8))
                    }
            }
            Text(String(localized: "home.noBooks"))
                .font(.caption)
                .foregroundStyle(.secondary)
                .frame(width: 130, alignment: .leading)
        }
    }
}
