import SwiftUI

struct LibraryGridView: View {
    let items: [LibraryItem]
    var onRefresh: (() async -> Void)? = nil
    var onSelect: (LibraryItem) -> Void

    var body: some View {
        ScrollView {
            LazyVGrid(columns: gridColumns, spacing: 8) {
                ForEach(items) { item in
                    LibraryItemCell(item: item)
                        .onTapGesture { onSelect(item) }
                }
            }
            #if os(iOS)
            .padding(.horizontal, 8)
            .padding(.vertical, 4)
            #else
            .padding(20)
            #endif
        }
        #if os(iOS)
        .refreshable { await onRefresh?() }
        #endif
    }

    private var gridColumns: [GridItem] {
        #if os(iOS)
        // 3 equal columns — compact spacing for full height utilization
        [GridItem(.flexible(), spacing: 8),
         GridItem(.flexible(), spacing: 8),
         GridItem(.flexible())]
        #else
        [GridItem(.adaptive(minimum: 180), spacing: 20)]
        #endif
    }
}
