import SwiftUI

/// Visual indicator of a data type's date range and quality
struct DataTypeRangeIndicator: View {
    let earliestDate: Date?
    let latestDate: Date?
    let quality: SnapshotQuality

    private var hasDateRange: Bool {
        earliestDate != nil && latestDate != nil
    }

    private var daySpan: Int? {
        guard let earliest = earliestDate, let latest = latestDate else { return nil }
        return Calendar.current.dateComponents([.day], from: earliest, to: latest).day ?? 0
    }

    var body: some View {
        VStack(spacing: 12) {
            HStack(spacing: 8) {
                Text("Data Range")
                    .font(.headline.weight(.semibold))

                Spacer()

                qualityBadge
            }

            if hasDateRange {
                dateRangeVisualization
            } else {
                Text("No data available")
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding(.vertical, 16)
            }
        }
        .padding(16)
        .background(Color(.systemBackground).opacity(0.5), in: RoundedRectangle(cornerRadius: 12))
    }

    @ViewBuilder
    private var qualityBadge: some View {
        if quality != .complete {
            Label("Incomplete", systemImage: "exclamationmark.triangle.fill")
                .font(.caption.weight(.medium))
                .foregroundStyle(Color.warningAmber)
                .padding(.horizontal, 8)
                .padding(.vertical, 4)
                .background(Color.warningAmber.opacity(0.12), in: Capsule())
        }
    }

    @ViewBuilder
    private var dateRangeVisualization: some View {
        if let earliest = earliestDate, let latest = latestDate, let span = daySpan {
            VStack(spacing: 12) {
                HStack(alignment: .top, spacing: 12) {
                    VStack(alignment: .center, spacing: 4) {
                        Image(systemName: "calendar.badge.clock")
                            .font(.system(size: 16, weight: .semibold))
                            .foregroundStyle(Color.healthyGreen)

                        VStack(alignment: .center, spacing: 2) {
                            Text("Earliest")
                                .font(.caption2.weight(.medium))
                                .foregroundStyle(.secondary)
                            Text(earliest, format: .dateTime.month().day().year())
                                .font(.caption.weight(.semibold))
                        }
                    }
                    .frame(maxWidth: .infinity)

                    VStack(alignment: .center, spacing: 4) {
                        Text("\(span)")
                            .font(.system(size: 18, weight: .semibold).monospacedDigit())
                            .foregroundStyle(.primary)

                        Text("days")
                            .font(.caption2.weight(.medium))
                            .foregroundStyle(.secondary)
                    }

                    VStack(alignment: .center, spacing: 4) {
                        Image(systemName: "calendar.badge.clock")
                            .font(.system(size: 16, weight: .semibold))
                            .foregroundStyle(Color.accentColor)

                        VStack(alignment: .center, spacing: 2) {
                            Text("Latest")
                                .font(.caption2.weight(.medium))
                                .foregroundStyle(.secondary)
                            Text(latest, format: .dateTime.month().day().year())
                                .font(.caption.weight(.semibold))
                        }
                    }
                    .frame(maxWidth: .infinity)
                }

                timelineBar
            }
        }
    }

    @ViewBuilder
    private var timelineBar: some View {
        if earliestDate != nil, latestDate != nil {
            ZStack(alignment: .leading) {
                RoundedRectangle(cornerRadius: 3)
                    .fill(Color(.systemGray5))

                RoundedRectangle(cornerRadius: 3)
                    .fill(
                        LinearGradient(
                            gradient: Gradient(colors: [Color.healthyGreen, Color.accentColor]),
                            startPoint: .leading,
                            endPoint: .trailing
                        )
                    )
                    .opacity(0.7)
            }
            .frame(height: 4)
        }
    }
}

#Preview {
    VStack(spacing: 20) {
        DataTypeRangeIndicator(
            earliestDate: Date.now.addingTimeInterval(-86400 * 30),
            latestDate: Date.now,
            quality: .complete
        )

        DataTypeRangeIndicator(
            earliestDate: Date.now.addingTimeInterval(-86400 * 7),
            latestDate: Date.now,
            quality: .partial
        )

        DataTypeRangeIndicator(
            earliestDate: nil,
            latestDate: nil,
            quality: .complete
        )
    }
    .padding()
}
