import SwiftUI

struct SnapshotNavigationHeader: View {
    let snapshots: [HealthSnapshot]
    let currentSnapshot: HealthSnapshot?
    let onSnapshotSelected: (HealthSnapshot) -> Void

    private var previousSnapshot: HealthSnapshot? {
        guard let current = currentSnapshot,
              let idx = snapshots.firstIndex(where: { $0.id == current.id }),
              idx > 0 else { return nil }
        return snapshots[idx - 1]
    }

    private var nextSnapshot: HealthSnapshot? {
        guard let current = currentSnapshot,
              let idx = snapshots.firstIndex(where: { $0.id == current.id }),
              idx < snapshots.count - 1 else { return nil }
        return snapshots[idx + 1]
    }

    var body: some View {
        if #available(iOS 26.0, *) {
            GlassEffectContainer(spacing: 10) {
                content
                    .padding(.horizontal, 8)
                    .frame(height: 52)
                    .background(Color(.systemBackground).opacity(0.08), in: Capsule())
                    .glassEffect(
                        .regular.tint(Color(.systemBackground).opacity(0.14)),
                        in: Capsule()
                    )
                    .shadow(color: .black.opacity(0.18), radius: 18, x: 0, y: 8)
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 6)
        } else {
            content
                .padding(.horizontal, 8)
                .frame(height: 52)
                .background(.ultraThinMaterial, in: Capsule())
                .overlay(
                    Capsule()
                        .strokeBorder(Color.primary.opacity(0.08), lineWidth: 1)
                )
                .shadow(color: .black.opacity(0.14), radius: 16, x: 0, y: 8)
                .padding(.horizontal, 12)
                .padding(.vertical, 6)
        }
    }

    private var content: some View {
        HStack(spacing: 12) {
            navigationButton(
                systemName: "arrowshape.backward",
                target: previousSnapshot,
                accessibilityLabel: "Previous snapshot"
            )

            Spacer(minLength: 8)

            Menu {
                ForEach(snapshots) { snapshot in
                    Button {
                        onSnapshotSelected(snapshot)
                    } label: {
                        if snapshot.id == currentSnapshot?.id {
                            Label(
                                snapshot.timestamp.formatted(.dateTime.year().month().day().hour().minute()),
                                systemImage: "checkmark"
                            )
                        } else {
                            Text(snapshot.timestamp, format: .dateTime.year().month().day().hour().minute())
                        }
                    }
                }
            } label: {
                HStack(spacing: 7) {
                    Image(systemName: "camera.viewfinder")
                        .font(.system(size: 16, weight: .semibold))
                        .symbolRenderingMode(.hierarchical)
                        .foregroundStyle(.secondary)

                    Text(currentSnapshot?.timestamp ?? Date(), format: .dateTime.month().day().hour().minute())
                        .font(.headline.weight(.semibold))
                }
            }
            .buttonStyle(.plain)
            .lineLimit(1)
            .accessibilityLabel("Select current snapshot")

            Spacer(minLength: 8)

            navigationButton(
                systemName: "arrowshape.forward",
                target: nextSnapshot,
                accessibilityLabel: "Next snapshot"
            )
        }
    }

    @ViewBuilder
    private func navigationButton(
        systemName: String,
        target: HealthSnapshot?,
        accessibilityLabel: String
    ) -> some View {
        if let target = target {
            Button {
                onSnapshotSelected(target)
            } label: {
                Image(systemName: systemName)
                    .font(.system(size: 22, weight: .semibold))
                    .symbolRenderingMode(.hierarchical)
                    .frame(width: 48, height: 44)
                    .contentShape(Rectangle())
            }
            .buttonStyle(.plain)
            .foregroundStyle(Color.primary)
            .accessibilityLabel(accessibilityLabel)
        } else {
            Color.clear
                .frame(width: 48, height: 44)
        }
    }
}

#Preview {
    let mockSnapshots = (0..<3).map { idx in
        HealthSnapshot(
            timestamp: Calendar.current.date(byAdding: .day, value: -idx, to: Date.now) ?? .now,
            osVersion: "iOS 26.4",
            deviceName: "Preview iPhone",
            deviceID: "preview-device"
        )
    }

    VStack {
        SnapshotNavigationHeader(
            snapshots: mockSnapshots,
            currentSnapshot: mockSnapshots.first,
            onSnapshotSelected: { _ in }
        )
        Spacer()
    }
}
