import SwiftUI
import HealthDB
import HealthKit
import HealthKitExtensions

struct QuantitySampleList<T>: View where T: HKQuantitySampleContainer {

    let database: HealthDatabase

    var body: some View {
        GenericSampleList(sampleView: { sample in
            HStack {
                Text(sample.quantity.description)
                Spacer()
                Text(sample.endDate.formatted())
            }
        }, search: { start, end, _ -> [T] in
            try database.samples(includingSeriesData: true, from: start, to: end)
        })
        .navigationTitle(T.quantityTypeIdentifier.description)
    }
}

#Preview {
    NavigationStack {
        QuantitySampleList<ActiveEnergyBurned>(database: .empty)
    }
}
