import SwiftUI

struct RecordChangeComparisonCard: View {
    let displayName: String
    let currentCount: Int
    let previousCount: Int?
    let addedCount: Int
    let disappearedCount: Int
    let isCurrentValid: Bool
    let isPreviousTracked: Bool
    let onAddedTap: (() -> Void)?
    let onDisappearedTap: (() -> Void)?

    private var totalChange: Int {
        addedCount - disappearedCount
    }

    private var changeRate: String {
        guard isPreviousTracked, let prev = previousCount, prev > 0 else { return "–" }
        let rate = Double(totalChange) / Double(prev) * 100
        let symbol = totalChange >= 0 ? "+" : ""
        return "\(symbol)\(String(format: "%.1f", rate))%"
    }

    var body: some View {
        VStack(spacing: 12) {
            HStack(spacing: 16) {
                VStack(alignment: .leading, spacing: 8) {
                    Label("Before", systemImage: "clock.badge")
                        .font(.caption.weight(.semibold))
                        .foregroundStyle(.secondary)

                    if isPreviousTracked, let prev = previousCount {
                        Text("\(prev)")
                            .font(.title3.weight(.semibold))
                            .monospacedDigit()
                            .foregroundStyle(.primary)
                    } else {
                        Text("–")
                            .font(.title3.weight(.semibold))
                            .foregroundStyle(.secondary)
                    }
                }

                Spacer()

                VStack(alignment: .center, spacing: 8) {
                    Image(systemName: "arrow.right")
                        .font(.system(size: 14, weight: .semibold))
                        .foregroundStyle(.secondary)

                    if totalChange >= 0 {
                        Text("+\(totalChange)")
                            .font(.headline.weight(.semibold))
                            .foregroundStyle(Color.healthyGreen)
                    } else {
                        Text("\(totalChange)")
                            .font(.headline.weight(.semibold))
                            .foregroundStyle(Color.criticalRed)
                    }

                    Text(changeRate)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                        .monospacedDigit()
                }

                Spacer()

                VStack(alignment: .trailing, spacing: 8) {
                    Label("Now", systemImage: "checkmark.circle.fill")
                        .font(.caption.weight(.semibold))
                        .foregroundStyle(isCurrentValid ? .secondary : Color.criticalRed)

                    Text(isCurrentValid ? "\(currentCount)" : "Unavailable")
                        .font(.title3.weight(.semibold))
                        .monospacedDigit()
                        .foregroundStyle(isCurrentValid ? .primary : Color.criticalRed)
                }
            }
            .padding(12)
            .background(Color(.systemBackground).opacity(0.5), in: RoundedRectangle(cornerRadius: 8))

            if addedCount > 0 || disappearedCount > 0 {
                HStack(spacing: 12) {
                    if addedCount > 0 {
                        Button(action: { onAddedTap?() }) {
                            HStack(spacing: 6) {
                                Image(systemName: "plus.circle.fill")
                                    .foregroundStyle(Color.healthyGreen)
                                VStack(alignment: .leading, spacing: 2) {
                                    Text("Added")
                                        .font(.caption)
                                        .foregroundStyle(.secondary)
                                    Text("\(addedCount)")
                                        .font(.subheadline.weight(.semibold))
                                        .foregroundStyle(Color.healthyGreen)
                                        .monospacedDigit()
                                }
                                Spacer()
                                Image(systemName: "chevron.right")
                                    .font(.system(size: 12, weight: .semibold))
                                    .foregroundStyle(.secondary)
                            }
                            .padding(10)
                            .background(Color.healthyGreen.opacity(0.1), in: RoundedRectangle(cornerRadius: 6))
                        }
                        .buttonStyle(.plain)
                    }

                    if disappearedCount > 0 {
                        Button(action: { onDisappearedTap?() }) {
                            HStack(spacing: 6) {
                                Image(systemName: "minus.circle.fill")
                                    .foregroundStyle(Color.criticalRed)
                                VStack(alignment: .leading, spacing: 2) {
                                    Text("Disappeared")
                                        .font(.caption)
                                        .foregroundStyle(.secondary)
                                    Text("\(disappearedCount)")
                                        .font(.subheadline.weight(.semibold))
                                        .foregroundStyle(Color.criticalRed)
                                        .monospacedDigit()
                                }
                                Spacer()
                                Image(systemName: "chevron.right")
                                    .font(.system(size: 12, weight: .semibold))
                                    .foregroundStyle(.secondary)
                            }
                            .padding(10)
                            .background(Color.criticalRed.opacity(0.1), in: RoundedRectangle(cornerRadius: 6))
                        }
                        .buttonStyle(.plain)
                    }
                }
            }
        }
    }
}

#Preview {
    VStack(spacing: 16) {
        RecordChangeComparisonCard(
            displayName: "Step Count",
            currentCount: 1125,
            previousCount: 1000,
            addedCount: 150,
            disappearedCount: 25,
            isCurrentValid: true,
            isPreviousTracked: true,
            onAddedTap: nil,
            onDisappearedTap: nil
        )

        RecordChangeComparisonCard(
            displayName: "Heart Rate",
            currentCount: 500,
            previousCount: 450,
            addedCount: 100,
            disappearedCount: 50,
            isCurrentValid: true,
            isPreviousTracked: true,
            onAddedTap: nil,
            onDisappearedTap: nil
        )
    }
    .padding(16)
}
