import SwiftUI

struct DiagnosticReportsListSection: View {
    let title: String
    let emptyMessage: String
    let message: String?
    let reports: [DiagnosticReportFile]
    let onOpen: (DiagnosticReportFile) -> Void
    let onDelete: (DiagnosticReportFile) -> Void
    let onRefresh: () -> Void

    var body: some View {
        Section(title) {
            if reports.isEmpty {
                Text(emptyMessage)
                    .foregroundStyle(.secondary)
            } else {
                ForEach(reports) { report in
                    Button {
                        onOpen(report)
                    } label: {
                        VStack(alignment: .leading, spacing: 3) {
                            Text(report.displayTitle)
                                .font(.subheadline.weight(.semibold))
                                .foregroundStyle(.primary)
                                .lineLimit(2)
                            Text(report.displaySubtitle)
                                .font(.caption)
                                .foregroundStyle(.secondary)
                        }
                    }
                    .swipeActions {
                        Button(role: .destructive) {
                            onDelete(report)
                        } label: {
                            Label("Delete", systemImage: "trash")
                        }
                    }
                }
            }

            Button {
                onRefresh()
            } label: {
                Label("Refresh Results", systemImage: "arrow.clockwise")
            }

            if let message {
                Label(message, systemImage: "exclamationmark.triangle")
                    .font(.caption)
                    .foregroundStyle(Color.warningAmber)
            }
        }
    }
}
