import SwiftUI

/// Displays a preview of the shared card with option to save to Contacts.
struct ClipCardPreview: View {
    let snapshot: SharedCardSnapshot
    let onSave: () -> Void

    var body: some View {
        VStack(spacing: ClipDesign.Spacing.xLarge) {
            Spacer()

            // Card content
            VStack(spacing: ClipDesign.Spacing.large) {
                // Profile photo or placeholder
                if let photoData = snapshot.photoData,
                   let uiImage = UIImage(data: photoData) {
                    Image(uiImage: uiImage)
                        .resizable()
                        .scaledToFill()
                        .frame(width: ClipDesign.Size.avatarLarge, height: ClipDesign.Size.avatarLarge)
                        .clipShape(.circle)
                } else {
                    Image(systemName: "person.crop.circle.fill")
                        .resizable()
                        .scaledToFit()
                        .frame(width: ClipDesign.Size.avatarLarge, height: ClipDesign.Size.avatarLarge)
                        .foregroundStyle(Color.Clip.secondaryText)
                }

                // Name
                Text(snapshot.displayName)
                    .font(.title)
                    .bold()
                    .foregroundStyle(Color.Clip.text)
                    .multilineTextAlignment(.center)

                // Role and company
                if !snapshot.role.isEmpty || !snapshot.company.isEmpty {
                    VStack(spacing: ClipDesign.Spacing.xSmall) {
                        if !snapshot.role.isEmpty {
                            Text(snapshot.role)
                                .font(.headline)
                                .foregroundStyle(Color.Clip.secondaryText)
                        }
                        if !snapshot.company.isEmpty {
                            Text(snapshot.company)
                                .font(.subheadline)
                                .foregroundStyle(Color.Clip.secondaryText)
                        }
                    }
                }
            }
            .padding(ClipDesign.Spacing.xLarge)
            .frame(maxWidth: .infinity)
            .background(Color.Clip.cardBackground)
            .clipShape(.rect(cornerRadius: ClipDesign.CornerRadius.xLarge))
            .padding(.horizontal, ClipDesign.Spacing.large)

            Spacer()

            // Save button
            Button(action: onSave) {
                HStack(spacing: ClipDesign.Spacing.small) {
                    Image(systemName: "person.crop.circle.badge.plus")
                    Text("Save to Contacts")
                }
                .font(.headline)
                .foregroundStyle(Color.Clip.background)
                .frame(maxWidth: .infinity)
                .frame(height: ClipDesign.Size.buttonHeight)
                .background(Color.Clip.accent)
                .clipShape(.capsule)
            }
            .padding(.horizontal, ClipDesign.Spacing.xLarge)
            .accessibilityLabel(Text("Save \(snapshot.displayName) to contacts"))

            // Get full app prompt
            Button {
                openAppStore()
            } label: {
                Text("Get the full app")
                    .font(.subheadline)
                    .foregroundStyle(Color.Clip.accent)
            }
            .padding(.bottom, ClipDesign.Spacing.large)
        }
    }

    private func openAppStore() {
        // Open App Store page for the full app
        // Replace with actual App Store URL when available
        if let url = URL(string: "https://apps.apple.com/app/id1234567890") {
            UIApplication.shared.open(url)
        }
    }
}

#Preview {
    ZStack {
        Color.Clip.background
            .ignoresSafeArea()

        ClipCardPreview(
            snapshot: SharedCardSnapshot(
                recordName: "test",
                vCardData: """
                BEGIN:VCARD
                VERSION:3.0
                N:Sullivan;Daniel;;;
                FN:Daniel Sullivan
                ORG:WR Construction
                TITLE:Property Developer
                END:VCARD
                """
            )
        ) {
            print("Save tapped")
        }
    }
}
